浜屽弶鏌ユ壘鏍?Java瀹炵幇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浜屽弶鏌ユ壘鏍?Java瀹炵幇相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%9b%be%e7%89%87' title='鍥剧墖'>鍥剧墖   閬嶅巻   else   spl   div   create   click   play   鎯呭喌   

涓€銆佸畾涔変笌姒傚康

 1銆佷簩鍙夋煡鎵炬爲鍙堢О涓?strong>浜屽弶鎺掑簭鏍?/strong>锛屽畠鏄竴绉嶇壒娈婁簩鍙夋爲銆?/p>

浜屽弶鏌ユ壘鏍戠殑瀹氫箟涓轰竴棰楃┖鏍戯紝鎴栨弧瓒冲涓嬫€ц川鐨勬爲锛?/p>

鈶犲乏瀛愭爲闈炵┖锛屽垯宸﹀瓙鏍戞墍鏈夌粨鐐圭殑鍊煎潎灏忎簬鏍圭粨鐐圭殑鍊笺€?/p>

鈶″彸瀛愭爲闈炵┖锛屽垯鍙冲瓙鏍戞墍鏈夌粨鐐圭殑鍊煎潎澶т簬锛堟垨澶т簬绛変簬锛夋牴缁撶偣鐨勫€笺€?/p>

鈶㈠乏鍙冲瓙鏍戜篃閫掑綊鐨勫畾涔変负浜屽弶鏌ユ壘鏍?/p>

浜屻€佷簩鍙夋煡鎵炬爲鐨勬搷浣?/h3>

1銆佺粨鐐圭殑瀛樺偍缁撴瀯

public class TreeNode {
    int data;
    TreeNode lchild;
    TreeNode rchild;
    TreeNode(int s) {
        data = s;
    }
    public String toString() {
        return String.valueOf(data);
    }
}

 2銆佷簩鍙夋煡鎵炬爲鐨勫垱寤?/strong>

step1锛?/strong>鍦˙ST鏍戜腑鎻掑叆涓€涓妭鐐?/p>

[绠楁硶鎬濇兂]

鈶犲鏋淏ST鏍戞槸绌烘爲锛屽垯寰呮彃鍏ョ殑key鍊兼垚涓築ST鏍戠殑鏍广€?/p>

鈶¤嫢BST鏍戦潪绌猴紝鍒檏ey涓庝簩鍙夋爲鐨勬牴杩涜姣旇緝銆傦紙杩欓噷鐨勬牴鏄箍涔夌殑锛屽嵆鍙〃绀築ST瀛愭爲鐨勬牴锛?/p>

a. key < bst.data锛屽垯灏唊ey鎻掑叆宸﹀瓙鏍?/p>

b. key >= bat.data锛屽垯灏唊ey鎻掑叆鍙冲瓙鏍?/p>

浠庣畻娉曠殑鎻忚堪涓氨鍙互鐪嬪嚭锛岃繖閲屾槸閫掑綊瀹炵幇鐨勩€?/p>

private static int[] tree = {24,53,90,12,28,45};
TreeNode insertBST(TreeNode bst, int key) {
    if(bst == null) {
        bst = new TreeNode(key);
        return bst;
    }else {
        if(key < bst.data) {
            bst.lchild = insertBST(bst.lchild,key);
        }else {
            bst.rchild = insertBST(bst.rchild,key);
        }
        return bst;
    }
}

 step2锛?/strong>鍒涘缓涓€棰桞ST鏍?/p>

TreeNode CreateBST() {
    TreeNode bst = null;
    for(int key : tree) {
        bst = insertBST(bst,key);
    }
    return bst;
}

 3銆佷簩鍙夋煡鎵炬爲鐨勬煡鎵?/strong>

[绠楁硶鎬濇兂]锛?/strong>棣栧厛灏嗗緟鏌ユ壘鐨刱ey鍊间笌鏍圭粨鐐瑰叧閿瓧鍊兼瘮杈?/p>

鈶爇ey = bst.data锛屽垯鎵惧埌閫€鍑?/p>

鈶ey < bst.data锛屽垯鏌ユ壘宸﹀瓙鏍?/p>

鈶ey > bst.data锛屽垯鏌ユ壘鍙冲瓙鏍?/p>

閫掑綊瀹炵幇锛?/p>

boolean searchBST(TreeNode bst, int key) {
    if(bst == null) {
        return false;
    }else {
        if(bst.data == key) {
            return true;
        }else if(bst.data > key) {
            return searchBST(bst.lchild,key);
        }else {
            return searchBST(bst.rchild,key);
        }
    }
    
}

 闈為€掑綊瀹炵幇锛氭煡鎵剧殑鎬濇兂灏辨槸閬嶅巻鍢涳紝杩欓噷浣跨敤寰幆鏉ュ疄鐜般€?/p>

TreeNode searchBST1(TreeNode bst, int key) {
    if(bst == null) {
        return null;
    }else {
        TreeNode p = bst;
        while(p != null) {
            if(p.data == key) {
                return p;
            }else if(p.data > key) {
                p = p.lchild;
            }else {
                p = p.rchild;
            }
        }
        return null;
    }
}

 4銆佷簩鍙夋煡鎵炬爲鐨勫垹闄?/strong>

鍒犻櫎鎿嶄綔瑕佷繚璇侊紝鍒犻櫎缁撶偣涔嬪悗杩樻槸涓€棰桞ST鏍戯紝杩欓噷缁嗚妭鍜屾儏鍐佃繕鏄尯澶氱殑锛屾湁鐐归毦鍝熴€?/p>

[绠楁硶鎬濇兂]锛?/strong>鍦ㄨ繖閲屾垜浠亣璁撅紝濡傛灉BST鏍戜笉绌猴紝鍒橞ST鏍戠殑鍙冲瓙鏍戝瓨鍦ㄣ€傚叾瀹濨ST鍙冲瓙鏍戠┖涓嶇┖瀵圭畻娉曠殑瀹炵幇鏄病鏈夊奖鍝嶇殑銆?/p>

锛?锛夐鍏堬紝璁剧疆p鎸囬拡閬嶅巻鏍戯紝鏌ユ壘鍒拌鍒犻櫎鐨勮妭鐐癸紝璁剧疆f鎸囬拡锛宖鎸囧悜p鐨勫弻浜茶妭鐐?/p>

TreeNode p = bst;
TreeNode f = null;
while(p != null) {
    if(key == p.data) {
        break;
    }else if(key < p.data) {
        f = p;
        p = p.lchild;
    }else {
        f = p;
        p = p.rchild;
    }
}

锛?锛塸.lchild == null 宸﹀瓙鏍戠┖

鎯呭喌鈶爁 == null 瑕佸垹闄ょ殑鏄牴缁撶偣

鎯呭喌鈶.lchild == p p鏄痜鐨勫乏瀛╁瓙锛屽垯灏唒鐨勫彸瀛愭爲鏀逛负f鐨勫彸瀛愭爲

鎯呭喌鈶.rchild == p p鏄痜鐨勫彸瀛╁瓙 锛屽垯灏唒鐨勫彸瀛愭爲鏀逛负f鐨勫乏瀛愭爲

if(p.lchild == null) {
    if(f == null) {
        return bst = p.rchild;
    }else if(f.lchild == p){
        f.lchild = p.rchild;
    }else if(f.rchild == p) {
        f.rchild = p.rchild;
    }
}

 灏辩畻p鐨勫彸瀛愭爲涓虹┖涔熶笉褰卞搷浠€涔堛€?/p>

锛?锛塸鐨勫乏瀛愭爲涓嶄负绌?/p>

璁剧疆鎸囬拡s銆乹锛宷鎸囧悜s鐨勫墠椹辫妭鐐?/p>

姝ラ鈶犲皢s鎸囬拡绉诲埌锛宲宸﹀瓙鏍戠殑鏈€鍙崇锛堝嵆涓簭閬嶅巻p鐨勭洿鎺ュ墠椹辫妭鐐癸級

姝ラ鈶′氦鎹涓巔鑺傜偣鐨勫€硷紙s鏄痯宸﹀瓙鏍戜腑鐨勬渶澶у€间簡锛?/p>

姝ラ鈶€€銆€

銆€銆€鎯呭喌a if(s缁撶偣灏辨槸 p鐨勫乏瀛╁瓙缁撶偣) 锛屽垯q.lchild = s.lchild

銆€銆€鎯呭喌b else 鍒檘.rchild = s.lchild

else {
    TreeNode s = null;
    TreeNode q = p;
    s = p.lchild;
    while(s.rchild!=null) { 
        q = s;
        s = s.rchild; 
    }
    if(q == p) { q.lchild = s.lchild;}
    else { q.rchild = s.lchild;}
    p.data = s.data;
}

 瀹屾暣浠g爜锛?/p>

鎶€鏈浘鐗? id=
TreeNode deleteBST(TreeNode bst, int key) {
    if(bst == null) {
        System.out.println("bst null");
    }
    TreeNode p = bst;
    TreeNode f = null;
    while(p != null) {
        if(key == p.data) {
            break;
        }else if(key < p.data) {
            f = p;
            p = p.lchild;
        }else {
            f = p;
            p = p.rchild;
        }
    }
    if(p.lchild == null) {
        if(f == null) {
            return bst = p.rchild;
        }else if(f.lchild == p){
            f.lchild = p.rchild;
        }else if(f.rchild == p) {
            f.rchild = p.rchild;
        }
    }else {
        TreeNode s = null;
        TreeNode q = p;
        s = p.lchild;
        while(s.rchild!=null) { 
            q = s;
            s = s.rchild; 
        }
        if(q == p) { q.lchild = s.lchild;}
        else { q.rchild = s.lchild;}
        p.data = s.data;
    }
    return bst;
}
View Code

 

銆€銆€

以上是关于浜屽弶鏌ユ壘鏍?Java瀹炵幇的主要内容,如果未能解决你的问题,请参考以下文章

Golang-鏌ユ壘(浜屽垎娉曟煡鎵?

绾㈤粦鏍戞彃鍏ャ€佸垹闄よВ鏋?/h1>

浠庨厭妗屾父鎴忕湅浜屽垎鏌ユ壘绠楁硶

Spring Cloud Alibaba锛堜簩锛?閰嶇疆涓績澶氶」鐩€佸閰嶇疆鏂囦欢銆佸垎鐩綍瀹炵幇

HTML璺宠浆鍒伴〉闈㈡寚瀹氫綅缃殑鍑犵鏂规硶

(c)2006-2024 SYSTEM All Rights Reserved IT常识