码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树

时间:2014-08-13 01:22:25      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:二叉搜索树   iterator   

	//搜索二叉树的节点
	private static class STNode<T> {
		private T nodeValue;
		private STNode<T> left;
		private STNode<T> right;
		private STNode<T> parent;
		
		//创建节点
		public STNode (T item,STNode<T> parentNode)
		{
			nodeValue = item;
			left = null;
			right = null;
			parent = parentNode;
		}

	}//end class STNode<T>

//add()插入节点
	public boolean add(T item){
		STNode<T > t = root, parent = null, newNode;
		int orderValue = 0;
		while(t != null){
			parent = t;
			orderValue = ((Comparable<T>)item).compareTo(t.nodeValue);
			if(orderValue == 0) {
				return false;
			}
			else if(orderValue < 0){
				t = t.left;
			}
			else{
				t = t.right;
			}
		}//end while
		
		newNode = new STNode<T>(item, parent);
		if(parent == null){
			root = newNode;
		}
		else if(orderValue < 0){
			parent.left = newNode;
		}
		else{
			parent.right = newNode;
		}
		treeSize ++;
		modCount ++;
		
		return true;
	}//end add()

//remove()删除指定节点
	private void removeNode(STNode<T> dNode){
		if(dNode == null){
			return ;
		}
		STNode<T> pNode,rNode;
		pNode = dNode.parent;
		
		//要删除的节点至少有一个子节点为空的情况
		if(dNode.left == null || dNode.right == null){
			if(dNode.left == null){
				rNode = dNode.right;
			}
			else{
				rNode = dNode.left;
			}
			//要删除的节点不为空
			if(rNode != null){
				rNode.parent = pNode;
			}
			
			if(pNode == null){
				root = rNode;
			}
			else if(((Comparable<T>)dNode.nodeValue).compareTo(pNode.nodeValue) < 0){
				pNode.left = rNode;
			}
			else{
				pNode.right = rNode;
			}			
		}//end if
		//当两个节点都不为空的时候,选择比要删除节点大的最小的节点作为目标节点,
		//把目标节点的值和要删除节点互换,然后目标节点节点。
		//找到的目标节点有两种情况,要么为要删除节点的右节点且左子节点为空,dNode.right = rNode.right;
		//要么目标节点的左子为空,且目标节为父节点的左子节点。pOfRNode.left = rNode.right;
		else{
			STNode<T> pOfRNode = dNode;
			rNode = dNode.right;
			pOfRNode = dNode;
			
			//找到要替换的节点
			while(rNode.left != null){
				pOfRNode = rNode;
				rNode = rNode.left;
			}
			dNode.nodeValue = rNode.nodeValue;
			if(pOfRNode == dNode ){
				dNode.right = rNode.right;
			}
			else{
				pOfRNode.left = rNode.right;
			}
			
			if(rNode.right != null){
				rNode.right.parent = pOfRNode;
			}
		}//end else
		
	}//end remove()
	
	
	//remove()根据值删除节点
	public boolean remove(Object item){
		STNode<T> dNode = findNode(item);
		if(dNode == null){
			return false;
		}
		
		removeNode(dNode);
		treeSize --;
		modCount++;
		return true;
	}//end remove(Object item)

//找到指定的节点
	private STNode<T> findNode(Object item){
		STNode<T> t = root;
		int orderValue;
		while(t != null){
			orderValue = ((Comparable<T>)item).compareTo(t.nodeValue);
			if(orderValue == 0){
				return t;
			}
			else if(orderValue < 0){
				t = t.left;
			}
			else{
				t = t.right;
			}
		}//end while
		return null;
	}//end findNode()

//内部类构造迭代器
	private class IteratorImpl implements Iterator<T>{
		private int exepectedModCount = modCount;
		private STNode<T> lastReturned = null;
		private STNode<T> nextNode = null;
		
		//构造函数
		public IteratorImpl(){
			nextNode = root;
			if(nextNode != null){
				while(nextNode.left != null){
					nextNode = nextNode.left;
				}
			}
		}//end 构造函数
		
		public boolean hasNext() {
			return nextNode != null;
		}

		public T next() {
			checkIteratorState();
			if(nextNode == null ){
				throw new NoSuchElementException("Iterator+没有节点了!");
			}
			
			lastReturned = nextNode;
			STNode<T> p ;
			//当前节点的右子节点不为空
			//找到右子节点中德最小值,即最左边的值
			if(nextNode.right != null){
				nextNode = nextNode.right;
				while(nextNode.left != null){
					nextNode = nextNode.left;
				}
			}
			//当前节点的右子节点为空
			//向上遍历知道当前节点为父节点的左子节点为止
			//则该父节点就是要找的下一个节点
			else{
				p = nextNode.parent;
				while( p != null && p.right == nextNode){
					nextNode= p;
					p = p.parent;
				}
				nextNode = p;
			}
			return lastReturned.nodeValue;
		}

二叉搜索树,布布扣,bubuko.com

二叉搜索树

标签:二叉搜索树   iterator   

原文地址:http://blog.csdn.net/sunfeilong1993/article/details/38522259

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