目 录
P49 4.22 二叉排序树的概述
BinarySortTree:插入(创建节点、直接插入)、查找性能较好!
P50 4.23 创建二叉排序树&添加节点
P51 4.24 二叉排序树中查找节点
P52 4.25 删除叶子节点
能在Tree里实现的都在树里,需要考虑左右节点的才会写入Node中。
P53 4.26 删除只有一颗子树的节点
P54 4.27 删除有两颗子树的节点
代码汇总
1、Node.java
package demo11;
public class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
/**
* 向子树中添加节点
*
* @param node
*/
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);
}
}
}
/**
* 中序遍历
*
* @param node
*/
public void midShow(Node node) {
if (node == null) {
return;
}
midShow(node.left);
System.out.print(node.value + "、");
midShow(node.right);
}
/**
* 查找节点
*
* @param value2
*/
public Node search(int value) {
if (this.value == value) {
return this;
} else if (value < this.value) {
if (left == null) {
return null;
}
return left.search(value);
} else {
if (right == null) {
return null;
}
return right.search(value);
}
}
/**
* 搜索父节点
*
* @param value
* @return
*/
public Node 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;
}
}
}
2、BinarySortTree.java
package demo11;
public class BinarySortTree {
Node root;
/**
* 向二叉排序树中添加节点
* @param node
*/
public void add(Node node) {
// 如果是一颗空树
if (root == null) {
root = node;
} else {
root.add(node);
}
}
/**
* 中序遍历二叉排序树,从小到大的顺序
*/
public void midShow() {
if (root != null) {
root.midShow(root);
}
}
/**
* 节点的查找
*
* @param value
* @return
*/
public Node search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
/**
* 删除节点
*
* @param 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.left == null && target.right == null) {
// 要删除的节点是父节点的左子节点
if (parent.left.value == value) {
parent.left = null;
// 要删除的节点是父节点的右子节点
} else {
parent.right = null;
}
// 要删除的节点有两个子节点的情况
} else if (target.left != null && target.right != null) {
// 删除右子树中值最小的节点,并获取到该节点的值
int min = deleteMin(target.right);
// 替换目标节点中的值
target.value = min;
// 要删除的节点有一个左子节点或右子节点
} else {
// 有左子节点
if (target.left != null) {
// 要删除的节点是父节点的左子节点
if (parent.left.value == value) {
parent.left = target.left;
// 要删除的节点是父节点的右子节点
} else {
parent.right = target.left;
}
// 有右子节点
} else {
// 要删除的节点是父节点的左子节点
if (parent.left.value == value) {
parent.left = target.right;
// 要删除的节点是父节点的右子节点
} else {
parent.right = target.right;
}
}
}
}
}
/**
* 删除一颗树中最小的节点
*
* @param right
* @return
*/
private int deleteMin(Node node) {
Node target = node;
// 递归向左找
while (target.left != null) {
target = target.left;
}
// 删除最小的这个节点
delete(target.value);
return target.value;
}
/**
* 搜索父节点
*
* @param value
* @return
*/
public Node searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}
}
3、TestBinarySortTree.java
package demo11;
public class TestBinarySortTree {
public static void main(String[] args) {
int[] arr = new int[] { 7, 3, 10, 12, 5, 1, 9 };
// 创建一颗二叉排序树
BinarySortTree bst = new BinarySortTree();
// 循环添加
for (int i : arr) {
bst.add(new Node(i));
}
// // 查看树中的值
// bst.midShow();
// System.out.println("-----");
// // 查找
// Node node = bst.search(10);
// System.out.println(node.value);
//
// Node node2 = bst.search(20);
// System.out.println(node2);
// //测试查找父节点
// Node p1 = bst.searchParent(12);
// System.out.println(p1.value);
// System.out.println("-----");
// //删除叶子节点
// bst.delete(5);
// bst.midShow();
// System.out.println("===");
// // 删除只有一个子节点的节点
// bst.delete(3);
bst.midShow();
// 删除有两个子节点的节点
bst.delete(3);
System.out.println("----");
bst.midShow();
}
}
嘿嘿嘿~