简书 賈小強
转载请注明原创出处,谢谢!
package com.lab1.test6;
import com.lab1.test1.LinkedQueue;
import com.lab1.test1.LinkedStack;
public class RedBlackBST<Key extends Comparable<Key>, Value> {
private static final boolean RED = true;
private static final boolean BLACK = false;
private Node root;
private class Node {
Key key;
Value value;
Node left, right;
boolean color;
int size;
public Node(Key key, Value value, boolean color, int size) {
this.key = key;
this.value = value;
this.color = color;
this.size = size;
}
}
private 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;
}
}
private 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;
}
private 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;
}
private boolean contains(Key key) {
return get(key) != null;
}
private int size() {
return size(root);
}
private boolean isEmpty() {
return size() == 0;
}
private void put(Key key, Value value) {
root = put(root, key, value);
root.color = BLACK;
}
private Node put(Node h, Key key, Value value) {
if (h == null) {
return new Node(key, value, RED, 1);
}
int cmp = key.compareTo(h.key);
if (cmp < 0) {
h.left = put(h.left, key, value);
} else if (cmp > 0) {
h.right = put(h.right, key, value);
} else {
h.value = value;
}
if (!isRed(h.left) && isRed(h.right)) {
h = rotateLeft(h);
}
if (isRed(h.left) && isRed(h.left.left)) {
h = rotateRight(h);
}
if (isRed(h.left) && isRed(h.right)) {
flipColors(h);
}
h.size = size(h.left) + size(h.right) + 1;
return h;
}
private Node rotateLeft(Node h) {
Node x = h.right;
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = RED;
x.size = h.size;
h.size = size(h.left) + size(h.right) + 1;
return x;
}
private void flipColors(Node h) {
h.color = RED;
h.left.color = BLACK;
h.right.color = BLACK;
}
private Node rotateRight(Node h) {
Node x = h.left;
h.left = x.right;
x.right = h;
x.color = h.color;
h.color = RED;
x.size = h.size;
h.size = size(h.left) + size(h.right) + 1;
return x;
}
private boolean isRed(Node x) {
if (x == null) {
return false;
}
return x.color == RED;
}
private int size(Node x) {
if (x == null) {
return 0;
}
return x.size;
}
private 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<>();
inOrder(root, queue);
return queue;
}
private void inOrder(Node x, LinkedQueue<Key> queue) {
if (x == null) {
return;
}
inOrder(x.left, queue);
queue.enqueue(x.key);
inOrder(x.right, queue);
}
private Key min() {
return min(root).key;
}
private Node min(Node x) {
if (x.left == null) {
return x;
}
return min(x.left);
}
private 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;
}
private 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 static void main(String[] args) {
RedBlackBST<String, Integer> st = new RedBlackBST<>();
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.contains("S"));
st.delete("S");
System.out.println("contains : " + st.contains("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 : 3
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
M E R C L P X H
Happy learning !!
网友评论