简书 賈小強
转载请注明原创出处,谢谢!
package com.lab1.test3;
import com.lab1.test1.LinkedQueue;
import com.lab1.test1.LinkedStack;
public class BST<Key extends Comparable<Key>, Value> {
private Node root;
private class Node {
Key key;
Value value;
Node left, right;
int size;
public Node(Key key, Value value, int size) {
this.key = key;
this.value = value;
this.size = size;
}
}
public Value get(Key key) {
return get(root, key);
}
private Value get(Node x, Key key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp < 0) {
return get(x.left, key);
} else if (cmp > 0) {
return get(x.right, key);
} else {
return x.value;
}
}
public Iterable<Key> keys() {
LinkedQueue<Key> queue = new LinkedQueue<>();
LinkedStack<Node> stack = new LinkedStack<>();
Node x = root;
while (x != null || !stack.isEmpty()) {
if (x != null) {
stack.push(x);
x = x.left;
} else {
x = stack.pop();
queue.enqueue(x.key);
x = x.right;
}
}
return queue;
}
public void delete(Key key) {
root = delete(root, key);
}
private Node delete(Node x, Key key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp < 0) {
x.left = delete(x.left, key);
} else if (cmp > 0) {
x.right = delete(x.right, key);
} else {
if (x.left == null) {
return x.right;
}
if (x.right == null) {
return x.left;
}
Node t = x;
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.size = size(x.left) + size(x.right) + 1;
return x;
}
public boolean contain(Key key) {
return get(key) != null;
}
public boolean isEmpty() {
return size() == 0;
}
public int size() {
return size(root);
}
public void deleteMin() {
root = deleteMin(root);
}
private Node deleteMin(Node x) {
if (x.left == null) {
return x.right;
}
x.left = deleteMin(x.left);
x.size = size(x.left) + size(x.right) + 1;
return x;
}
public Key min() {
return min(root).key;
}
private Node min(Node x) {
if (x.left == null) {
return x;
}
return min(x.left);
}
public int height() {
return height(root);
}
private int height(Node x) {
if (x == null) {
return -1;
}
return Math.max(height(x.left), height(x.right)) + 1;
}
public void put(Key key, Value value) {
root = put(root, key, value);
}
private Node put(Node x, Key key, Value value) {
if (x == null) {
return new Node(key, value, 1);
}
int cmp = key.compareTo(x.key);
if (cmp < 0) {
x.left = put(x.left, key, value);
} else if (cmp > 0) {
x.right = put(x.right, key, value);
} else {
x.value = value;
}
x.size = size(x.left) + size(x.right) + 1;
return x;
}
public int size(Node x) {
if (x == null) {
return 0;
}
return x.size;
}
public Iterable<Key> levelOrder() {
LinkedQueue<Key> keys = new LinkedQueue<>();
LinkedQueue<Node> queue = new LinkedQueue<>();
queue.enqueue(root);
while (!queue.isEmpty()) {
Node x = queue.dequeue();
if (x == null) {
continue;
}
keys.enqueue(x.key);
queue.enqueue(x.left);
queue.enqueue(x.right);
}
return keys();
}
private Iterable<Key> inOrder() {
LinkedQueue<Key> queue = new LinkedQueue<>();
root = inOrder(root, queue);
return queue;
}
private Node inOrder(Node x, LinkedQueue<Key> queue) {
if (x == null) {
return null;
}
inOrder(x.left, queue);
queue.enqueue(x.key);
inOrder(x.right, queue);
return x;
}
public static void main(String[] args) {
BST<String, Integer> st = new BST<>();
String test = "S E A R C H E X A M P L E";
String[] keys = test.split(" ");
for (int i = 0; i < keys.length; i++) {
st.put(keys[i], i);
}
System.out.println("height : " + st.height());
System.out.println("min : " + st.min());
st.deleteMin();
System.out.println("min : " + st.min());
System.out.println("size : " + st.size());
System.out.println("isEmpty : " + st.isEmpty());
System.out.println("contains : " + st.contain("S"));
st.delete("S");
System.out.println("contains : " + st.contain("S"));
for (String key : st.keys()) {
System.out.println(key + " " + st.get(key));
}
for (String key : st.inOrder()) {
System.out.print(key + " ");
}
System.out.println();
for (String key : st.levelOrder()) {
System.out.print(key + " ");
}
}
}
输出
height : 5
min : A
min : C
size : 9
isEmpty : false
contains : true
contains : false
C 4
E 12
H 5
L 11
M 9
P 10
R 3
X 7
C E H L M P R X
C E H L M P R X
Happy learning !!