基本介绍
对于二叉排序树的任何一个非叶子节点,要求左子结点的值比当前节点的值小,右子结点的值比当前节点的值大。
如果有相同的值,可以将该节点放在左子结点或者右子结点。
二叉排序树的删除
二叉排序树的删除情况比较复杂,有下面三种情况需要考虑
- 删除叶子节点(比如:2,5,9,12)
- 删除只有一颗子树的节点(比如:1)
- 删除有两颗子树的节点(比如:7,3,10)
package com.xxxx.binarysorttree;
public class BinarySortTreeDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 7, 3, 10, 12, 5, 1, 9, 2 };
BinarySortTree binarySortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++) {
binarySortTree.add(new Node(arr[i]));
}
// 中序遍历二叉排序树
System.out.println("中序遍历二叉排序树");
binarySortTree.infixOrder();
// 测试一下删除叶子节点
binarySortTree.delNode(7);
System.out.println("删除节点后");
binarySortTree.infixOrder();
}
}
class BinarySortTree {
private Node root;
// 添加节点的方法
public void add(Node node) {
if (root == null) {
root = node;// 如果root为空,直接让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);
}
}
/**
* @param node 传入的节点(当做二叉排序树的根节点)
* @return 返回的 已node为根节点的二叉排序数的最小节点值
*
*/
public int delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
// 这时target就指向了最小的节点
// 删除最小的节点
delNode(target.value);
return target.value;
}
// 删除节点
public void delNode(int value) {
if (root == null) {
return;
} else {
// 1.需要先去找到要删除的结点
Node targetNode = search(value);
// 如果没有要删除的节点,返回null
if (targetNode == null) {
return;
}
// 如果我们发现当前这棵二叉排序树只有一个节点
// if(root.left==null&&root.right==null) {
// root=null;
// return;
// }
// 找到targetNode的父节点
Node parent = searchParent(value);
// 如果要删除的节点是叶子节点
if (targetNode.left == null && targetNode.right == null) {
// 判断targetNode是父节点的左子结点还是右子结点
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) {// 删除有两个子结点的
int minValue = delRightTreeMin(targetNode.right);
targetNode.value = minValue;
} else {// 删除只有一颗子树的节点
// 如果要删除的节点有左子结点
if (targetNode.left != null) {
if (parent != null) {
// 如果targetNode是parent的左子结点
if (parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else {
if (parent != null) {
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
} else {
root = targetNode.right;
}
}
}
}
}
}
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
// 查找要删除的节点
/**
* @param value 希望删除的节点的值
* @return 如果找到返回该节点,否则返回null
*/
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);
}
}
// 查找要删除的节点的父节点
/**
* @param value要找到的节点的值
* @return 返回的是要删除的节点的父节点,如果没有就返回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;
}
}
}
// 添加节点的方法
// 递归的形式添加节点,注意需要满足二叉排序树的要求
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();
}
}
}