10.4-二叉排序树
- 导言:针对一个数列{7,3,10,12,5,1,9},要求可以高效的完成数据的查询和添加
- 数组
- 无序数组:查找速度慢,但是添加的时候可以直接添加到数组尾部,快速
- 有序:可以使用二分查找,查找速度快,但是添加的时候为了保证数组有序,可能要移动大量元素,速度慢
- 链表:不论有序无序,查找速度都不快,但是添加速度快
- 二叉排序树:既可以保证数据的检索速度,也可以保证数据插入删除修改的速度
- 数组
- 二叉排序树介绍:
- 对于二叉排序树的如何一个非叶子结点,要求左子树的值比当前结点的值小,右子节点的值比当前结点的值打,如果有相同的值,可以将该结点放在左子节点或者右子节点
- 二叉排序树的创建和遍历
package com.hejiale.dataStructures.tree.binarySortTree;
public 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();
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
package com.hejiale.dataStructures.tree.binarySortTree;
public class BinarySortTree {
private Node root;
//添加结点的方法
public void add(Node node){
if (root==null){
root = node;
}else{
root.add(node);
}
}
//中序遍历
public void infixOrder(){
if (root==null){
System.out.println("树为空,无法遍历");
}else {
root.infixOrder();
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NAxrPy6E-1626076632193)(…/images/d9cc365bbee9d72f1145d1849918dca3edb4e783c57924b25a0efa39708920d8.png)]
- 二叉排序树-删除结点
- 删除的结点没有子树(叶子结点)
(1) 需要先找到要删除的结点targetNode (2) 找到targetNode的父节点 parent (3) 确定targetNode是parent的左子节点还是右子节点 (4) 根据前面的情况来进行删除操作 如果是左子节点--> parent.left = null 如果是右子节点--> parent.right = null
- 删除的结点有一个子树
(1) 先找到要删除的结点targetNode (2) 找到targetNode的父结点parent (3) 确定targetNode的子节点是左子节点还是右子节点 (4) 确定targetNode是parent的左子节点还是右子节点 targetNode的子节点是左子节点 targetNode是parent的左子节点 parent.left = targetNode.left targetNode是parent的右子节点 parent.left = targetNode.right targetNode的子节点是右子节点 targetNode是parent的左子节点 parent.right = targetNode.left targetNode是parent的右子节点 parent.right = targetNode.right 注意:我们需要考虑到一个特殊情况,就是树中只有两个结点,那么这个时候我们删除根节点的时候,是没有parent结点的,所以我们需要对parent进行是否为空的判断,如果parent为空,所以此时树只有两个结点,那么我们应该采用特殊操作
- 删除的结点有两个子树
(1) 先找到要删除的结点targetNode (2) 找到targetNode的父结点parent (3) 从targetNode的右子树,找到最小的结点||左子树最大结点也可以 (4) 用一个临时变量temp,将最小结点的值保存下来 (5) 删掉该最小结点(删除的时候依然要按照二叉排序树的方式删除) (6) target.value = temp
/**
* 查找要删除的结点
*
* @param value 希望删除的结点的value值
* @return 如果找到, 就返回该结点, 否则返回null
*/
public Node search(int value) {
if (value == this.value) {
//找到,就是该结点
return this;
} else if (value < this.value) {
//要查找的结点,应该在当前结点的左子树上,但是要先判断该结点是否右左子节点
if (this.left != null) {
return this.left.search(value);
} else {
return null;
}
} else {
if (this.right != null) {
return this.right.search(value);
} else {
return null;
}
}
}
/**
* 查找要删除结点的父结点
*
* @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 (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 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 int delRightTreeMin(Node node) {
Node target = node;
//循环查找左结点
while (target.left != null) {
target = target.left;
}//循环结束,target指向最小结点
int min = target.value;
deleteNode(min);
return min;
}
//删除结点
public void deleteNode(int value) {
if (root == null) {
return;
} else {
//1. 先找到要删除的结点
Node targetNode = search(value);
if (targetNode == null) {
return;
}
if (root.left == null && root.right == null) {
//说明树只有一个结点
root = null;
return;
}
//2. 找到要删除结点的父结点
Node parent = searchParent(value);
if (targetNode.left == null && targetNode.right == null) {
//如果要删除的结点是叶子结点
//判断targetNode是parent的左子节点还是右子节点
if (targetNode.value < parent.value) {
parent.left = null;
} else {
parent.right = null;
}
} else if ((targetNode.left != null && targetNode.right == null) ||
(targetNode.left == null && targetNode.right != null)) {
//如果要删除的结点,有一个子树
if (targetNode.left != null) {
//删除的结点有左子节点
if (parent!=null){
if (targetNode.value < parent.value) {
//待删除结点是父节点的左子节点
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
}else {
root = targetNode.left;
}
} else {
//删除的结点有右子节点
if (parent !=null){
if (targetNode.value < parent.value) {
//待删除结点是父节点的左子节点
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
}else {
root = targetNode.right;
}
}
} else {
//如果要删除的结点,有两个子树
int minValue = delRightTreeMin(targetNode.right);
targetNode.value = minValue;
}
}
}