import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BST<Key extends Comparable<Key>, Value> {
private Node root;
public class Node {
private Key key;
private Value val;
private Node parent, left, right;
private int height;
private Node(Key key, Value val, int height, Node parent) {
this.key = key;
this.val = val;
this.height = height;
this.parent = parent;
this.left = this.right = null;
}
}
public int size(Node x) {
return x.height;
}
private void UpdateHeight(Node x) {
while (true) {
if (x.left == null && x.right == null) {
} else if (x.left == null)
x.height = x.right.height + 1;
else if (x.right == null)
x.height = x.left.height + 1;
else
x.height = (x.left.height == x.right.height ? x.left.height : x.left.height > x.right.height ? x.left.height : x.right.height) + 1;
if (x.parent == null)
break;
else
x = x.parent;
}
root = x;
}
public int Search(Key key) {
return SearchIn(root, key);
}
private int SearchIn(Node t, Key key) {
if (t == null)
return 0;
else if (key.compareTo(t.key) == 0)
return t.height;
return SearchIn(key.compareTo(t.key) < 0 ? t.left : t.right, key);
}
public void Insert(Key key, Value val) {
if (root == null)
root = new Node(key, val, 0, null);
Node temp = root;
while (true) {
int cmp = key.compareTo(temp.key);
if (cmp < 0) {
if (temp.left == null) {
temp.left = new Node(key, val, 0, temp);
UpdateHeight(temp);
return;
} else
temp = temp.left;
} else if (cmp > 0) {
if (temp.right == null) {
temp.right = new Node(key, val, 0, temp);
UpdateHeight(temp);
return;
} else
temp = temp.right;
} else {
temp.val = val;
return;
}
}
}
public Node Remove(Key key) {
Node temp_root = root;
if (root == null || key == null)
return null;
while (temp_root != null) {
int cmp = key.compareTo(temp_root.key);
if (cmp > 0)
temp_root = temp_root.right;
else if (cmp < 0)
temp_root = temp_root.left;
else {
if (temp_root.left == null || temp_root.right == null) {
if (temp_root.left != null) {
temp_root.key = temp_root.left.key;
temp_root.val = temp_root.left.val;
temp_root.left = temp_root.left.left;
temp_root.right = temp_root.left.right;
} else if (temp_root.right != null) {
temp_root.key = temp_root.right.key;
temp_root.val = temp_root.right.val;
temp_root.left = temp_root.right.left;
temp_root.right = temp_root.right.right;
} else {
temp_root = temp_root.parent;
if (temp_root.left != null && temp_root.left.key == key)
temp_root.left = null;
else
temp_root.right = null;
}
UpdateHeight(temp_root);
} else {
Node t = temp_root.right;
while (t.left != null)
t = t.left;
temp_root.key = t.key;
temp_root.val = t.val;
if (t.right != null) {
t.key = t.right.key;
t.val = t.right.val;
t.left = t.right.left;
t.right = t.right.right;
} else {
t.parent.left = null;
}
UpdateHeight(t);
}
return root;
}
}
return root;
}
public Queue<Key> Inorder() { //先序遍历
Queue<Key> keys = new LinkedList<>();
Stack<Node> stack = new Stack<Node>();
stack.push(root);
Node node = root;
while (true) {
if (node != null) {
stack.push(node);
node = node.left;
} else if (!stack.isEmpty()) {
node = stack.pop();
keys.offer(node.key);
node = node.right;
} else
break;
}
return keys;
}
public Queue<Key> LevelOrder() { //层次遍历
Queue<Key> keys = new LinkedList<Key>();
Queue<Node> nodes = new LinkedList<Node>();
Node node = root;
nodes.offer(node);
while (!nodes.isEmpty()) {
Node x = ((LinkedList<Node>) nodes).pop();
keys.offer(x.key);
if (x.left != null)
nodes.offer(x.left);
if (x.right != null)
nodes.offer(x.right);
}
return keys;
}
public static void main(String args[]) {
BST<Integer, Integer> tt = new BST<Integer, Integer>();
for (int i = 0; i < 10; i++) {
int x = (int) (Math.random() * 50);
tt.Insert(x, i);
System.out.println(x);
}
for (Integer i : tt.LevelOrder())
System.out.println(i + " : " + tt.Search(i));
tt.Remove(30);
System.out.println("---------------------");
for (Integer i : tt.LevelOrder())
System.out.println(i + " : " + tt.Search(i));
System.out.println("断点设置处");
}
}