二叉排序树概述
-
为什么会有二叉排序树
之前我们学过顺序存储的二叉树和链式存储的二叉树,他俩都面临着结点的查询比较慢的缺点,于是便有了二叉排序树
-
什么是二叉排序树
二叉排序树,也叫二叉查找树,二叉搜索树,顾名思义查找结点便是十分方便。对于二叉树中任何一个非叶子结点,要求左子结点比当前结点值小,右子结点比当前结点大,因此针对于这种排列方式,要求该树不能有相同的结点
结点的增删查
public class BinarySortTreeNode {
int value;
BinarySortTreeNode left;
BinarySortTreeNode right;
public BinarySortTreeNode(int value) {
this.value = value;
}
public void middleShow(){
if (left != null){
left.middleShow();
}
System.out.println(this.value);
if (right != null){
right.middleShow();
}
}
public void add(BinarySortTreeNode 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 BinarySortTreeNode search(int value) {
if (this.value == value){
return this;
}else if (this.value>value){
if (left == null){
return null;
}
return left.search(value);
}else {
if (right == null){
return null;
}
return right.search(value);
}
}
public BinarySortTreeNode 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;
}
}
}
public class BinarySortTree {
BinarySortTreeNode root;
public void add(BinarySortTreeNode node){
if (root == null){
root = node;
}else {
root.add(node);
}
}
public void show() {
root.middleShow();
}
public BinarySortTreeNode search(int value){
if (root == null){
return null;
}else {
return root.search(value);
}
}
public BinarySortTreeNode searchParent(int value){
if (root == null){
return null;
}else {
if (root.value == value){
return null;
}
return root.searchParent(value);
}
}
public void delete(int value){
if (root == null){
return;
}else {
BinarySortTreeNode node = search(value);
if (node == null){
return;
}
BinarySortTreeNode parent = searchParent(value);
if (node.left == null&& node.right == null){
if (parent.left != null && parent.left.value == value){
parent.left = null;
}
if (parent.right != null&& parent.right.value == value){
parent.right = null;
}
}else if (node.left != null && node.right != null){
int min = deleteMin(node.right);
node.value = min;
}else{
if (node.left != null) {
if (parent.left != null &&parent.left.value == value){
parent.left = node.left;
}else {
parent.right = node.left;
}
}else {
if (parent.left != null && parent.left.value == value){
parent.left = node.right;
}else {
parent.right = node.right;
}
}
}
}
}
private int deleteMin(BinarySortTreeNode node) {
BinarySortTreeNode target = node;
while (target.left != null){
target = target.left;
}
delete(target.value);
return target.value;
}
}
public class TestBinarySortTree {
public static void main(String[] args) {
int[] arr = new int[]{5,7,9,1,6,3,4};
BinarySortTree tree = new BinarySortTree();
for (int i : arr) {
tree.add(new BinarySortTreeNode(i));
}
tree.show();
System.out.println("=======================");
System.out.println(tree.search(5).value);
System.out.println(tree.search(10));
System.out.println("=======================");
tree.delete(5);
tree.show();
}
}