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é)點
Node rightNode = root.right;
root=EmptyNode;
count--;
return rightNode;
}
if(root.right == EmptyNode) {//沒有右節(jié)點
Node leftNode = root.right;
root=EmptyNode;
count--;
return leftNode;
}
//左右節(jié)點都存在
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)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來这嚣,“玉大人鸥昏,你說我怎么就攤上這事〗阒悖” “怎么了吏垮?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長罐旗。 經(jīng)常有香客問我膳汪,道長,這世上最難降的妖魔是什么九秀? 我笑而不...
- 正文 為了忘掉前任遗嗽,我火速辦了婚禮,結(jié)果婚禮上鼓蜒,老公的妹妹穿的比我還像新娘痹换。我一直安慰自己,他們只是感情好都弹,可當(dāng)我...
- 文/花漫 我一把揭開白布娇豫。 她就那樣靜靜地躺著,像睡著了一般缔杉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上搁料,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼庐杨!你這毒婦竟也來了选调?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布欺冀,位于F島的核電站树绩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏隐轩。R本人自食惡果不足惜饺饭,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望职车。 院中可真熱鬧瘫俊,春花似錦、人聲如沸悴灵。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽积瞒。三九已至川尖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間茫孔,已是汗流浹背叮喳。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 二叉樹 之前的一篇關(guān)于數(shù)組的鏈表中的文章中赞弥,我們說了鏈表是存儲在內(nèi)存中是以一種邏輯上的鏈?zhǔn)浇Y(jié)構(gòu)毅整,每個節(jié)點不僅存儲元...
- 這里引申出了一個不同的問題: 為什么對值二分,而不是對索引進(jìn)行二分 二分查找可以根據(jù)索引二分绽左,也可以根據(jù)數(shù)值二分毛嫉,...
- 二叉搜索樹 定義 一種在有序數(shù)組中查找某一特定元素的搜索算法承粤,稱之為二叉查找或者二分查找法暴区。 在樹結(jié)構(gòu)中類似,從中...
- 一.二叉樹 和鏈表一樣辛臊,動態(tài)數(shù)據(jù)結(jié)構(gòu) class Node{E e;Node left;←左孩子Node righ...
- 目錄 Convert/Create99 Recover Binary Search Tree108 Convert...