1. BST简介
本文部分内容参考自此处。
Binary Tree : A data structure in which we have nodes containing data and two references to other nodes, one on the left and one on the right.
Binary Tree consist of Nodes
Nodes are nothing but objects of a class and each node has data and a
link to the left node and right node. Usually we call the starting
node of a tree as root.
Left and right node of a Leaf node points to NULL so you will know that you have reached to the end of the tree.Binary Search Tree:
Often we call it as BST, is a type of Binary tree which has a special
property.Nodes smaller than root goes to the left of the root and Nodes greater
than root goes to the right of the root.
二叉搜索树:是一种特殊结构的二叉树,其左节点元素小于等于父节点,而右节点元素值大于父节点。
2. BST实现demo及相关说明
//BSTTree.java
package com.fqyuan.bst;
import org.junit.Test;
public class BSTTree {
private Node root;
public class Node {
private int data;
private Node lchild;
private Node rchild;
public Node(int item) {
data = item;
}
}
public BSTTree() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
public void insert(int item) {
if (root == null) {
Node newNode = new Node(item);
root = newNode;
return;
}
// First find the position to insert.
Node parent = root;
Node currrent = root;
while (currrent != null) {
parent = currrent;
if (item < currrent.data)
currrent = currrent.lchild;
else
currrent = currrent.rchild;
}
// 'current' is the node to insert, 'parent' is the parent of 'current'.
Node newNode = new Node(item);
if (item < parent.data)
parent.lchild = newNode;
else
parent.rchild = newNode;
}
// Choose the right max as the successor.
private Node getSuccessor(Node node) {
Node current = node.rchild;
Node successor = null;
// The node is to build a link with the successor.
Node grand = null;
while (current != null) {
grand = successor;
successor = current;
current = current.lchild;
}
// After the loop, parent is the successor, grand is successor's father.
if (successor != node.rchild) {
grand.lchild = successor.rchild;
successor.rchild = node.rchild;
}
return successor;
}
// 3 cases:
public boolean delete(int item) {
if (root == null)
return false;
// First of all, find the position to delete.
Node current = root;
Node parent = root;
boolean isLeftChild = false;
while (current.data != item) {
parent = current;
if (item < current.data) {
current = current.lchild;
} else
current = current.rchild;
if (current == null)
return false;
}
if (parent.lchild.data == item)
isLeftChild = true;
else
isLeftChild = false;
// Case 1: if the node to be deleted is a leaf.
if (current.lchild == null && current.rchild == null) {
if (current == root) {
root = null;
} else if (isLeftChild) {
parent.lchild = null;
} else {
parent.rchild = null;
}
}
// Case 2: if node to be deleted has a child.
else if (current.lchild == null) {
if (current == root) {
root = current.rchild;
} else if (isLeftChild) {
parent.lchild = current.rchild;
} else {
parent.rchild = current.rchild;
}
} else if (current.rchild == null) {
if (current == root) {
root = current.lchild;
} else if (isLeftChild) {
parent.lchild = current.lchild;
} else {
parent.rchild = current.lchild;
}
}
// Case 3: if node to be deleted has 2 children.
else {
// First find the successor: the leftMax or the rightMax node to
// replace.
Node successor = getSuccessor(current);
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.lchild = successor;
} else {
parent.rchild = successor;
}
successor.lchild = current.lchild;
}
return false;
}
public Node search(int item) {
Node current = root;
while (current.data != item) {
if (item < current.data)
current = current.lchild;
else if (item > current.data)
current = current.rchild;
if (current == null)
return null;
}
return current;
}
public void inOrder() {
inOrder(root);
}
private void inOrder(Node node) {
if (node == null)
return;
inOrder(node.lchild);
System.out.print(node.data + " ");
inOrder(node.rchild);
}
@Test
public void test() {
BST<Integer> bst = new BST<>();
int[] arr = { 1, 42, 29, 65, 22, 14, 6, 59, 71, 47, 20 };
for (int i = 0; i < arr.length; i++) {
bst.insert(arr[i]);
}
bst.inOrder();
if (bst.search(10))
System.out.println("\nFind 10");
else
System.out.println("\n10 not found.");
// Insert a new element: 18
bst.insert(18);
bst.inOrder();
// Delete an element:
System.out.println();
bst.delete(29);
bst.inOrder();
}
}
//Running result:
1 6 14 20 22 29 42 47 59 65 71
10 not found.
1 6 14 18 20 22 29 42 47 59 65 71
1 6 14 18 20 22 42 47 59 65 71