您的位置:首页 > 其它

算法 3.2节 二叉查找树的实现

2018-03-11 13:56 162 查看
根据书上的代码,用了递归的方法实现二叉查找树的各项功能,很多地方比较难理解,费了不少功夫

import java.util.Iterator;
import java.util.Stack;

public class BST {

private Node root;

private class Node {
private int key;
private int value;
private Node left;
private Node right;
private int count;

public Node(int key, int value, int count) {
this.key = key;
this.value= value;
this.count = count;
}

}

public BST() {
root.count = 1;
root.left = null;
root.right = null;
}

public int size() {
return size(root);
}

private int size(Node x) {
return  x == null   ? 0 : x.count;
}

public int get(int key) {
return get(root, key);
}

private int get(Node x, int key) {
if (x == null) return -1;
else if (x.key == key) return x.value;
else if (x.key > key) return get(x.left, key);
else if (x.key < key) return get(x.right,key);
}

public void insert(int key, int value) {
root = insert(key, value, root);
}

private Node insert(int key, int value, Node x) {
if (x == null)
return new Node(key, value, 1);
else if (x.key > key)
x.left = insert(key, value, x.left);
else if (x.key < key)
x.right = insert(key,value,x.right);
else x.value = value;

x.count =size(x.left) + size(x.right) +1;
return x;
}

private void insert_without_recursion(int key, int value) {
Node temp = new Node(key,value,1);
if (root == null) root = temp;
Node parent =null,t= root;
while (t != null) {
parent = t;
if (t.key < key) t= t.right;
if (t.key > key) t=t.left;
if (t.key == key) {
t.value = value; // t的类型不是基础类型,所以是传递引用而不是值,相当于指针
return;
}
}
boolean cmp = parent.key > key;
if (cmp)
parent.left = temp;
else
parent.right = temp;

}

public int min() {
return min(root).value;
}

private Node min(Node x) {
if(x.left == null) return x;
else return min(x.left);
}

public Node floor(int key) {
return floor(key,root);
}

private Node floor(int key, Node x) {
if (x == null) return null;
if (x.key == key) return x;
else if (x.key > key) {
return floor(key, x.left);
} else {
Node t = floor(key, x.right);
if (t != null) return t;
else return x;
}

}

public Node select_by_rank(int k) {
return select_by_rank(k,root);
}

private Node select_by_rank(int k,Node x) { //有k个小于这个键的键
if (x == null) return null;
int cmp = size(x.left);
if (cmp == k) return x;
else if (cmp > k ) return select_by_rank(k,x.left);
else  return select_by_rank(k - cmp -1,x.right);
}

public void delectMin() {
root= delectMin(root);
}

private Node delectMin(Node x) {
if (x.left == null) return x.right;
else {
x.left  = delectMin(x.left);
x.count = size(x.left) +size(x.right) +1;
return x;
}
}

public void delect(int key) {
root = delect(key, root);
}

private Node delect(int key, Node x) {
if (x.key > key) x.left = delect(key, x.left);
else if (x.key < key) {
x.right = delect(key, x.right);
} else {
if (x.left == null) return x.right;
if (x.right == null) return x.left;
Node t = x;
x= min(t.right);
x.right = delectMin(t.right);
x.left = t.left;
}
x.count = size(x.left) + size(x.right) +1;
return x;

}

public Iterable<Integer> keys(int low, int high) {
Stack<Integer> stack = new Stack<>() ;
keys(low, high, stack, root);
return stack;
}

public Iterable<Integer> traverse_without_recursion() {
Stack<Node> stack = new Stack<Node>();
Stack<Integer> queue = new Stack<Integer>();
Node x = root;
while (x != null || !stack.isEmpty()) {
if (x != null) {
stack.push(x);
x = x.left;
}
else {
x = stack.pop();
queue.push(x.key);
x = x.right;
}
}
return queue;
}

private void keys(int low, int high, Stack stack, Node x) {
if (x == null) return;
if (x.key > low) keys(low, high, stack, x.left);
if (x.key >= low && x.key <= high) stack.push(x.key);
if (x.key < high) keys(low, high, stack, x.right);
}

public int height() {
return height(root);
}

private int height(Node x) {
if (x == null) return -1;
int a = height(x.left);
int b = height(x.right);
return 1+ a>b ? a : b;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: