package demo;
import java.util.LinkedList;
/**
* 二分查找樹
*
* @author Administrator
*
* @param <K>
* @param <V>
*/
public class BST<K extends Comparable<K>, V> {
private final static Node EmptyNode = null;
private Node head;
private int count;
private static class Node<K extends Comparable<K>, V> {
K key;
V value;
Node<K, V> left;
Node<K, V> right;
private Node(K key, V val) {
this.key = key;
this.value = val;
this.left = EmptyNode;
this.right = EmptyNode;
}
private Node(Node<K,V> node) {
this.key = node.key;
this.value = node.value;
this.left = node.left;
this.right = node.right;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{ ").append(key).append(":").append(value).append(" }");
return sb.toString();
}
}
public BST() {
head = EmptyNode;
count = 0;
}
public int size() {
return count;
}
public boolean isEmpty() {
return count == 0;
}
public void insert(K key, V val) {
checkArg(key, "key is null");
head = insert(head, key, val);
}
private Node<K, V> insert(Node<K, V> root, K key, V val) {
if (root == EmptyNode) {
count++;
return new Node<K, V>(key, val);
}
if (root.key.compareTo(key) > 0)
root.left = insert(root.left, key, val);
else if (root.key.compareTo(key) < 0)
root.right = insert(root.right, key, val);
else
root.value = val;
return root;
}
public V search(K key) {
return (V) search(head, key);
}
private V search(Node<K, V> root, K key) {
checkArg(key, "key is null");
if (root == EmptyNode)
return null;
if (root.key.compareTo(key) == 0)
return root.value;
else if (root.key.compareTo(key) > 0)
return (V) search(root.left, key);
else
return (V) search(root.right, key);
}
public void preOrder() {
preOrder(head);
}
private void preOrder(Node root) {
if (root == EmptyNode)
return;
System.out.print(root);
preOrder(root.left);
preOrder(root.right);
}
public void midOrder() {
midOrder(head);
}
private void midOrder(Node root) {
if (root == EmptyNode)
return;
midOrder(root.left);
System.out.print(root);
midOrder(root.right);
}
public void postOrder() {
postOrder(head);
}
private void postOrder(Node root) {
if (root == EmptyNode)
return;
postOrder(root.left);
postOrder(root.right);
System.out.println(root);
}
private void checkArg(Object key, String thrStr) {
if (key == null)
throw new IllegalArgumentException(thrStr);
}
public void levelOrder() {
if (head == EmptyNode)
return;
LinkedList<Node> ll = new LinkedList();
ll.addLast(head);
while (!ll.isEmpty()) {
Node node = ll.removeFirst();
System.out.println(node);
if (node.left != EmptyNode)
ll.addLast(node.left);
if (node.right != EmptyNode)
ll.addLast(node.right);
}
}
public K getMinNode() {
checkArg(head, "this tree is empty!!");
return (K) getMinNode(head).key;
}
private Node getMinNode(Node root) {
if (root.left == EmptyNode)
return root;
return getMinNode(root.left);
}
public K getMaxNode() {
checkArg(head, "this tree is empty!!");
return (K) getMaxNode(head).key;
}
private Node<K, V> getMaxNode(Node<K, V> root) {
if (root.right == EmptyNode)
return root;
return getMaxNode(root.right);
}
public void removeMin() {
checkArg(head, "the tree is empty");
head = removeMin(head);
}
private Node removeMin(Node root) {
if (root.left == EmptyNode) {
Node rightNode = root.right;
root = EmptyNode;
count--;
return rightNode;
}
root.left = removeMin(root.left);
return root;
}
public void removeMax() {
checkArg(head, "the tree is empty");
head = removeMax(head);
}
private Node removeMax(Node root) {
if (root.right == EmptyNode) {
Node leftNode = root.left;
root = null;
count--;
return leftNode;
}
root.right = removeMax(root.right);
return root;
}
public void remove(K key) {
head = remove(head, key);
}
private Node remove(Node root, K key) {
if(root ==EmptyNode) return null;
if (root.key.compareTo(key) < 0) {//
root.right = remove(root.right, key);
return root;
} else if (root.key.compareTo(key) > 0) {
root.left = remove(root.left, key);
return root;
} else {// equal
if(root.left ==EmptyNode) {//沒有左節(jié)點(diǎn)
Node rightNode = root.right;
root=EmptyNode;
count--;
return rightNode;
}
if(root.right == EmptyNode) {//沒有右節(jié)點(diǎn)
Node leftNode = root.right;
root=EmptyNode;
count--;
return leftNode;
}
//左右節(jié)點(diǎn)都存在
Node min = getMinNode(root.right);
Node s = new Node<K, V>(min);
s.right = removeMin(root.right);
s.left = root.left;
return s;
}
}
public static void main(String[] args) {
BST<Integer, Object> bst = new BST();
bst.insert(1, "hello");
bst.insert(10, "hello");
bst.insert(9, "hello");
bst.insert(20, "hello");
bst.insert(11, "hello");
bst.insert(14, "hello");
bst.insert(90, "hello");
bst.insert(25, "hello");
bst.midOrder();
bst.remove(10);
System.out.println("");
bst.midOrder();
}
}
簡單的二分搜索樹-BST
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 二叉樹 之前的一篇關(guān)于數(shù)組的鏈表中的文章中,我們說了鏈表是存儲(chǔ)在內(nèi)存中是以一種邏輯上的鏈?zhǔn)浇Y(jié)構(gòu),每個(gè)節(jié)點(diǎn)不僅存儲(chǔ)元...
- 這里引申出了一個(gè)不同的問題: 為什么對值二分,而不是對索引進(jìn)行二分 二分查找可以根據(jù)索引二分,也可以根據(jù)數(shù)值二分,...
- 二叉搜索樹 定義 一種在有序數(shù)組中查找某一特定元素的搜索算法,稱之為二叉查找或者二分查找法。 在樹結(jié)構(gòu)中類似,從中...
- 一.二叉樹 和鏈表一樣,動(dòng)態(tài)數(shù)據(jù)結(jié)構(gòu) class Node{E e;Node left;←左孩子Node righ...
- 目錄 Convert/Create99 Recover Binary Search Tree108 Convert...