二叉搜索树(二叉排列树或二叉查找树)

1、二叉排序树性质:
1、就是若它的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
2、若它的右子树不空,则右子树上所有节点的值均大于其根节点的值。
3、换句话说就是:任何节点的键值一定大于其左子树中的每一个节点的键值,并小于其右子树中的每一个节点的键值。

2、二叉排序树查找
要在二叉树中找出查找最大最小元素是极简单的事情,从根节点一直往左走,直到无路可走就可得到最小值;从根节点一直往右走,直到无路可走,就可以得到最大值。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package org.sxt.tree2;
/*二叉搜索树/二叉查找树/二叉排列数
 */
public class Node {
	int value;
	Node left;
	Node right;
	 
	public Node(int value) {
		this.value=value;
	}
	
	//向子树中添加节点
	public void add(Node node) {
		if(node==null) {
			return;
		}
		//判断传入节点的值比当前节点大还是小
		//添加节点比当前节点更小
		if(node.value<this.value) {
			//如果左节点为空
			if(this.left==null) {
				this.left=node;
			}else {
				this.left.add(node);
			}
		}else{
			//如果右节点为空
			if(this.right==null) {
				this.right=node;
			}else {
				this.right.add(node);
			}
		}
	}
 
	//中序遍历方法
	public void midShow(Node node) {
		if(node==null)
			return;
		midShow(node.left);
		System.out.println(node.value);
		midShow(node.right);
	}
	
	
//查找节点
	public Node search(int value) {
		if(this.value==value) {
			return this;
		}else if(value<this.value){
			if(left==null)
				return null;
			return left.search(value);
		}else {
			if(right==null)
				return null;
			return right.search(value);
		}
	}
	
	//搜索父节点
	public Node searchParent(int value) {
		if((this.left!=null&&this.left.value==value)||(this.right!=null&&this.right.value==value)) {
			return this;
		}else {
			if(this.value>value&&this.left!=null) {
				return this.left.searchParent(value);
			}else if(this.value<value&&this.right!=null) {
				return this.right.searchParent(value);
			}
			return null;
		}
	}
	
	

	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}
	
	
	
	
}

package org.sxt.tree2;
/*二叉搜索树/二叉查找树/二叉排列数
 */

public class BinarySortTree {
	Node root;
	//向二叉树中添加节点
	public void add(Node node) {
	//如果是一颗空树
	if(root==null) {
		root=node;
	}else {
		root.add(node);
	}
}



//中序遍历二叉树,正好是从大到小的顺序
public void midShow() {
	if(root!=null) {
		root.midShow(root);
	}
 }

//查找节点
public Node search(int value) {
      if(root==null) {
    	  return null;
       }else {
    	   return root.search(value);
       }
}

//查找父节点
public Node searchParent(int value) {
    if(root==null) {
    	return null;
    }else {
    	return root.searchParent(value);
    }
}

//删除节点
public void delete(int value) {
	if(root==null) {
		return;
	}else {
		//找到这个节点
		Node target=search(value);
		//如果没有这个节点
		if(target==null) {
			return;
		}
		//找到他的父节点
		Node parent=searchParent(value);
		//如果要删除的是叶子节点,直接将其父节点的指向变空
		if(target.left==null&&target.right==null) {
			//要删除节点为父节点的子节点
			if(parent.left.value==value)
				parent.left=null;
			else
				parent.right=null;
			//要删除的节点有两个节点的情况
		}else if(target.left!=null&&target.right!=null) {
			//删除有子树中最小的节点
			int min =deleteMin(target.right);
			//替换目标节点种的值
			target.value=min;
			
			//要删除的节点有一个左节点或一个右节点
		}else {
			//有左子节点
			if(target.left!=null) {
				//要删除的节点是父节点的左子节点
				if(parent.left.value==value)
					parent.left=target.left;
				else
					parent.right=target.left;
			}else {
				//有右子节点
				if(target.left!=null) {
					//要删除的节点是父节点的左子节点
					if(parent.left.value==value)
						parent.left=target.right;
					else
						parent.right=target.right;
			    }
		 }
	 }
 }
}


//删除一颗数据中最小的节点(一定在左树)
private int deleteMin(Node node) {
	Node target =node;
	//递归向左栈
	while(target.left!=null) {
		target=target.left;
	}
	//删除最小的这个节点
	delete(target.value);
	return target.value;
}


}
package org.sxt.tree2;
/*二叉搜索树/二叉查找树/二叉排列数
 */
public class TestBinarySortTree {

	public static void main(String[] args) {
		int[] arr=new int[] {7,3,10,12,5,1,9};
        //创建一颗二叉树
		BinarySortTree bst=new BinarySortTree();
		//循环添加
		for(int i:arr) {
			bst.add(new Node(i));
		}
		//中序遍历
		bst.midShow();
		
		//查找节点
		System.out.println("-------");
		Node node=bst.search(10);
		System.out.println(node);
		
		Node node2=bst.search(20);
		System.out.println(node2);
		
		//测试查找父节点
		Node p1=bst.searchParent(12);
		System.out.println(p1.value);
		
		System.out.println("---------");
		bst.delete(1);
		bst.midShow();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值