码迷,mamicode.com
首页 > 编程语言 > 详细

java :非递归方法的二叉搜索树,实现部分功能

时间:2016-05-12 16:40:32      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

//
//实现了如下功能
public class BinarySearchTree {
	
	/**
	 * 
	 * @author Administrator
	 *嵌套类构造结点
	 * @param <Integer>
	 */
	private static class BinaryNode<Integer>{
		public Integer data;
		public BinaryNode<Integer> lchild;
		public BinaryNode<Integer> rchild;
		
		public BinaryNode(Integer d,BinaryNode<Integer> l,BinaryNode<Integer> r){
			this.data = d;
			this.lchild = l;
			this.rchild = r;
		}
	}
	
	//根节点
	private BinaryNode<Integer> root;
	
	public BinarySearchTree(){
		root = null;
	}
	
	/**
	 * 返回根节点
	 * @param x
	 * @param r
	 * @return
	 */
	public BinaryNode<Integer> add(Integer x,BinaryNode<Integer> r){
		BinaryNode<Integer> tmp = r;
		BinaryNode<Integer> p = new BinaryNode<Integer>(x,null,null);
		if(tmp == null){
			tmp = p;
		}else{
			while(tmp.lchild!=p && tmp.rchild != p){
				if(x<tmp.data){
					if(tmp.lchild == null){
						tmp.lchild = p;
					}else{
						tmp = tmp.lchild;
					}
				  }else if(x>tmp.data){
					  if(tmp.rchild == null){
							tmp.rchild = p;
						}else{
							tmp = tmp.rchild;
						}
				}
				
			}
			
		}
		
		return r;
	}
	
	/**
	 * 返回最小的结点
	 * @param r
	 * @return
	 */
	public BinaryNode<Integer> findMin(BinaryNode<Integer> r){
		if(r != null){
			while(r.lchild != null){
				r = r.lchild;
			}
		}
		
		return r;
	}
	
	/**
	 * 返回最大的结点
	 * @param r
	 * @return
	 */
	public BinaryNode<Integer> findMax(BinaryNode<Integer> r){
		if(r != null){
			while(r.rchild != null){
				r = r.rchild;
			}
		}
		
		return r;
	}
	
	//删除一个节点
	public BinaryNode<Integer> remove(Integer x,BinaryNode<Integer> r){
		BinaryNode<Integer> tmp = r;
		BinaryNode<Integer> p = null ;//指示目标结点的父亲
		if(tmp == null){
			throw new NullPointerException();
		}
		
		while(tmp.data != x){
			if(x<tmp.data){
				p = tmp;
				tmp = tmp.lchild;
			}else if(x<tmp.data){
				p = tmp;
				tmp = tmp.rchild;
		}
	}
		//如果待删结点是叶子结点
		if(tmp.lchild == null && tmp.rchild == null){
			if(tmp == r){
				tmp = null; //若为根节点,将根节点置空
			}else if(p.lchild == tmp){
				p.lchild = null;
			}else{
				p.rchild = null;
			}
		}else
		
		//如果为单叶子结点
		if(tmp.lchild == null || tmp.rchild == null){
			if(tmp == r){
				if(tmp.lchild == null){
					tmp = tmp.rchild;
				}else if(tmp.rchild == null){
					tmp = tmp.lchild;
				}
			}else
			
			if(p.lchild == tmp){
				if(tmp.lchild == null){
					p.lchild = tmp.rchild;
				}else if(tmp.rchild == null){
					p.lchild = tmp.lchild;
				}
			} else if(p.rchild == tmp){
				if(tmp.lchild == null){
					p.lchild = tmp.rchild;
				}else if(tmp.rchild == null){
					p.lchild = tmp.lchild;
				}
			}
		}else {//目标结点的子结点都不为空
			BinaryNode<Integer> a = tmp ;
			BinaryNode<Integer> b = tmp.rchild ;
			
			while(b.lchild != null){//选右边数最小的数据
				a = b;
				b = b.lchild;
			}
			
			tmp.data = b.data;
			if(a == tmp){
				tmp.rchild = b.rchild;//若目标结点的右子结点没有左子节点
			}else{
				a.lchild = b.rchild;
			}
			
		}
		return r;
	}
}

java :非递归方法的二叉搜索树,实现部分功能

标签:

原文地址:http://blog.csdn.net/cool_ben/article/details/51366883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!