学习地址:【数据结构与算法基础-java版】 🚀数据结构--Java专栏🚀
- 笔记01【01-09】【概述、数组基本使用】【源码、课件】
- 笔记02【10-18】【栈、队列、单链表(增删节点)、循环链表、双向循环链表、递归(斐波那契、汉诺塔)】
- 笔记03【19-27】【(时间、空间复杂度);八大排序(冒泡、快速、插入、希尔、选择、归并、基数、队列基数)】
- 笔记04【28-33】【树结构(二叉树)概述、创建、遍历、查找节点、删除节点】
- 笔记05【34-39】【顺序存储二叉树概述、二叉树遍历、堆排序、线索二叉树实现及遍历】
- 笔记06【40-48】【赫夫曼树、概述、原理分析、代码实现(数据压缩、创建编码表、解码、压缩文件、解压文件)】
- 笔记07【49-54】【二叉排序树(添加、查找、删除节点)】
- 笔记08【55-57】【二叉平衡树(AVL)-概述、单旋转、双旋转】
- 笔记09【58-60】【计算机中数据的存储原理、2-3树的插入原理、B树和B+树】
- 笔记10【61-63】【哈希表概述、散列函数的设计、散列冲突解决方案】
- 笔记11【64-67】【图结构概述、图遍历原理(BFS\DFS)、图遍历代码实现】
目 录
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();
}
}
嘿嘿嘿~