搜索二叉树(BST)的实现

本文详细介绍了如何使用Java实现搜索二叉树(BST),包括递归和非递归两种方法。通过理解BST的基本操作,如插入、删除和查找节点,读者将能够掌握二叉树的核心概念。
摘要由CSDN通过智能技术生成
搜索二叉树之一种特殊的二叉树,其右子树值大于根节点,根节点值大于左子树。因此,BST的插入、删除都要维持二叉树的这一特性。利用这一特性也使BST的元素查找效率提高,同时,本代码也实现了BST的递归遍历和非递归遍历。代码如下:
import java.util.ArrayDeque;
public class StdBST   
{
	
	BSTNode root;
	public StdBST()
	{
		root=null;
	}
	
	void visit(BSTNode p)
	{
		System.out.print(p.key+" ");
	}
	
	void insert(Object el)//插入元素
	{
		BSTNode p=root,pre=null;
		while(p!=null)
		{
		  pre=p;
	      if((int)el>(int)p.key)
		        p=p.right;
	      else p=p.left;
		}//跳出循环时,pre指向叶子节点,p指向null
		if(root==null)
			root=new BSTNode(el);
		else if((int)el>(int)pre.key)
			pre.right=new BSTNode(el);
		else 
			pre.left=new BSTNode(el);
	}
	
	void breadthFirst()//深度优先遍历
	{
		BSTNode p=root;
		ArrayDeque<bstnode> que=new ArrayDeque<>();
		if(p!=null)
		{
	    	que.offer(p);
	    	while(!que.isEmpty())
		    {
	    	   p=(BSTNode) que.poll();	
			   visit(p);
			   if(p.left!=null)
			       que.offer(p.left);
			   if(p.right!=null)
			     que.offer(p.right);
			   
		     }
		}
	}
	
	void preOrder(BSTNode p)//递归先序遍历
	{
		if(p!=null)
		{
			visit(p);
			preOrder(p.left);
			preOrder(p.right);
		}
	}
	
	void inOrder(BSTNode p)//递归中序遍历
	{
		if(p!=null)
		{
			inOrder(p.left);
			visit(p);
			inOrder(p.right);
		}
	}
	
	void postOrder(BSTNode p)//递归后序遍历
	{
		if(p!=null)
		{
			postOrder(p.left);
			postOrder(p.right);
			visit(p);
			
		}
	}
	
	void iterativePreorder()//非递归先序遍历
	{
		BSTNode p=root;
		if(p!=null)
		{
			ArrayDeque<bstnode> stack=new ArrayDeque<>();
			stack.push(p);
			while(!stack.isEmpty())
			{
				p=(BSTNode)stack.pop();
				visit(p);
				if(p.right!=null)
					stack.push(p.right);
				if(p.left!=null)
					stack.push(p.left);
			}
		}
	}
	
 
	void iterativePostorder()//非递归后序遍历
	{
		BSTNode p=root,q=root;
		ArrayDeque<bstnode> stack=new ArrayDeque<>();
		while(p!=null)
		{
			for(;p.left!=null;p=p.left)
				stack.push(p);
			while(p!=null&&(p.right==null||p.right==q))
			{
				visit(p);
				q=p;
				if(stack.isEmpty())
					return;
				p=(BSTNode)stack.pop();
			}
			stack.push(p);
			p=p.right;
		}
	}
	
	
	void iterativeInorder()//非递归中序遍历
	{
		BSTNode p=root;
		ArrayDeque<bstnode> stack=new ArrayDeque<>();
		while(p!=null)
		{
			while(p!=null)
			{
				if(p.right!=null)
					stack.push(p.right);
				stack.push(p);
				p=p.left;			
			}
			p=(BSTNode)stack.pop();
			while(!stack.isEmpty()&&p.right==null)
			{
				visit(p);
				p=(BSTNode)stack.pop();
			}
			visit(p);
			if(!stack.isEmpty())
				p=(BSTNode)stack.pop();
			else p=null;
		}
	}

     BSTNode search(BSTNode p,Object el)//查找元素
     {
    	 while(p!=null)
    	 {
    		 if(p.key==el)
    			 return p;
    		 else if((int)el<(int)p.key)
    			 p=p.left;
    		 else p=p.right;
    	 }
    	 return null;
     }


     void deleteByMerging(Object el)//归并删除
     {
    	 BSTNode tmp,node,pre=null,p=root;
    	 while(p!=null&&p.key!=el)
    	 {
    		 pre=p;
    		 if((int)el>(int)p.key)
    			 p=p.right;
    		 else
    			 p=p.left;
    	 }
    	 node=p;
    	 if(p!=null&&p.key==el)
    	 {
    		 if(node.left==null)
    			 node=node.right;
    		 else if(node.right==null)
    			 node=node.left;
    		 else
    		 {
    			  tmp=node.left;
    			  while(tmp.right!=null)
    				  tmp=tmp.right;
    			  tmp.right=node.right;
    			  node=node.left;
    		 }
    		 if(p==root)
    			 root=node;
    		 else if(pre.left==p)
    			 pre.left=node;
    		 else
    			 pre.right=node;
    			 
    	 }
    	 else if(root!=null)
    		 System.out.println("key"+el+"在此树中不存在");
    	 else 
    		 System.out.println("树为空");
     }
	


public class BSTNode //数的节点内部类
{
	Object key;
	BSTNode left,right;

	public BSTNode(Object key,BSTNode left,BSTNode right)
	{ 
		this.key=key;
		this.left=left;
		this.right=right;	
	}
	
	public BSTNode(Object key)
	{
		this(key,null,null);
	}
	
	public BSTNode()
	{
		left=right=null;
	}
}

public static void main(String[] args)
{
	StdBST st=new StdBST();
	st.insert(13);
	st.insert(25);
	st.insert(10);
	st.insert(20);
	st.insert(31);
	st.insert(12);
	st.insert(2);
	st.insert(29);
	st.insert(15);
	st.insert(21);
	st.insert(32);
	st.insert(28);
	st.insert(36);
	
	st.deleteByMerging(12);
	st.breadthFirst();
}

}

参考来源:数据结构与算法 Adam Drozdek
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值