二叉排序樹的創建和刪除
最近學習了二叉樹的創建和刪除功能,本文代碼主要實現以下功能:
- 給定一個數列,創建二叉排序樹(BST)
- 遍歷二叉排序樹(中序遍歷)
- 刪除二叉排序樹的節點
其中,二叉排序樹的刪除節點步驟較爲繁瑣,思路總結如下:
一.如果要刪除的節點是葉子節點
- 1.找到要刪除節點:targetNode;
- 2.找到要刪除節點的父節點:parent;
- 3.判斷要刪除的節點是其父節點的左子節點還是右子節點;
- 4.parent.left=null或者parent.right=null;
二.如果要刪除的節點只有一棵子樹
- 1.找到要刪除節點:targetNode;
- 2.找到要刪除節點的父節點:parent;
- 3.如果要刪除的節點是其父節點的左子節點:
3.1 要刪除節點的子節點是左子節點:parent.left=targetNode.left;
3.2 要刪除節點的子節點是右子節點:parent.left=targetNode.right; - 4.如果要刪除的節點是其父節點的右子節點:
4.1 要刪除節點的子節點是左子節點:parent.right=targetNode.left;
4.2 要刪除節點的子節點是右子節點:parent.right=targetNode.right;
三.如果要刪除的節點有兩棵子樹
- 第一種方案:找到要刪除節點左子樹中最大的值;
- 第二種方案:找到要刪除節點右子樹中最小的值;
以第二種方案爲例:
- 1.找到要刪除節點:targetNode;
- 2.找到要刪除節點的父節點:parent;
- 3.找到要刪除節點右子樹中最小值對應的節點,將其保存:temp
- 4.刪除該最小節點
- 5.將要刪除的節點替換成該最小節點targetNode.value=temp.value
如有錯誤之處,還望指出,定會及時改正
package com.binarySortTree;
/*
* 1.給定一個數列,創建二叉排序樹(BST)
* 2.遍歷二叉排序樹(中序遍歷)
* 3.刪除二叉排序樹的節點
*/
public class BinarySortTreeDemo2 {
public static void main(String[] args) {
int[] arr = { 8, 5, 10, 6, 3, 2, 4, 7, 9, 1 };
BinarySortTree2 binarySortTree2 = new BinarySortTree2();
// 創建二叉排序樹
for (int i = 0; i < arr.length; i++) {
binarySortTree2.add(new Node2(arr[i]));
}
// 中序遍歷
System.out.println("中序遍歷二叉排序樹");
binarySortTree2.infixList();
//測試刪除節點
binarySortTree2.delNode2(1);
/*binarySortTree2.delNode2(2);
binarySortTree2.delNode2(3);
binarySortTree2.delNode2(9);
binarySortTree2.delNode2(7);
binarySortTree2.delNode2(8);
binarySortTree2.delNode2(5);
binarySortTree2.delNode2(10);
binarySortTree2.delNode2(4);
binarySortTree2.delNode2(6);*/
System.out.println("刪除節點後");
binarySortTree2.infixList();
}
}
//創建二叉排序樹類
class BinarySortTree2 {
Node2 root;
/**
* 創建二叉排序樹(添加節點)
*
* @param node
*/
public void add(Node2 node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
/**
* 中序遍歷二叉樹
*/
public void infixList() {
if (root == null) {
System.out.println("空樹無法遍歷!");
} else {
root.infixList();
}
}
// 刪除節點:三種情況
/*
* 一.如果要刪除的節點是葉子節點
* 1.找到要刪除節點:targetNode;
* 2.找到要刪除節點的父節點:parent;
* 3.判斷要刪除的節點是其父節點的左子節點還是右子節點;
* 4.parent.left=null或者parent.right=null;
* 二.如果要刪除的節點只有一棵子樹
* 1.找到要刪除節點:targetNode;
* 2.找到要刪除節點的父節點:parent;
* 3.如果要刪除的節點是其父節點的左子節點:
* 3.1 要刪除節點的子節點是左子節點:parent.left=targetNode.left;
* 3.2 要刪除節點的子節點是右子節點:parent.left=targetNode.right;
* 4.如果要刪除的節點是其父節點的右子節點:
* 4.1 要刪除節點的子節點是左子節點:parent.right=targetNode.left;
* 4.2 要刪除節點的子節點是右子節點:parent.right=targetNode.right;
* 三.如果要刪除的節點有兩棵子樹
* 第一種方案:找到要刪除節點左子樹中最大的值;
* 第二種方案:找到要刪除節點右子樹中最小的值;
* 以第二種方案爲例:
* 1.找到要刪除節點:targetNode;
* 2.找到要刪除節點的父節點:parent;
* 3.找到要刪除節點右子樹中最小值對應的節點,將其保存:temp
* 4.刪除該最小節點
* 5.將要刪除的節點替換成該最小節點targetNode.value=temp.value
*/
/**
* 刪除節點
*
* @param value
*/
public void delNode2(int value) {
if (root == null) {
System.out.println("二叉排序樹爲空!");
return;
}
// 首先要找到要刪除的節點
Node2 targetNode = root.search(value);
if (targetNode == null) {
System.out.println("沒有找到要刪除的節點!");
return;
}
// 如果要刪除的節點就是根節點,並且該二叉樹只有一個節點,直接將根節點置爲空即可!
if (targetNode.value == root.value && root.left == null && root.right == null) {
root = null;
return;
}
// 找到要刪除節點的父節點
Node2 parent = root.searchParent(value);
// 一.如果要刪除的節點是葉子節點
if (targetNode.left == null && targetNode.right == null) {
// 1.如果要刪除節點是其父節點的左子節點
if (parent.left == targetNode) {
parent.left = null;
} else {// 2.如果要刪除節點是其父節點的右子節點
parent.right = null;
}
return;
}
// 二.如果要刪除的節點只有一棵子樹--左子樹
if (targetNode.left != null && targetNode.right == null) {
// !!!這裏需要考慮一種特殊情況:如果要刪除的節點是根節點,並且根節點下還有個左子節點
if (parent != null) {
// 1.如果要刪除的節點是其父節點的左子節點
if (parent.left == targetNode) {
parent.left = targetNode.left;
}
// 2.如果要刪除的節點是其父節點的右子節點
if (parent.right == targetNode) {
parent.right = targetNode.left;
}
} else {// 如果要刪除的節點是根節點,並且根節點下還有個左子節點
root = targetNode.left;
}
return;
}
// 二.如果要刪除的節點只有一棵子樹--右子樹
if (targetNode.left == null && targetNode.right != null) {
// !!!這裏需要考慮一種特殊情況:如果要刪除的節點是根節點,並且根節點下還有個右子節點
if (parent != null) {
// 1.如果要刪除的節點是其父節點的左子節點
if (parent.left == targetNode) {
parent.left = targetNode.right;
}
// 2.如果要刪除的節點是其父節點的右子節點
if (parent.right == targetNode) {
parent.right = targetNode.right;
}
} else {// 如果要刪除的節點是根節點,並且根節點下還有個右子節點
root = targetNode.right;
}
return;
}
// 三.如果要刪除的節點有兩棵子樹(第二種方案:找要刪除節點的右子樹中最小值的節點)
if (targetNode.left != null && targetNode.right != null) {
// 1.在要刪除節點的右子樹中找最小值的節點
int min = searchNodeTreeMin(targetNode.right);
// 2.將要刪除節點替換成最小值的節點
targetNode.value = min;
return;
}
}
/**
* 根據傳入的node值,將node認定爲根節點,找其子樹中最小值並將其刪除
*
* @param node
* @return
*/
public int searchNodeTreeMin(Node2 node) {
Node2 temp = node;
// 找最左邊的葉子節點
while (temp.left != null) {
temp = temp.left;
}
// 將最小值的節點刪除
delNode2(temp.value);
// 將最小值返回
return temp.value;
}
}
//創建節點類
class Node2 {
int value;
Node2 left;
Node2 right;
public Node2(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node2 [value=" + value + "]";
}
/**
* 添加節點
*
* @param node
*/
public void add(Node2 node) {
if (node == null) {
return;
}
// 1.要添加的節點的值小於當前節點的值
if (node.value < this.value) {
// 如果當前節點的的左子節點爲空,就直接掛上
if (this.left == null) {
this.left = node;
} else {// 如果不爲空,左遞歸
this.left.add(node);
}
} else {// 2.要添加的節點的值大於或等於當前節點的值
// 如果當前節點的右子節點爲空,就直接掛上
if (this.right == null) {
this.right = node;
} else {// 如果不爲空,右遞歸
this.right.add(node);
}
}
}
/**
* 中序遍歷
*/
public void infixList() {
if (this.left != null) {
this.left.infixList();
}
System.out.println(this);
if (this.right != null) {
this.right.infixList();
}
}
/*
* 查找要刪除的節點
*/
public Node2 search(int value) {
if (this.value == value) {// 當前節點就是要刪除的節點
return this;
} else if (value < this.value) {// 如果要刪除節點的值小於當前節點的值
// 左遞歸查找
if (this.left == null) {
return null;
} else {
return this.left.search(value);
}
} else {// 如果要刪除節點的值大於或等於當前節點的值
if (this.right == null) {
return null;
} else {
return this.right.search(value);
}
}
}
/*
* 查找要刪除節點的父節點
*/
public Node2 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 {// 如果都沒找到,那就說明要刪除的節點沒有父節點,返回null
return null;
}
}
}
}