二叉排序树
非空左子树的所有键值小于其根节点的键值
非空右子数的所有键值大于其根节点的键值
左右子数都是二叉排序树
创建
先是创建一棵树 然后进行添加节点
比根节点小那么就放到左子树 在进行递归
比根节点大那么就放到右子数 在进行递归
public void add(Node node) {
if (node == null) {
return;
}
if (node.value < this.value) {
if (this.leftNode == null) {
this.leftNode = node;
} else {
this.leftNode.add(node);
}
} else {
if (this.rightNode == null) {
this.rightNode = node;
} else {
this.rightNode.add(node);
}
}
}
查找
类似于二分查找
public Node search(int value) {
if (this.value == value) {
return this;
} else if (value < this.value) {
if (leftNode == null) {
return null;
}
return leftNode.search(value);
} else if (value > this.value) {
if (rightNode == null) {
return null;
}
return rightNode.search(value);
} else {
return null;
}
}
删除
要分成三种情况
1.删除的是叶子节点 不让前面的节点指向他就OK了
2.删除的节点有一个子节点 让其父节点指向他的子节点就OK了
3.删除的节点有左右儿子 用另外的节点去代替被删的节点
(1)右子数的最小元素 或者 (2)左子树的最大元素
首先是先写一个找父节点的方法
public Node searchParent(int value) {
if (this.leftNode != null && this.leftNode.value == value || this.rightNode != null && this.rightNode.value == value) {
return this;
} else {
if (this.value > value && this.leftNode != null) {
return this.leftNode.searchParent(value);
} else if (this.value < value && this.rightNode != null) {
return this.rightNode.searchParent(value);
}
return null;
}
}
删除叶子节点的时候
这个叶子节点是父节点的左二子 那么父亲的左儿子设为空 反之同样
if (parent.leftNode.value == value) {
parent.leftNode = null;
} else {
parent.rightNode = null;
}
}
删除有两个儿子的时候
找到(1)右子数的最小元素 或者 (2)左子树的最大元素
然后去替换要删除的节点的值
可以直接删了这个节点的右子树的最小元素 获取了这个值 让被删的节点的值等于这个值
private int deleteMin(Node node) {
Node target = node;
while (target.leftNode != null) {
target = target.leftNode;
}
delete(target.value);
return target.value;
}
int min = deleteMin(target.rightNode);
target.value = min;
删除只有一个儿子的时候
删除的节点是父亲的左儿子 并且自己有左儿子 那么让父亲的左儿子指向自己的左儿子
。。。
else {
if (target.leftNode != null) {
if (target.leftNode.value == value) {
parent.leftNode = target.leftNode;
} else {
parent.rightNode = target.leftNode;
}
} else {
if (target.leftNode.value == value) {
parent.leftNode = target.rightNode;
} else {
parent.rightNode = target.rightNode;
}
}
}
放在一起好乱。。。。
class Node{
int value;
Node leftNode;
Node rightNode;
public Node(int value) {
this.value = value;
}
public void add(Node node) {
if (node == null) {
return;
}
if (node.value < this.value) {
if (this.leftNode == null) {
this.leftNode = node;
} else {
this.leftNode.add(node);
}
} else {
if (this.rightNode == null) {
this.rightNode = node;
} else {
this.rightNode.add(node);
}
}
}
public void midShow(Node node) {
if (node == null) {
return;
}
midShow(node.leftNode);
System.out.print(node.value + " ");
midShow(node.rightNode);
}
public Node search(int value) {
if (this.value == value) {
return this;
} else if (value < this.value) {
if (leftNode == null) {
return null;
}
return leftNode.search(value);
} else if (value > this.value) {
if (rightNode == null) {
return null;
}
return rightNode.search(value);
} else {
return null;
}
}
public Node searchParent(int value) {
if (this.leftNode != null && this.leftNode.value == value || this.rightNode != null && this.rightNode.value == value) {
return this;
} else {
if (this.value > value && this.leftNode != null) {
return this.leftNode.searchParent(value);
} else if (this.value < value && this.rightNode != null) {
return this.rightNode.searchParent(value);
}
return null;
}
}
}
public class BinarySearchTree {
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);
}
}
private int deleteMin(Node node) {
Node target = node;
while (target.leftNode != null) {
target = target.leftNode;
}
delete(target.value);
return target.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.leftNode == null && target.rightNode == null) {
if (parent.leftNode.value == value) {
parent.leftNode = null;
} else {
parent.rightNode = null;
}
} else if (target.leftNode != null && target.rightNode != null) {
int min = deleteMin(target.rightNode);
target.value = min;
} else {
if (target.leftNode != null) {
if (target.leftNode.value == value) {
parent.leftNode = target.leftNode;
} else {
parent.rightNode = target.leftNode;
}
} else {
if (target.leftNode.value == value) {
parent.leftNode = target.rightNode;
} else {
parent.rightNode = target.rightNode;
}
}
}
}
}
}
测试
public static void main(String args[]) {
// 7 3 10 12 5 1 9
int[] arr = new int[]{7, 3, 10, 12, 5, 1, 9};
BinarySearchTree B = new BinarySearchTree();
for (int i : arr) {
B.add(new Node(i));
}
B.midShow();
System.out.println(" ");
Node node = B.search(10);
System.out.println(node.value);
Node Father = B.searchParent(1);
System.out.println(Father.value);
B.delete(7);
B.midShow();
}