算法筆記-查找01:二叉查找樹

符號表

符號表最主要的一個目的就是將一個鍵和一個值關聯起來。用例能夠將一個鍵值對插入符號表并在之后能夠從符號表的所有鍵值對中按照鍵直接查找到相應的值。

定義:符號表是一種存儲鍵值對的數據結構,支持兩種操作:插入(put),即將一組鍵值對存入表中;查找 (get),即根據給定的鍵得到相應的值。

典型的應用程序中,鍵都是Comparable對象,許多符號表的實現都利用了Comparable接口來保持鍵的有序性從而更好的實現put和get方法。更重要的是在這些實現中,我們可以認為符號表都會保持鍵的有序并大大擴展它的API。

Symbol tables

實現這份API有很多種方式,兩種最為簡單直接的方式就是使用有序數組或者是鏈表,但是鏈表實現下查找和插入一個鍵所需要的時間都是線性的,有序數組實現下查找雖然可以用二分查找優化,但插入的成本還是N,所以我們需要更加高效的實現方式,于是就有了接下來的樹及以后的哈希表。

定義:一棵二叉查找樹是一棵二叉樹,其中每一個結點都含有一個Comparable的鍵(以及相關聯的值),且每個結點的鍵都大于左子樹的任意結點的鍵并小于右子樹的任意結點的鍵。

二叉樹由結點組成,節點包含有指向其他結點的鏈接,這個鏈接可以為空也可以指向其他結點。在二叉樹中,每個結點只能有一個父節點(根結點沒有父結點),而且每個結點都只有左右兩個鏈接,分別指向自己的左子結點和右子結點。盡管鏈接指向的是結點,但是我們可以將每個鏈接看作指向了另一棵二叉樹,而這棵樹的根結點就是被指向的結點。在二叉樹中每一個結點都包含了一個鍵和一個值,鍵之間也有順序之分以支持高效的查找。

二叉樹
基本實現
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;}
    }
    
    public int size(){
        return size(root);
    }
    
    private int size(Node x){
        if (x == null) return 0;
        else return x.N;
    }
    
    public Value get(Key key){
        return get(root, key);
    }
    
    private Value get(Node x, Key key){
        //在以x為根結點的子樹中查找并返回key所對應的值,如果找不到就返回null
        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 void  put(Key key, Value value){
        //查找key,找到就更新它的值,否則為它創建一個新的結點
        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.N = 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 Key max(){
        return max(root).key;
    }
    
    private Node max(Node x){
        if (x.right == null) return x;
        return max(x.right);
    }
    
    
    //查找小于等于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);
        Node t = floor(x.right, key);
        if (t != null) return t;
        else return x;
    }
    
    //查找大于等于key的最小鍵
    public Key ceiling(Key key){
        Node x = ceiling(root, key);
        if (x == null) return null;
        return x.key;
    }
    
    private Node ceiling(Node x, Key key){
        if (x == null) return null;
        int cmp = key.compareTo(x.key);
        if (cmp == 0) return x;
        if (cmp > 0) return ceiling(x.right, key);
        Node t = ceiling(x.left, key);
        if (t != null) return t;
        else return x;
    }
    
    //查找排名為k的鍵
    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的鍵的數量
    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;
    }
    
    //刪除最大鍵
    public void deleteMax(){
        root = deleteMax(root);
    }
    
    private Node deleteMax(Node x){
        if (x.right == null) return x.left;
        x.right = deleteMax(x.right);
        x.N = size(x.left) + size(x.right) + 1;
        return x;
    }
    
    
    //刪除任意鍵
    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);
        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;
    }
    
    
    //二叉查找樹的范圍查找
    public Iterable<Key> keys(){
        return keys(min(), max());
    }
    
    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<Key> 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 (cmphi <= 0 && cmphi >= 0) queue.equeue(x.key);
        if (cmphi > 0) keys(x.right, queue, lo, hi);
    }   
}

上面的代碼實現了API中的所有方法。

二叉查找樹構造的符號表的性能分析

在一棵二叉樹中,所有操作在最壞的情況下所需的時間都和樹的高度成正比,對于,如果鍵夠隨機,樹的高度將會是鍵的總數的對數,在這種情況下插入和查找的運行時間的增長數量級都會是lgN, 但是,在一些特定的情況下(比如用例是按從小到大的順序輸入將鍵值對存入二叉樹的),二叉樹就會變得根鏈表一樣,從二導致非常差的性能


二叉樹

于是我們需要尋找更好的數據結構來解決這個問題,或許我們能夠使用某種方法來控制樹的高度。

二叉樹的前序,中序,后序遍歷

前序遍歷

private void print(Node x){
        if (x == null) return;
        System.out.println(x.key);
        print(x.left);
        print(x.right);
    }

中序遍歷

private void print(Node x){
        if (x == null) return;
        print(x.left);
        System.out.println(x.key);
        print(x.right);
    }

后序遍歷

private void print(Node x){
        if (x == null) return;
        print(x.left);
        print(x.right);
        System.out.println(x.key);
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,572評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,071評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,409評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,569評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,360評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,895評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,979評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,123評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,643評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,559評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,742評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,250評論 5 356
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,981評論 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,363評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,622評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,354評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,707評論 2 370

推薦閱讀更多精彩內容