二叉排序树
简介
二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树。是数据结构中的一类。在一般情况下,查询效率比链表结构要高。对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大,如果有相同的值,可以将该节点放在左子节点或右子节点。
代码
创建二叉排序树和遍历
节点对象
class Node {
int value;
Node left;
Node right;
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public Node(int value) {
this.value = value;
}
/**
* 添加结点的方法(二叉排序树)
* @param node 添加的结点
*/
public void add(Node node) {
if (node == null) {
return;
}
if (this.value > node.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 infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
}
BinarySortTree对象
class BinarySortTree {
private Node root;
/**
* 添加结点的方法
* @param node 添加的结点
*/
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
//中序遍历
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉树为空,不能遍历");
}
}
}
测试
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i : arr) {
binarySortTree.add(new Node(i));
}
binarySortTree.infixOrder();
可以发现二叉排序树用中序遍历刚好是升序的顺序。
删除
三种情况
- 删除叶子结点
- 删除有一颗子结点的结点
- 删除有两颗子结点的结点
结点对象
//查找要删除的结点
public Node search(int value) {
if (value == this.value) {
return this;
} else if (value < this.value) {
if (this.left == null) {
return null;
}
return this.left.search(value);
} else {
if (this.right == null) {
return null;
}
return this.right.search(value);
}
}
//查找要删除的结点的父结点,没找到就返回null
public Node searchParent(int value) {
if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
return this;
} else {
if (value < this.value && this.left != null) {
return this.left.searchParent(value);
} else if (value >= this.value && this.right != null) {
return this.right.searchParent(value);
} else {
return null;
}
}
}
BinarySortTree对象
//查找要删除的结点
public Node search(int value) {
if (root == null) {
return null;
}
return root.search(value);
}
//查找要删除的结点的父结点
public Node searchParent(int value) {
if (root == null) {
return null;
}
return root.searchParent(value);
}
//删除结点
public void delNode(int value) {
if (root == null) {
return;
}
//得到要删除的结点对象
Node targetNode = search(value);
if (targetNode == null) {
return;
}
//如果满足说明只有一个根结点并且根结点就是要删除的结点
if (root.left == null && root.right == null) {
root = null;
return;
}
//要删除结点的父结点
Node parentNode = searchParent(value);
if (targetNode.left == null && targetNode.right == null) { //叶子结点
if (parentNode.left != null && parentNode.left.value == value) { //判断被删除的结点是不是父结点的左子结点
parentNode.left = null;
} else if (parentNode.right != null && parentNode.right.value == value) { //判断被删除的结点是不是父结点的右子结点
parentNode.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) { //被删除的节点有两个子结点
targetNode.value = delRightTreeMin(targetNode.right);
} else { //反之只有一个结点
if (targetNode.left != null) {
if (parentNode != null) {
if (parentNode.left != null && parentNode.left.value == value) {
parentNode.left = targetNode.left;
} else {
parentNode.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else {
if (parentNode != null) {
if (parentNode.left != null && parentNode.left.value == value) {
parentNode.left = targetNode.right;
} else {
parentNode.right =targetNode.right;
}
} else {
root = targetNode.right;
}
}
}
}
/**
* 删除node 为根结点的二叉排序树的最小结点
* @param node 传入的结点
* @return 返回以node为根节点的二叉排序树的最小结点的值
*/
public int delRightTreeMin(Node node) {
Node temp = node;
while (temp.left != null) {
temp = temp.left;
}
delNode(temp.value);
return temp.value;
}
测试
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i : arr) {
binarySortTree.add(new Node(i));
}
binarySortTree.infixOrder();
binarySortTree.delNode(3);
binarySortTree.delNode(12);
binarySortTree.delNode(1);
System.out.println("删除之后=========");
binarySortTree.infixOrder();