二叉排序树
定义
二叉排序树(Binary Sort Tree)又称二叉查找树、二叉搜索树。 它或者是一棵空树;或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
思路
-
添加节点
1) 判断被插结点是其父亲结点的左、右儿子。将被插结点作为叶子结点插入。
2) 若二叉树为空。则首先单独生成根结点。 -
删除节点
1)先去找到target节点
2)找到target节点的父节点parent
3)确定target是parent的左子节点L还是右子节点R
4)根据前面的情况来对应删除删除叶子节点:
左子节点parent.left = null
右子节点 parent.right = null删除只有一颗子树的节点
确定target的子节点是左子节点L还是右子节点R
target是parent的左子节点L还是右子节点R
target是parent的左子节点 -> target的子节点是左子节点
parent.left = target.left
其他情况依次类推删除有两颗子树的节点
从target的右子树找到最小的节点
用一个临时变量将最小节点的值保存temp
删除该最小节点
target.value = temp
代码实现
class BinarySortTree {
Node root;
public BinarySortTree() {
}
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("二叉排序树为空,不能遍历");
}
}
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 delNode(int value) {
if (root == null) {
return;
} else {
Node targetNode = search(value);
if (targetNode == null) {
return;
}
//如果只可二叉排序树只有一个节点
if (root.left == null && root.right == null) {
root = null;
return;
}
Node parent = searchParent(value);
//删除叶子节点情况
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
} else if (parent.right != null && parent.right.value == value) {
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) {
targetNode.value = delRightTreeMin(targetNode.right);
} else {
if (parent == null) {
if (targetNode.left != null) {
root = targetNode.left;
} else {
root = targetNode.right;
}
} else {
//如果target有左子节点
if (targetNode.left != null) {
if (parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
}
}
}
}
}
/**
* 删除node节点
* @param node 当做二叉排序树的根节点
* @return 返回以node为根节点的二叉排序树右子树最小节点的值
*/
public int delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
//这时temp指向了最小节点
delNode(target.value);
return target.value;
}
}
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 infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
//查找要删除的节点
public Node search(int value) {
if (this.value == 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);
}
}
//查找要删除节点的父节点
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; //没有父节点
}
}
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}