1二叉查找树
二叉查找树定义:
- 是一棵二叉树
- 每个结点含有一个键和对应的值
- 每个结点的键都大于其左子树的的任意结点,小于右子树的任意结点。
1.1基本实现
/**
* 基于二叉查找树的符号表
* @Author: AZhu
* @Date: 2021/3/3 10:29
*/
public class BST<Key extends Comparable<Key>, Value> {
private Node root;//二叉查找树的根结点
private class Node {
private Key key;//键
private Value value;//值
private Node left, right;//左右子树
private int N;//以该节点为根的树中的结点总数
public Node(Key key, Value value, int n) {
this.key = key;
this.value = value;
this.N = n;
}
}
/**
* 返回树的结点数目
* @return
*/
public int size() {
return size(root);
}
private int size(Node x) {
if (x == null) {
return 0;
} else {
return x.N;
}
}
/**
* 查找key对应的value
* @param key
* @return
*/
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;//找到了
}
}
/**
* 插入或更新一对键值对
* @param key
* @param value
*/
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;//更新
}
//增加路径上的每一个结点的N值
x.N = size(x.left) + size(x.right) + 1;
return x;
}
}
1.2分析
使用二叉查找树的算法的运行时间取决于树的形状,而树的形状又取决于键被插入的先后顺序。
二叉查找树和快速排序几乎就是双胞胎。
在由N
个随机键构建的二叉查找树中:
- 查找命中的平均比较次数为
~ 2lnN
(约1.39lgN
) - 插入操作和查找未命中的平均比较次数为
~ 2lnN
(约1.39lgN
)
也就是说明二叉查找树中查找随机键的成本比二分查找高约39%
。
2有序性相关方法
二叉查找树得以广泛应用的一个重要原因是能够保持键的有序性
。
2.1基本实现
/**
* 基于二叉查找树的符号表
* @Author: AZhu
* @Date: 2021/3/3 10:29
*/
public class BST<Key extends Comparable<Key>, Value> {
/**
* 返回最大的key
* @return
*/
public Key max() {
return max(root).key;
}
private Node max(Node x) {
if (x.right==null) return x;
return max(x.right);
}
/**
* 向下取整
* @param key
* @return
*/
public Key floor(Key key) {
Node x = floor(root, key);
if (x==null) return null;
return x.key;
}
private Node floor(Node x, Key key) {
if (x==null) return null;
int cmp = key.compareTo(x.key);
if (cmp==0) return x;
if (cmp<0) return floor(x.left, key);
Node t = floor(x.right, key);
if (t != null) {
return t;
} else {
return x;
}
}
/**
* 返回排名为第k个结点的key
* @param k
* @return
*/
public Key select(int k) {
return select(root, k).key;
}
private Node select(Node x, int k) {
if (x==null) return null;
int t = size(x.left);
if (t > k) {
return select(x.left, k);
} else if (t < k) {
return select(x.right, k - t - 1);
} else {
return x;
}
}
/**
* 返回树中小于key的键的数量
* @param key
* @return
*/
public int rank(Key key) {
return rank(key, root);
}
private int rank(Key key, Node x) {
if (x==null) return 0;
int cmp = key.compareTo(x.key);
if (cmp < 0) {
return rank(key, x.left);
} else if (cmp > 0) {
return 1 + size(x.left) + rank(key, x.right);
} else {
return size(x.left);
}
}
/**
* 删除最小的键的结点
*/
public void deleteMin() {
root = deleteMin(root);
}
private Node deleteMin(Node x) {
if (x.left==null) return x.right;
x.left = deleteMin(x.left);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
/**
* 删除key对应的结点
* @param key
*/
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.right==null) return x.left;
if (x.left==null) return x.right;
Node t = x;
x = min(t.right);//右子树的最小结点
x.right = deleteMin(t.right);
x.left = t.left;
}
x.N = size(x.left) + size(x.right) + 1;
return x;
}
/**
* 返回所有key的集合
* @return
*/
public Iterable<Key> keys() {
return keys(min(), max());
}
/**
* 返回lo..hi范围内的key
* @param lo
* @param hi
* @return
*/
public Iterable<Key> keys(Key lo, Key hi) {
Queue<Key> queue = new Queue<Key>();
keys(root, queue, lo, hi);
return queue;
}
private void keys(Node x, Queue queue, Key lo, Key hi) {
if (x==null) return;
int cmplo = lo.compareTo(x.key);
int cmphi = hi.compareTo(x.key);
if (cmplo < 0) {
keys(x.left, queue, lo, hi);
}
if (cmplo <= 0 && cmphi >= 0) {
queue.enqueue(x.key);
}
if (cmphi > 0) {
keys(x.right, queue, lo, hi);
}
}
}
2.2分析
在一棵二叉查找树中:
- 所有操作在最坏的情况下所需的时间都和树的高度成正比。
简单的符号表实现的成本总结:
算法 | 最坏情况 | 平均情况 | 是否高效的支持有序性相关的操作 | ||
---|---|---|---|---|---|
查找 | 插入 | 查找 | 插入 | ||
顺序查找 | N | N | N/2 | N | 否 |
二分查找 | lgN | 2N | lgN | N | 是 |
二叉查找树 | N | N | 1.39lgN | 1.39lgN | 是 |