最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java數(shù)據(jù)結(jié)構(gòu)之二叉查找樹的實(shí)現(xiàn)

 更新時(shí)間:2022年03月28日 15:49:13   作者:gonghr  
二叉查找樹(亦稱二叉搜索樹、二叉排序樹)是一棵二叉樹,且各結(jié)點(diǎn)關(guān)鍵詞互異,其中根序列按其關(guān)鍵詞遞增排列。本文將通過示例詳細(xì)講解二叉查找樹,感興趣的可以了解一下

定義

二叉查找樹(亦稱二叉搜索樹、二叉排序樹)是一棵二叉樹,且各結(jié)點(diǎn)關(guān)鍵詞互異,其中根序列按其關(guān)鍵詞遞增排列。

等價(jià)描述:二叉查找樹中任一結(jié)點(diǎn) P,其左子樹中結(jié)點(diǎn)的關(guān)鍵詞都小于 P 的關(guān)鍵詞,右子樹中結(jié)點(diǎn)的關(guān)鍵詞都大于 P 的關(guān)鍵詞,且結(jié)點(diǎn) P 的左右子樹也都是二叉查找樹

節(jié)點(diǎn)結(jié)構(gòu)

:one: key:關(guān)鍵字的值

:two: value:關(guān)鍵字的存儲信息

:three: left:左節(jié)點(diǎn)的引用

:four: right:右節(jié)點(diǎn)的引用

class BSTNode<K extends Comparable<K>,V>{
    public K key;
    public V value;

    public BSTNode<K,V> left;
    public BSTNode<K,V> right;
}

為了代碼簡潔,本文不考慮屬性的封裝,一律設(shè)為 public

查找算法

思想:利用二叉查找樹的特性,左子樹值小于根節(jié)點(diǎn)值,右子樹值大于根節(jié)點(diǎn)值,從根節(jié)點(diǎn)開始搜索

  • 如果目標(biāo)值等于某節(jié)點(diǎn)值,返回該節(jié)點(diǎn)
  • 如果目標(biāo)值小于某節(jié)點(diǎn)值,搜索該節(jié)點(diǎn)的左子樹
  • 如果目標(biāo)值大于某節(jié)點(diǎn)值,搜索該節(jié)點(diǎn)的右子樹

:one: 遞歸版本

public BSTNode<K, V> searchByRecursion(K key) {
        return searchByRecursion(root, key);
    }

    private BSTNode<K, V> searchByRecursion(BSTNode<K, V> t, K key) {
        if (t == null || t.key == key) return t;
        else if (key.compareTo(t.key) < 0) return searchByRecursion(t.left, key);
        else return searchByRecursion(t.right, key);
    }

:two: 迭代版本

public BSTNode<K,V> searchByIteration(K key) {
        BSTNode<K,V> p = this.root;
        while(p != null) {
            if(key.compareTo(p.key) < 0) p = p.left;
            else if(key.compareTo(p.key) > 0) p = p.right;
            else return p;
        }
        return null;
    }

插入算法

  • 在以 t 為根的二叉查找樹中插入關(guān)鍵詞為 key 的結(jié)點(diǎn)
  • 在 t 中查找 key ,在查找失敗處插入

:one: 遞歸版本

public void insertByRecursion(K key, V value) {
        this.root = insertByRecursion(root, key, value);
    }

    private BSTNode<K, V> insertByRecursion(BSTNode<K, V> t, K key, V value) {
        if (t == null) {
            return new BSTNode<>(key, value);
        } 
        else if (key.compareTo(t.key) < 0) t.left = insertByRecursion(t.left, key, value);
        else if (key.compareTo(t.key) > 0) t.right = insertByRecursion(t.right, key, value);
        else {
            t.value = value;  // 如果二叉查找樹中已經(jīng)存在關(guān)鍵字,則替換該結(jié)點(diǎn)的值
        }
        return t;
    }

:two: 迭代版本

public void insertByIteration(K key, V value) {
        BSTNode<K, V> p = root;
        if (p == null) {
            root = new BSTNode<>(key, value);
            return;
        }
        BSTNode<K, V> pre = null;
        while (p != null) {
            pre = p;
            if (key.compareTo(p.key) < 0) p = p.left;
            else if (key.compareTo(p.key) > 0) p = p.right;
            else {
                p.value = value;    // 如果二叉查找樹中已經(jīng)存在關(guān)鍵字,則替換該結(jié)點(diǎn)的值
                return;
            }
        }
        if(key.compareTo(pre.key) < 0) {
            pre.left = new BSTNode<>(key, value);
        } else {
            pre.right = new BSTNode<>(key, value);
        }
    }

刪除算法

在以 t 為根的二叉查找樹中刪除關(guān)鍵詞值為 key 的結(jié)點(diǎn)

在 t 中找到關(guān)鍵詞為 key 的結(jié)點(diǎn),分三種情況刪除 key

1.若 key 是葉子節(jié)點(diǎn),則直接刪除

2.若 key 只有一棵子樹,則子承父業(yè)

3.若 key 既有左子樹也有右子樹,則找到 key 的后繼結(jié)點(diǎn),替換 key 和后繼節(jié)點(diǎn)的值,然后刪除后繼節(jié)點(diǎn)(后繼節(jié)點(diǎn)只有一棵子樹,轉(zhuǎn)化為第二種情況)。

后繼結(jié)點(diǎn)是當(dāng)前結(jié)點(diǎn)的右子樹的最左結(jié)點(diǎn),如果右子樹沒有左子樹,則后繼節(jié)點(diǎn)就是右子樹的根節(jié)點(diǎn)。

public void removeByRecursion(K key) {
        this.root = removeByRecursion(root, key);
    }
    private BSTNode<K, V> removeByRecursion(BSTNode<K, V> t, K key) {
        if(t == null) return root;
        else if(t.key.compareTo(key) < 0) t.right = removeByRecursion(t.right, key); // key大,遞歸處理右子樹
        else if(t.key.compareTo(key) > 0) t.left = removeByRecursion(t.left, key);   // key小,遞歸處理左子樹
        else {
            if(t.right == null) return t.left;  // 情況一、二一起處理
            if(t.left == null) return t.right;  // 情況一、二一起處理
            BSTNode<K, V> node = t.right;       // 情況三:右子樹沒有左子樹
            if (node.left == null) {
                node.left = t.left;
            } else {                            // 情況三:右子樹有左子樹
                BSTNode<K, V> pre = null;
                while (node.left != null) {
                    pre = node;
                    node = node.left;
                }
                t.key = node.key;
                t.value = node.value;
                pre.left = node.right;
            }
        }
        return t;
    }

完整代碼

class BSTNode<K extends Comparable<K>, V> {
    public K key;
    public V value;

    public BSTNode<K, V> left;
    public BSTNode<K, V> right;

    public BSTNode(K key, V value) {
        this.key = key;
        this.value = value;
    }
}

class BSTree<K extends Comparable<K>, V> {
    public BSTNode<K, V> root;

    private void inorder(BSTNode<K, V> root) {
        if (root != null) {
            inorder(root.left);
            System.out.print("(key: " + root.key + " , value: " + root.value + ") ");
            inorder(root.right);
        }
    }

    private void preorder(BSTNode<K, V> root) {
        if (root != null) {
            System.out.print("(key: " + root.key + " , value: " + root.value + ") ");
            preorder(root.left);
            preorder(root.right);
        }
    }

    private void postorder(BSTNode<K, V> root) {
        if (root != null) {
            postorder(root.left);
            postorder(root.right);
            System.out.print("(key: " + root.key + " , value: " + root.value + ") ");
        }
    }

    public void postorderTraverse() {
        System.out.print("后序遍歷:");
        postorder(root);
        System.out.println();
    }

    public void preorderTraverse() {
        System.out.print("先序遍歷:");
        preorder(root);
        System.out.println();
    }

    public void inorderTraverse() {
        System.out.print("中序遍歷:");
        inorder(root);
        System.out.println();
    }

    public BSTNode<K, V> searchByRecursion(K key) {
        return searchByRecursion(root, key);
    }

    private BSTNode<K, V> searchByRecursion(BSTNode<K, V> t, K key) {
        if (t == null || t.key == key) return t;
        else if (key.compareTo(t.key) < 0) return searchByRecursion(t.left, key);
        else return searchByRecursion(t.right, key);
    }

    public BSTNode<K, V> searchByIteration(K key) {
        BSTNode<K, V> p = this.root;
        while (p != null) {
            if (key.compareTo(p.key) < 0) p = p.left;
            else if (key.compareTo(p.key) > 0) p = p.right;
            else return p;
        }
        return null;
    }

    public void insertByRecursion(K key, V value) {
        this.root = insertByRecursion(root, key, value);
    }

    private BSTNode<K, V> insertByRecursion(BSTNode<K, V> t, K key, V value) {
        if (t == null) {
            return new BSTNode<>(key, value);
        } else if (key.compareTo(t.key) < 0) t.left = insertByRecursion(t.left, key, value);
        else if (key.compareTo(t.key) > 0) t.right = insertByRecursion(t.right, key, value);
        else {
            t.value = value;
        }
        return t;
    }

    public void insertByIteration(K key, V value) {
        BSTNode<K, V> p = root;
        if (p == null) {
            root = new BSTNode<>(key, value);
            return;
        }
        BSTNode<K, V> pre = null;
        while (p != null) {
            pre = p;
            if (key.compareTo(p.key) < 0) p = p.left;
            else if (key.compareTo(p.key) > 0) p = p.right;
            else {
                p.value = value;    // 如果二叉查找樹中已經(jīng)存在關(guān)鍵字,則替換該結(jié)點(diǎn)的值
                return;
            }
        }
        if (key.compareTo(pre.key) < 0) {
            pre.left = new BSTNode<>(key, value);
        } else {
            pre.right = new BSTNode<>(key, value);
        }
    }

    public void removeByRecursion(K key) {
        this.root = removeByRecursion(root, key);
    }
    private BSTNode<K, V> removeByRecursion(BSTNode<K, V> t, K key) {
        if(t == null) return root;
        else if(t.key.compareTo(key) < 0) t.right = removeByRecursion(t.right, key); // key大,遞歸處理右子樹
        else if(t.key.compareTo(key) > 0) t.left = removeByRecursion(t.left, key);   // key小,遞歸處理左子樹
        else {
            if(t.right == null) return t.left;  // 情況一、二一起處理
            if(t.left == null) return t.right;  // 情況一、二一起處理
            BSTNode<K, V> node = t.right;       // 情況三:右子樹沒有左子樹
            if (node.left == null) {
                node.left = t.left;
            } else {                            // 情況三:右子樹有左子樹
                BSTNode<K, V> pre = null;
                while (node.left != null) {
                    pre = node;
                    node = node.left;
                }
                t.key = node.key;
                t.value = node.value;
                pre.left = node.right;
            }
        }
        return t;
    }
}

:bug: 方法測試:

public class Main {
    public static void main(String[] args) {
        BSTree<Integer, Integer> tree = new BSTree<>();
//        tree.insertByRecursion(1, 1);
//        tree.insertByRecursion(5, 1);
//        tree.insertByRecursion(2, 1);
//        tree.insertByRecursion(4, 1);
//        tree.insertByRecursion(3, 1);
//        tree.insertByRecursion(1, 2);
        tree.insertByIteration(1, 1);
        tree.insertByIteration(5, 1);
        tree.insertByIteration(2, 1);
        tree.insertByIteration(4, 1);
        tree.insertByIteration(3, 1);
        tree.insertByIteration(1, 5);
        tree.removeByRecursion(4);
        tree.inorderTraverse();
        tree.postorderTraverse();
        tree.preorderTraverse();
        BSTNode<Integer, Integer> node = tree.searchByIteration(1);
        System.out.println(node.key + " " + node.value);
    }
}

中序遍歷:(key: 1 , value: 5) (key: 2 , value: 1) (key: 3 , value: 1) (key: 5 , value: 1) 
后序遍歷:(key: 3 , value: 1) (key: 2 , value: 1) (key: 5 , value: 1) (key: 1 , value: 5) 
先序遍歷:(key: 1 , value: 5) (key: 5 , value: 1) (key: 2 , value: 1) (key: 3 , value: 1) 
1 5

以上就是Java數(shù)據(jù)結(jié)構(gòu)之二叉查找樹的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Java二叉查找樹的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解java 中的CAS與ABA

    詳解java 中的CAS與ABA

    這篇文章主要介紹了java 中的CAS與ABA的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-05-05
  • dependencies導(dǎo)致的Maven依賴出錯(cuò)包紅問題解決方法

    dependencies導(dǎo)致的Maven依賴出錯(cuò)包紅問題解決方法

    多模塊和分布式開發(fā)一般都是有專門的的dependencies來進(jìn)行jar包的版本依賴問題,本文主要介紹了dependencies導(dǎo)致的Maven依賴出錯(cuò)包紅問題解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-05-05
  • spring項(xiàng)目如何配置多數(shù)據(jù)源(已上生產(chǎn),親測有效)

    spring項(xiàng)目如何配置多數(shù)據(jù)源(已上生產(chǎn),親測有效)

    這篇文章主要介紹了spring項(xiàng)目如何配置多數(shù)據(jù)源(已上生產(chǎn),親測有效),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Spring?Security自定義認(rèn)證邏輯實(shí)例詳解

    Spring?Security自定義認(rèn)證邏輯實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Spring?Security自定義認(rèn)證邏輯的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Java詳細(xì)分析連接數(shù)據(jù)庫的流程

    Java詳細(xì)分析連接數(shù)據(jù)庫的流程

    Java數(shù)據(jù)庫連接,JDBC是Java語言中用來規(guī)范客戶端程序如何來訪問數(shù)據(jù)庫的應(yīng)用程序接口,提供了諸如查詢和更新數(shù)據(jù)庫中數(shù)據(jù)的方法。JDBC也是Sun Microsystems的商標(biāo)。我們通常說的JDBC是面向關(guān)系型數(shù)據(jù)庫的
    2022-05-05
  • Java你不了解的大數(shù)型BigInteger與BigDecimal類

    Java你不了解的大數(shù)型BigInteger與BigDecimal類

    這篇文章主要介紹了Java 處理超大數(shù)類型之BigInteger與BigDecimal案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2022-05-05
  • restTemplate實(shí)現(xiàn)跨服務(wù)API調(diào)用方式

    restTemplate實(shí)現(xiàn)跨服務(wù)API調(diào)用方式

    這篇文章主要介紹了restTemplate實(shí)現(xiàn)跨服務(wù)API調(diào)用方式,具有很好的參考價(jià)值,希望對大家有所幫助。
    2023-07-07
  • Spring Boot實(shí)現(xiàn)跨域訪問實(shí)現(xiàn)代碼

    Spring Boot實(shí)現(xiàn)跨域訪問實(shí)現(xiàn)代碼

    本文通過實(shí)例代碼給大家介紹了Spring Boot實(shí)現(xiàn)跨域訪問的知識,然后在文中給大家介紹了spring boot 服務(wù)器端設(shè)置允許跨域訪問 的方法,感興趣的朋友一起看看吧
    2017-07-07
  • Java調(diào)用python的方法(jython)

    Java調(diào)用python的方法(jython)

    這篇文章主要介紹了Java調(diào)用python的方法(jython),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • java獲取json中的全部鍵值對實(shí)例

    java獲取json中的全部鍵值對實(shí)例

    下面小編就為大家分享一篇java獲取json中的全部鍵值對實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03

最新評論

博湖县| 鹿邑县| 马边| 德安县| 永胜县| 贵南县| 荔浦县| 乌拉特前旗| 长沙县| 大英县| 红桥区| 汉沽区| 东丽区| 来安县| 获嘉县| 青岛市| 白银市| 信宜市| 莱芜市| 宜都市| 临湘市| 龙岩市| 巩义市| 昔阳县| 陇川县| 从化市| 布尔津县| 太和县| 湘西| 鄄城县| 江门市| 无极县| 林口县| 嘉义县| 徐水县| 犍为县| 谷城县| 绿春县| 古交市| 耒阳市| 临夏县|