二叉树前序中序后序搜索程序Java

本文介绍了一种二叉搜索树的实现方法,包括节点插入、前序、中序及后序遍历,并展示了如何计算树的深度。通过具体实例演示了如何构建一棵二叉搜索树并进行遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.ArrayList;
import java.util.List;

class BSTNode{
	private int value=0;
	private BSTNode lchild=null;
	private BSTNode rchild=null;
	public BSTNode() {
		
	}
public BSTNode(int value) {
 	this.value=value;
		
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public BSTNode getLchild() {
		return lchild;
	}
	public void setLchild(BSTNode lchild) {
		this.lchild = lchild;
	}
	public BSTNode getRchild() {
		return rchild;
	}
	public void setRchild(BSTNode rchild) {
		this.rchild = rchild;
	}

}
public class binary {
	
	
	public static void insertBSTNode(BSTNode t,BSTNode root)
	{
		BSTNode next;
		next=root;
		/*while(true)
		{
			if(next.getLchild()!=null)
			{
				if(t.getValue()<next.getValue())
					next=next.getLchild();
				else if(next.getRchild()!=null)
					next=next.getRchild();
				else
				  {
					next.setRchild(t);
				    break;
				   }
			}
			else
			{
				next.setLchild(t);
				break;
			}
				
		}*/
		while(next!=null)
		{
			
				if(t.getValue()<next.getValue())
				{
					if(next.getLchild()!=null)
					{
					next=next.getLchild();
					}
					else
					{
						next.setLchild(t);
						return;
					}
				}
				else if(t.getValue()>next.getValue())
				{
					 if(next.getRchild()!=null)
					 {
						 next=next.getRchild();
					 }
						else
						  {
							next.setRchild(t);
						   return;
						   }
				}
			else{  
                throw new RuntimeException("插入节点值已经存在");  
            }  
			
		}
		
			
		return ;
	}
	 public static void queryBSTNodeByPre(BSTNode bstNode){  
	        if(bstNode !=null){  
	            System.out.println("值:"+bstNode.getValue());  
	            queryBSTNodeByPre(bstNode.getLchild());  
	            queryBSTNodeByPre(bstNode.getRchild());  
	        }else{  
	            return;  
	        }  
	    }  
	 public static void queryBSTNodeByOrder(BSTNode bstNode) {  
	        if(bstNode !=null){  
	            queryBSTNodeByOrder(bstNode.getLchild());  
	            System.out.println("值:"+bstNode.getValue());  
	            queryBSTNodeByOrder(bstNode.getRchild());  
	        }else{  
	            return;  
	        }  
	    }  
	 public static void queryBSTNodeBypost(BSTNode bstNode) {  
	        if(bstNode !=null){  
	            queryBSTNodeBypost(bstNode.getLchild());   
	            queryBSTNodeBypost(bstNode.getRchild());  
	            System.out.println("值:"+bstNode.getValue()); 
	        }else{  
	            return;  
	        }  
	    }  
//树的深度等于左子树深度或者右子树深度的最小值+1
//使用递归进行遍历
 public static int queryBSTNodelength(BSTNode bstNode) {  
		 int l;
		 int r;
	        if(bstNode !=null){  
	        	l=queryBSTNodelength(bstNode.getLchild());   
	        	r=queryBSTNodelength(bstNode.getRchild());  
	            return Math.max(l+1, r+1);
	        }else{  
	            return 0;  
	        }
		
	    } 

	public  static void main(String []args)
	{
		int[]arr= {8,1,6,7,9,2,5,3}; 
           //使用链表进行存储 ArrayList  是list的子类,所有定义成如下的父类句柄指向子类对象
		List <BSTNode>list =new  ArrayList <BSTNode> ();
		int i;
		for(i=0;i<arr.length;i++)
		{
                   //使用add方法将节点加入list数组列表方法里的对象需要new (new BSTNode(arr[i]));
              list.add(new BSTNode(arr[i]));
                }
// 赋值时需要使用list对象到的get(序号)的方法
         BSTNode root=list.get(0);
		for(i=1;i<arr.length;i++)
		{
			insertBSTNode(list.get(i),root);
		}
		
		System.out.println("前序遍历:");  
        queryBSTNodeByPre(root);  
        System.out.println();  
        System.out.println("中序遍历:");  
        queryBSTNodeByOrder(root);  
        System.out.println();  
        System.out.println("后序遍历:");  
        queryBSTNodeBypost(root);  
        System.out.println();  
        queryBSTNodelength(root);
        System.out.println("长度:"+queryBSTNodelength(root));  
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值