您的位置:首页 > 其它

二叉查找树( Binary Search Tree)学习小记

2014-06-12 23:59 351 查看
近几日学习了二叉查找树( Binary Search Tree),既BST。在算法(Algorithm 4thEdtion)中是为了实现更高性能的ST(符号表)而进行的深入讲解的。

这是我第一次明显的感受到因为算法改进而带来大幅度的性能提升。由于在日常数据处理中,符号表不仅需要根据Key索引,还要进行大量的新节点插入。而这两点都对性能提出了要求

当我们采用无序链式存储结构时,未命中的查找和插入操作都需要N次比较。命中的查找在最坏的情况下也需要N次比较。特别的,向一个空表中插入N个不同的键插入需要~N2/2次比较。如此高的存储代价,显然不是我们想要的。

为此,一个稍好的解决方案是采取二分查找。而一般的二分查找是基于有序数组。然而,向大小为N的有序数组中插入一个新的元素在最坏的情况下需要访问~2N次数组。

插入元素的代价依然十分庞大。

所以,我们需要一种能够将链表插入的灵活性和有序数组查找高效性结合起来的符号表。那边是二叉查找树。

BST: 一个二叉树,每个节点都有一个可以比较(Comparable)的Key,大于其左子树且小于其右子树。

基本节点以及API实现较为简单,代码如下。

节点:

private class Node {
private Node left, right;
private Key key;  //键值
private Value val;    //关联值
private int N; //存储子树与该节点的总个数

public Node(Key key, Value val, int N) {
this.key = key;
this.val = val;
this.N = N;
}
}


size():

public int size(Node x) {
if(x == null)
return 0;
else return x.N;
}


get():

public 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.val;
}


在put()方法以及接下来的min() floor()等等方法中,都是用了一个在类封装中的常用方法,就是公有方法搭配调用私有方法,此法很好的实现了封装,并且便于日后代码的修改。

public void put(Key key, Value val) {
put(root, key, val); //调用同名私有方法,从根节点开始搜索
}

private Node put(Node x, Key key, Value val) {
if(x == null)      //搜索到null,说明并不存在key该节点 创建新节点
return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if(cmp < 0)
x.left = put(x.left, key, val);  //利用递归更新节点
else if(cmp > 0)
x.right = put(x.right, key, val);
else {
x.val = val;
}
x.N = size(x.left) + size(x.right) + 1; //逐层更新节点数量
return x;
}


至于与min() max()同理:

public Key min() {
return  min(root).key;
}
private Node min(Node x) {
if(x.left == null) {
return x;
}
return min(x.left);
}

public Key max() {
return max(root).key;
}
private Node max(Node x) {
if(x.right == null)
return x;
return max(x.right);
}


接下来的floor(Key Key)(求比Key小的最大键)设计的还是比较巧妙的。

当找到x.key键比key小的时候 存在两种可能 一种是 所求键在x.key的右子树内 或者就是x.key本身 这个值得注意

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);  // 若key没有x.key大 则肯定在x.left内
Node t = floor(x.right, key);   // 查找是否在比x.key大且合适的键值
if(t != null)
return t;
else
return x;   //不存在 返回x
}


select() rank() 互为逆操作 代码不难懂

public Key select(int n) {
return select(root, n).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;
}
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);
}


最为精彩的部分还是delete()的操作,在这之前 先演示deleteMin().

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;
}


delete(Key key)最难处理的情况是 该键左右子树均不是null的情况

此时要将Key的ceiling()值(大于他的最小值)替换到他的位置上。 利用其右子树的deleteMin()可实现此步骤 然后再将路径上都节点size()大小依次更新

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);  //将那个最小键从原位置删除 更新size
x.left = t.left; //接管被删节点左子树 块结束 t被删除
}
x.N = size(x.left) + size(x.right) + 1; //更新节点数
return x;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: