Lecture10

Binary Search Tree

Properties

  • Left subtree
    • The left subtree of a given node contains only nodes with keys lesser than the node's key.
  • Right subtree
    • The right subtree of a given node contains only nodes with keys greater than the node's key.
image-20210328105543998.png

Insertion

Case 1: Empty tree

  • Create a new node with new key
  • Make that new node root

Case 2: Non-empty Tree

  • Start at the root
  • Traverse the tree to find a location for the new key(stops at a leaf node X)
  • Add a new node as a child of X

Deletion

Case 1 : No Children

  • It is a leaf node, we don't need to worry about "disconnecting" its children.
  • "Sever" the connection between the parent node and found ndoe.
  • Either delete the "stray" node or let garbage collector remove it. Done!
image-20210328110520387.png
if (current.leftChid == null && current.rightChid == null){
            if (current == root){
                root = null;
            }else if (isLeftChild){
                parent.leftChid = null;
            }else{
                parent.rightChid = null;
            }

Case 2: One Child

  • It is not a leaf node, we NEED to worry about its child.
  • FOUND node's only child should take it's place to preserve BST properties
  • Connect FOUND node's parent to FOUND's child. "Old" connection "severed"
  • Either delete the "stray" node or let garbage collector remove it. Done!
image-20210328112046132.png
else if (current.rightChid == null){
                if (current == root){
                    root = current.rightChid;
                }else if (isLeftChild){
                    parent.leftChid = current.leftChid;
                }else{
                    parent.rightChid = current.rightChid;
                }
        }else if (current.leftChid == null){
            if (current == root){
                root = current.rightChid;
            }else if (isLeftChild){
                parent.leftChid = current.leftChid;
            }else{
                parent.rightChid = current.rightChid;
            }

Case 3: Two Children

Successor a Leaf Node,Successor a Right Child

  • It is not s leaf node, we NEED to worry about its children.
  • Trick: find an IN-ORDER successor of FOUND node place it instead of FOUND
  • FOUND's LEFT child also becomes FOUND's successor LEFT CHILD
  • We can now "server" FOUND's connection to its LEFT child. Successor's got it.
  • Now we need to connect FOUND'S Parent with FOUND'S successor
  • Either delete the "stray" node or let garbage collector remove it. Done!
image-20210328113809441.png

Successor is a Left Descendant of Right Child

image-20210328114102618.png

Successor is a Left Descendant of Right Child , Successor has a Right Child

image-20210328114358375.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末舷胜,一起剝皮案震驚了整個(gè)濱河市髓霞,隨后出現(xiàn)的幾起案子凡傅,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡痘煤,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門猿规,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)衷快,“玉大人,你說(shuō)我怎么就攤上這事姨俩≌喊危” “怎么了?”我有些...
    開封第一講書人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵环葵,是天一觀的道長(zhǎng)调窍。 經(jīng)常有香客問(wèn)我,道長(zhǎng)张遭,這世上最難降的妖魔是什么邓萨? 我笑而不...
    開封第一講書人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮菊卷,結(jié)果婚禮上缔恳,老公的妹妹穿的比我還像新娘。我一直安慰自己洁闰,他們只是感情好歉甚,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著扑眉,像睡著了一般铃芦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上襟雷,一...
    開封第一講書人閱讀 49,111評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音仁烹,去河邊找鬼耸弄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛卓缰,可吹牛的內(nèi)容都是我干的计呈。 我是一名探鬼主播砰诵,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼捌显!你這毒婦竟也來(lái)了茁彭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤扶歪,失蹤者是張志新(化名)和其女友劉穎理肺,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體善镰,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡妹萨,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了炫欺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乎完。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖品洛,靈堂內(nèi)的尸體忽然破棺而出树姨,到底是詐尸還是另有隱情,我是刑警寧澤桥状,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布帽揪,位于F島的核電站,受9級(jí)特大地震影響岛宦,放射性物質(zhì)發(fā)生泄漏台丛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一砾肺、第九天 我趴在偏房一處隱蔽的房頂上張望挽霉。 院中可真熱鬧,春花似錦变汪、人聲如沸侠坎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)实胸。三九已至,卻和暖如春番官,著一層夾襖步出監(jiān)牢的瞬間庐完,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工徘熔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留门躯,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓酷师,卻偏偏與公主長(zhǎng)得像讶凉,于是被迫代替她去往敵國(guó)和親染乌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容