二叉搜索树

package tree;

import java.util.Collection;
import java.util.Iterator;

public class STree<T> implements Collection<T>
{
	private STNode<T> root;
	private int treeSize;
	private int modCount;
	public STree()
	{
		root=null;
		treeSize=0;
		modCount=0;
	}
	private class IteratorImp1 implements Iterator<T>
	{
		private int expectedModCount=modCount;
		private STNode<T> lastReturned=null;
		private STNode<T> nextNode=null;
		public IteratorImp1()
		{
			nextNode=root;
			if(nextNode!=null)
				while(nextNode.left!=null)
				{
					nextNode=nextNode.left;
				}
		}
		public boolean hasNext() 
		{
			if(nextNode==null)
			return false;
			else
				return true;
		}

		
		public T next() 
		{
			if(nextNode==null)
				return null;
			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&&nextNode==p.right)
				{
					nextNode=p;
					p=p.parent;
				}
				nextNode=p;
			}
			return lastReturned.nodeValue;
		}

		
		public void remove() 
		{
			
			
		}
		
	}

	public T first()
	{
		STNode<T> t=root;
		if(t==null)
			return null; 
			while(t.left!=null)
			{
				t=t.left;
			}
			return t.nodeValue;
		
	}
	public T last()
	{
		STNode<T> nextNode=root;
		if(nextNode==null)
			return null;
		while(nextNode.right!=null)
		{
			nextNode=nextNode.right;
		}
		return nextNode.nodeValue;
			
	}
	public T find(T item)
	{
		STNode<T> t=findNode(item);
		T value=null;
		if(t!=null)
			value=t.nodeValue;
		return value;
	}
	private STNode<T> findNode(T item)
	{
		STNode<T> t=root;
		int orderValue=0;
		while(t!=null)
		{
			orderValue=((Comparable<T>)item).compareTo(t.nodeValue);
			if(orderValue==0)
				return t;
			else if(orderValue>0)
				t=t.right;
			else
				t=t.left;
		}
		return null;
	}
	public int size() 
	{
		
		return treeSize;
	}

	
	public boolean isEmpty() 
	{
		if(treeSize!=0)
		return false;
		else
			return true;
	}

	@Override
	public boolean contains(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	
	public Iterator<T> iterator()
	{
		
		return new IteratorImp1();
	}

	@Override
	public Object[] toArray() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object[] toArray(Object[] a) {
		// TODO Auto-generated method stub
		return null;
	}

	
	public boolean add(Object 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.right;
			else
				t=t.left;
		}
		newNode=new STNode(item,parent);
		if(parent==null)
			root=newNode;
		else if(orderValue>0)
			parent.right=newNode;
		else
			parent.left=newNode;
		treeSize++;
		modCount++;
		return true;
	}
	private void removeNode(STNode<T> dNode)
	{
		if(dNode==null)
			return;
		STNode<T> rNode,pNode;
		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;//将替换节点的parent指向待删除节点的父节点
				}
				
			if(pNode==null)
				root=rNode;
			else if(((Comparable<T>)dNode.nodeValue).compareTo(pNode.nodeValue)>0)
				pNode.right=rNode;
			else
				pNode.left=rNode;
				
		}
		else
		{
			STNode<T> pOfRNode;
			pOfRNode=dNode;
			rNode=dNode.right;
			while(rNode.left!=null)
			{
				pOfRNode=rNode;
				rNode=rNode.left;
			}
			dNode.nodeValue=rNode.nodeValue;
			if(pOfRNode==dNode)
			{
				pOfRNode.right=rNode.right;
			}
			else
				pOfRNode.left=rNode.right;
			if(rNode.right!=null)//易错点,如果rNode.right为null那么一个空对象将没有parent而抛出异常
				rNode.right.parent=pOfRNode;	
		}
		treeSize--;
		modCount++;
	}
	
	public boolean remove(Object item) 
	{
		STNode<T> dNode=findNode((T) item);
		if(dNode==null)
		return false;
		removeNode(dNode);
		return true;
	}

	@Override
	public boolean containsAll(Collection c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addAll(Collection c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeAll(Collection c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean retainAll(Collection c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值