学习尚硅谷韩顺平老师的Java数据结构笔记,详情请移步网站
1、需求
给你一个数列 (7, 3, 10, 12, 5, 1, 9),要求能够高效的完成对数据的查询和添加。
2、解决方案分析
2.1 使用数组
数组未排序, 优点:直接在数组尾添加,速度快。 缺点:查找速度慢.
数组排序,优点:可以使用二分查找,查找速度快,缺点:为了保证数组有序,在添加新数据时,找到插入位置后,后面的数据需整体移动,速度慢。
2.2 使用链式存储-链表
不管链表是否有序,查找速度都慢,添加数据速度比数组快,不需要数据整体移动。
2.3 使用二叉排序树
3、介绍
3.1 二叉排序树:BST: (Binary Sort(Search) Tree), 对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。
3.2 特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点
3.3 比如针对前面的数据 (7, 3, 10, 12, 5, 1, 9) ,对应的二叉排序树为:
4、创建和遍历
一个数组创建成对应的二叉排序树,并使用中序遍历二叉排序树,比如: 数组为 Array(7, 3, 10, 12, 5, 1, 9) , 创建成对应的二叉排序树为 :
5、删除结点
二叉排序树的删除情况比较复杂,有下面三种情况需要考虑
(1) 删除叶子节点 (比如:2, 5, 9, 12)
(2) 删除只有一颗子树的节点 (比如:1)
(3) 删除有两颗子树的节点. (比如:7, 3,10 )
(4) 操作的思路分析
//对删除结点的各种情况的思路分析:
第一种情况:
删除叶子节点 (比如:2, 5, 9, 12)
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 确定 targetNode 是 parent 的左子结点 还是右子结点
(4) 根据前面的情况来对应删除左子结点 parent.left = null
右子结点 parent.right = null;
第二种情况: 删除只有一颗子树的节点 比如 1
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 确定 targetNode 的子结点是左子结点还是右子结点
(4) targetNode 是 parent 的左子结点还是右子结点
(5) 如果 targetNode 有左子结点
5. 1 如果 targetNode 是 parent 的左子结点
5.2 如果 targetNode 是 parent 的右子结点
parent.right = targetNode.left;
(6) 如果 targetNode 有右子结点
6.1 如果 targetNode 是 parent 的左子结点
parent.left = targetNode.right;
6.2 如果 targetNode 是 parent 的右子结点
parent.right = targetNode.right
情况三 : 删除有两颗子树的节点. (比如:7, 3,10 )
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 从 targetNode 的右子树找到最小的结点
(4) 用一个临时变量,将 最小结点的值保存 temp = 11
(5) 删除该最小结点
(6) targetNode.value = temp
package com.BinarySortTree;
/**
* 二叉排序树
*/
//定义结点
class Node{
int value;
Node leftNode;
Node rightNode;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
/**
* 添加结点
* @param node 待添加的结点
*/
public void add(Node node){
if(node == null){
return;
}
//判断传入结点的值和当前子树根节点的值的关系
if (node.value < this.value){
if (this.leftNode == null){//当前结点的左子树是否为空
this.leftNode = node;
}else {
this.leftNode.add(node);//递归处理
}
}else {
if (this.rightNode == null){//当前结点的右子树是否为空
this.rightNode = node;
}else {
this.rightNode.add(node);
}
}
}
/**
* 中序遍历
*/
public void infixOrder(){
if (this.leftNode != null){
this.leftNode.infixOrder();
}
System.out.println(this.value);
if (this.rightNode != null) {
this.rightNode.infixOrder();
}
}
/**
* 查找要删除的结点
* @param value 希望删除的值
* @return 找到则返回结点,找不到则返回null
*/
public Node search(int value){
if (value == this.value){//就是当前结点
return this;
}else if (value < this.value){//小于当前结点
//左子节点为空
if (this.leftNode == null){
return null;
}
return this.leftNode.search(value);
}else {//大于等于当前结点
//右子结点为空
if (this.rightNode == null){
return null;
}
return this.rightNode.search(value);
}
}
/**
* 找到删除结点的父节点
* @param value 要删除的结点值
* @return 找到则返回结点,找不到则返回null
*/
public Node searchParent(int value){
if ((this.leftNode != null && this.leftNode.value == value)//当前结点为待删除结点的父节点
|| (this.rightNode != null && this.rightNode.value == value)) {
return this;
}else {
//如果小于,往左找
if (value < this.value && this.leftNode != null){
return this.leftNode.searchParent(value);
}else if (value >= this.value && this.rightNode != null){//大于等于往右边找
return this.rightNode.searchParent(value);
}else {
return null;//没有找到返回空
}
}
}
}
//创建二叉排序树
class BinarySortTree{
private Node root;
/**
* 添加结点
* @param node 待添加结点
*/
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("空树");
}
}
/**
* 查找待删除结点
* @param value 待删除的值
* @return 找到则返回结点,找不到则返回null
*/
public Node search(int value){
if (root == null){
return null;
}else {
return root.search(value);
}
}
/**
* 查找待删除结点的父节点
* @param value 待删除的值
* @return 找到则返回结点,找不到则返回null
*/
public Node searchParent(int value){
if (root == null){
return null;
}else {
return root.searchParent(value);
}
}
/**
* 删除结点
* @param value 待删除结点的值
*/
public void delNode(int value){
if (root == null){//空树
return;
}else {
//1.先去找到要删除的结点
Node targetNode = search(value);
//没有找到
if (targetNode == null){
return;
}
//如果只有一个根结点,此时root=targetNode
if (root.leftNode == null && root.rightNode == null){
root = null;
return;
}
//2.找到父节点
Node parent = searchParent(value);
//3.如果删除的是叶子结点
if (targetNode.leftNode == null && targetNode.rightNode ==null) {
//判断是父节点的左子节点还是右子结点
if (parent.leftNode != null && parent.leftNode.value == value){//左节点
parent.leftNode = null;
}else if (parent.rightNode != null && parent.rightNode.value == value){//右节点
parent.rightNode = null;
}
}else if (targetNode.leftNode != null && targetNode.rightNode != null){//4.如果删除两棵子树
int rightTreeMin = delRightTreeMin(targetNode.rightNode);//递归找到右边子树的最小值
targetNode.value = rightTreeMin;//待删除的结点值赋值为找到的最小值
}else {//5.删除只有一棵子树
//如果要删除的结点有左子结点
if (targetNode.leftNode != null){
if (parent!=null) {
//如果 targetNode 是 parent 的左子结点
if (parent.leftNode.value == value) {
parent.leftNode = targetNode.leftNode;
} else {//如果 targetNode 是 parent 的右子结点
parent.rightNode = targetNode.leftNode;
}
}else {//parent为空,删除的是根节点,且只有一个左子树
root = targetNode.leftNode;
}
}else {//如果要删除的结点有右子结点
if (parent != null) {
//如果 targetNode 是 parent 的左子结点
if (parent.leftNode.value == value) {
parent.leftNode = targetNode.rightNode;
} else {//如果 targetNode 是 parent 的右子结点
parent.rightNode = targetNode.rightNode;
}
}else {//parent为空,删除的是根节点,且只有一个右子树
root = targetNode.rightNode;
}
}
}
}
}
/**
* 返回的 以 node 为根结点的二叉排序树的最小结点的值
* 删除 node 为根结点的二叉排序树的最小结点
* @param node 传入的结点(当做二叉排序树的根结点)
* @return 以 node 为根结点的二叉排序树的最小结点的值
*/
public int delRightTreeMin(Node node){
Node temp = node;
//循环的查找左子节点,直到最小值
while (temp.leftNode != null){
temp = temp.leftNode;
}
//退出循环,找到了最小值并删除
delNode(temp.value);
//返回最小值
return temp.value;
}
}
public class BinarySortTreeDemo {
public static void main(String[] args) {
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();
System.out.println("删除之后");
binarySortTree.delNode(2);
binarySortTree.delNode(5);
binarySortTree.delNode(9);
binarySortTree.delNode(12);
binarySortTree.delNode(7);
binarySortTree.delNode(3);
binarySortTree.delNode(1);
binarySortTree.delNode(10);
binarySortTree.infixOrder();
}
}