二叉搜索树的查找、添加和删除操作

class Node{
	int data;
	Node lChild;
	Node rChild;
	
	public Node(int data){
		this.data=data;
		this.lChild=null;
		this.rChild=null;
	}
}

public class Tree {
	
	public static void main(String[] args) {
		Node n1=new Node(45);
		Node n2=new Node(12);
		Node n3=new Node(53);
        Node n4=new Node(3);
        Node n5=new Node(37);
        Node n6=new Node(100);
        Node n7=new Node(24);
        Node n8=new Node(61);
        Node n9=new Node(90);
        Node n10=new Node(78);
        
        Node root=n1;
        root.lChild=n2;
        root.rChild=n3;
        n2.lChild=n4;
        n2.rChild=n5;
        n3.rChild=n6;
        n5.lChild=n7;
        n6.lChild=n8;
        n8.rChild=n9;
        n9.lChild=n10; 
       
        System.out.println(delete(root,n2.data));
	}

	/**
	 * 在二叉树中查找节点
	 * @param root 当前搜索节点
	 * @param num 查找的值
	 * @param f  当前搜索节点父节点
	 * @return 若找到节点,则返回该节点,否则返回空
	 */
	public static Node search(Node root,int num){
		if(root==null){	                     
			return null;   
		}
		if(root.data>num){
			return search(root.lChild,num);
		}else if(root.data<num){
			return search(root.rChild,num);
		}else{
			return root;
		}
	}
	
	/**
	 * 得到某一数值的节点的父节点
	 * @param root 根节点
	 * @param num  特定的数值
	 * @return  返回特定数值的根节点
	 */
	public static Node getFather(Node root,int num){
		if(root==null){
			return null;
		}
		if(root.data==num){
			return root;
		}
		Node temp=root;
		Node father = null;
		while(temp!=null){ 
			father=temp;
			if(temp.data<num){
				temp=temp.rChild;
			}else{
				temp=temp.lChild;
			}
			if(temp==null||temp.data==num){  //找到特定节点或者特定节点不存在
				break;
			}
		}
		return father;
	}
	
	/**
	 * 在已有的二叉树中插入新节点
	 * @param root 根节点
	 * @param num 待插入的数据
	 * @return root为空或此数据已存在返回false
	 */
	public static boolean insert(Node root,int num){
		if(root!=null){
			Node p=search(root,num);
			if(p==null){
				Node node=new Node(num);
				Node father=getFather(root,num);
				if(father.data>num)
					father.lChild=node;
				else
					father.rChild=node;
				return true;
			}else{  //树中已有此数据
				return false;
			}
		}
		return false;
	}
	
	/**
	 * 删除节点:
	 * (1)删除的节点为叶子节点:直接删除节点,将其父节点的引用置空
	 * (2)删除的节点有左子树或者有右子树:直接将其父节点的引用指向其子树
	 * (3)删除的节点有左右子树:找到其右子树上的最小节点,将其父节点的引用指向最小节点,连接
	 * 其左右子树
	 * @param root 根节点
	 * @param num 要删除的节点的数值
	 * @return 返回删除操作成功与否
	 */
	public static boolean delete(Node root,int num){
		Node father;
		Node next;
		Node f;
		Node p=search(root,num);
		boolean left=false;
		if(p==null){ //此二叉树中不存在要删除的节点
			return false;
		}
		if(root.data==num&&root.lChild==null&&root.rChild==null){ //如果要删除的是根节点
			root.data=0;  //且根节点是叶子节点,则将根节点数据置0
			return true;
		}
		father=getFather(root,num);
		if(father.data>p.data){
			left=true;
		}
		if(p.lChild==null&&p.rChild==null){ //要删除的节点为叶子节点
			if(left){
				father.lChild=null;
			}else{
				father.rChild=null;
			}
		}else if(p.lChild!=null&&p.rChild==null){ //左孩子不为空,右孩子为空
			if(left){
				father.lChild=p.lChild;
			}else{
				father.rChild=p.lChild;
			}
		}else if(p.lChild==null&&p.rChild!=null){ //左孩子为空,右孩子不为空
			if(left){
				father.lChild=p.rChild;
			}else{
				father.rChild=p.rChild;
			}
		}else{ //左右孩子均不为空
			f=p;
			next=p.rChild;
			while(next.lChild!=null){ //找到要删除的节点右子树上的最小节点,并记录其父节点
				f=next;
				next=next.lChild;
			}
			if(p==root){ //如果删除的是根节点
				root.data=next.data;
				if(f!=root){
					root.rChild=f.rChild;
					f.lChild=null;
				}else{
					root.rChild=next.rChild;
				}
				return true;
			}
			if(f==p){ //如果要删除节点的右子树上没有左子树
				f.rChild=null;
				next.rChild=null;
			}else{
				f.lChild=null;
				next.rChild=p.rChild; //连接右子树
			}
			next.lChild=p.lChild; //连接左子树
			if(left){
				father.lChild=next;
			}else{
				father.rChild=next;
			}
		}
		return true;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值