Java实现BST(BinarySortTree 或 BirnarySearchTree)
马上就期末开始了,我还在做没意义的事情,下面直接附上代码。
package Tree;
import java.util.ArrayList;
import java.util.List;
/**
* 二叉排序树(BST)
*
* @author xuning
* @date 2020/12/20
*/
public class BinarySortTree {
private Node root;
private List<Integer> elements = new ArrayList<>(5);
public BinarySortTree() {
}
/**
* 添加结点
*
* @param node
*/
public void add(Node node) {
if (this.root == null) {
this.root = node;
} else {
this.root.add(node);
}
}
public void add(int value) {
this.add(new Node(value));
}
/**
* 中序遍历
*/
public void inOrder() {
if (root == null) {
System.out.println("空树不能遍历");
} else {
root.inOrder();
}
}
public void uinOrder() {
if (root == null) {
System.out.println("空树不能遍历");
} else {
this.root.uinOrder();
}
}
public Node getRoot() {
return this.root;
}
public Node searchTarget(int value) {
if (this.root != null) {
return this.root.searchTargetNode(value);
} else {
return null;
}
}
public Node searchParent(int value) {
if (this.root == null) {
return null;
} else {
return this.root.searchParentNode(value);
}
}
private int delRightMin(Node right) {
Node target = right;
while (target.left != null) {
target = target.left;
}
delNode(target.val);
return target.val;
}
public void delNode(int value) {
if (this.root == null) {
return;
} else {
Node targetNode = this.searchTarget(value);
if (targetNode == null) {
System.gc();
return;
}
// 如果目标结点没有父节点,那么就说明删除的是根结点。
if (this.root.left == null && this.root.right == null && this.root == targetNode) {
this.root = null;
System.gc();
return;
}
// 找到父亲结点
Node parentNode = this.searchParent(value);
// 叶子结点的情况
if (targetNode.left == null && targetNode.right == null) {
if (parentNode.left != null && targetNode == parentNode.left) {
parentNode.left = null;
}
if (parentNode.right != null && targetNode == parentNode.right) {
parentNode.right = null;
}
System.gc();
} else if (targetNode.left != null && targetNode.right != null) {
/*
* 中间结点的情况
* */
int minvalue = delRightMin(targetNode.right);
targetNode.val = minvalue;
} else {
/*
* 只有一个子结点的情况
* */
// 如果目标结点有左子结点
if (targetNode.left != null) {
if (parentNode != null) {
if (parentNode.left == targetNode) {
parentNode.left = targetNode.left;
}
if (parentNode.right == targetNode) {
parentNode.right = targetNode.left;
}
} else {
this.root = targetNode.left;
}
System.gc();
}
// 如果目标结点有右子结点
if (targetNode.right != null) {
if (parentNode != null) {
if (parentNode.left == targetNode) {
parentNode.left = targetNode.right;
}
if (parentNode.right == targetNode) {
parentNode.right = targetNode.right;
}
} else {
this.root = targetNode.right;
}
}
}
}
System.gc();
}
}
class Node {
public int val;
public Node left;
public Node right;
public Node() {
}
public Node(int value) {
this.val = value;
}
/**
* 查找目标结点
*
* @param value 目标节点值
* @return targetNode 目标结点
*/
public Node searchTargetNode(int value) {
// 如果查找的结点值等于本结点的值,那么返回本结点
if (value == this.val) {
return this;
} else if (value < this.val) {
/*
* 如果查找的值小于当前结点的值,
* 那么先检查左子树是否为空,
* 如果不为空那么左递归,反之返回null
* */
if (this.left != null) {
return this.left.searchTargetNode(value);
} else {
return null;
}
} else {
/*
* 如果查找的值大于等于当前结点值,
* 那么先检查右子树是否为空,
* 如果不为空那么右递归,反之返回null;
* */
if (this.right != null) {
return this.right.searchTargetNode(value);
} else {
return null;
}
}
}
/**
* 查找目标结点的父结点
*
* @param value 目标结点值
* @return ParentNode 目标结点的父节点
*/
public Node searchParentNode(int value) {
if ((this.left != null && this.left.val == value) || (this.right != null && this.right.val == value)) {
return this;
} else {
if (value < this.val && this.left != null) {
return this.left.searchParentNode(value);
} else if (value >= this.val && this.right != null) {
return this.right.searchParentNode(value);
} else {
return null;
}
}
}
/**
* 建立二叉排序树
*
* @param node
*/
public void add(Node node) {
if (node == null) {
return;
}
if (node.val < this.val) {
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 inOrder() {
if (this.left != null) {
this.left.inOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.inOrder();
}
}
public void uinOrder() {
if (this.right != null) {
this.right.uinOrder();
}
System.out.println(this);
if (this.left != null) {
this.left.uinOrder();
}
}
public int getVal() {
return this.val;
}
@Override
public String toString() {
return "Node{" +
"val=" + val +
'}';
}
}
class Test {
public static void main(String[] args) {
BinarySortTree binarySortTree = new BinarySortTree();
int[] data = {100, 5, 1000, 1, 2, 20, 3, 2, 80, 66, 1000, 30, 5, 0};
for (int i = 0; i < data.length; i++) {
binarySortTree.add(new Node(data[i]));
}
/* for(int i=0; i<data.length; i++){
binarySortTree.inOrder();
binarySortTree.delNode(data[i]);
System.out.println();
}*/
binarySortTree.uinOrder();
/*int N = 31381;
for (int i = 0; i < N; i++) {
binarySortTree.add(i);
}
for (int i = 0; i < N; i++) {
binarySortTree.delNode(i);
}*/
binarySortTree.delNode(1);
System.out.println();
binarySortTree.inOrder();
}
}