cs 2230 cs ii: data...
TRANSCRIPT
![Page 1: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/1.jpg)
CS2230CSII:Datastructures
Meeting23:binarysearchtreesBrandonMyers
UniversityofIowa
![Page 2: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/2.jpg)
Learningobjectives
• Identifywhetherabinarytreeisabinarysearchtree(BST)
• RunaddandcontainsonaBST
• Runremove onaBST
• CalculatetherunningtimeofBSTmethods
![Page 3: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/3.jpg)
Ourmotivationseeingtheforestforthetrees:)Thoughtexperiment:Let’susetheSetADTtoimplementadictionarythatcanlookupwords.
Writefor1minute:
IfthenumberofwordsinthedictionaryisN,howlongwillittaketolookupaword?
Ifahumanlookedupawordinabookwherethewordsareinarandomorder,howlongwillittake?Explain.
Ifthebookwasanormaldictionary(wordssorted),howlongwillittake?Explain.
// recall: ListSet is unsortedSet<String> dictionary = new ListSet<>();
![Page 4: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/4.jpg)
Ourmotivationseeingtheforestforthetrees:)Thoughtexperiment:Let’susetheSetADTtoimplementadictionarythatcanlookupwords.
YouwillimplementadifferentkindofSetusinga“binarysearchtree”(BST)tolearnmoreaboutthisgeneralconcept:organizingdatacarefullyhelpsusaccessitwithalowerrunningtime
// recall: ListSet is unsortedSet<String> dictionary = new ListSet<>();
Set<String> dictionary = new BSTSet<>();
![Page 5: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/5.jpg)
Binarysearchtree(BST)everyelementintheleftsubtreeislessthantherooteveryelementintherightsubtreeisgreaterthantheroot
trueforeverynode 10
6 20
3
4
12 33
31
![Page 6: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/6.jpg)
Identifywhetherabinarytreeisabinarysearchtree(BST)
1
2 3
4
5
6 7
8
10
5
72
24
3318
33
24
10
18
7
5
20
40
19
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
1)
2)
3)
![Page 7: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/7.jpg)
Supposeweaddthenumber18intothisbinarysearchtree.Wherecouldweput18tomakesurethetreestaysabinarysearchtree?
Peerinstruction
10
6 20
3
4
12 33
31
a)rightchildof33b)rightchildof6c) leftchildof31d)leftchildof3e)rightchildof12f) wecan’tmaintaintheBST
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 8: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/8.jpg)
find anelement(usefulhelpermethod)
19
![Page 9: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/9.jpg)
findinJavaprivate static Node find (int num, TreeNode node){
if (node == null){return null;
}else if (node.val == num){
return node;}else if (node.val < num){
return find(num, node.left);}else{
return find(num, node.right);}
} 19
![Page 10: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/10.jpg)
RunfindonaBST
Listtheorderinwhichnodeswillbevisited whenwecallfind(17)
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
19
![Page 11: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/11.jpg)
CalculatetherunningtimeofBSTmethods
Whatisthe1)worstcase,2)bestcaserunningtimeoffind(x)?AnswerintermsofN,thenumberofnodes.Assumeallthesubtreesareequallysized(asshown).
a) 𝑂(1)b) 𝑂(𝑙𝑜𝑔𝑁)c) 𝑂(𝑁)d) 𝑂(𝑁𝑙𝑜𝑔𝑁)e) 𝑂(𝑁2)f) 𝑂 20
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 12: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/12.jpg)
WhyistheheightO(logN)?N height bottoms1 0 12 1 13 1 24 2 15 2 2
6 2 3
7 2 4
8 3 1
𝑁 = 2234526 + 𝑏𝑜𝑡𝑡𝑜𝑚𝑠 − 1𝑁 − 𝑏𝑜𝑡𝑡𝑜𝑚𝑠 + 1 = 2234526
log@ 𝑁 − 𝑏𝑜𝑡𝑡𝑜𝑚𝑠 + 1 = ℎ𝑒𝑖𝑔ℎ𝑡
ℎ𝑒𝑖𝑔ℎ𝑡 = log2𝑁
and,weknowlog2𝑁 ∈ 𝑂(𝑙𝑜𝑔𝑁)
orequivalently
![Page 13: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/13.jpg)
addanelement
• Hereistheinitialtree.• Wewanttoinsert24intothistree
![Page 14: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/14.jpg)
addanelement
• Startatrootandcheckif24islessthanorgreaterthan.• Moverightof18• Leftof25• Rightof24
![Page 15: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/15.jpg)
RunaddonaBST1. Wherewillthekey11gowhenwecalladd(11)?2. Wherewillthekey22gowhenwecalladd(22)?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
10
6 20
3
4
12 33
312
![Page 16: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/16.jpg)
Anexceptionalcase
add(30)add(24)add(18)add(10)add(7)add(5)add(2)
Ifweuseouralgorithmforadd(),whatwillthisBSTlooklike?
(drawit)
![Page 17: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/.../lectures/lecture-024-binary-search-trees.pdf · Our motivation seeing the forest for the trees :) Thought experiment: Let’s](https://reader034.vdocument.in/reader034/viewer/2022050122/5f528c422fed22084f7148ac/html5/thumbnails/17.jpg)
It’spossibletogetunlucky
add(30)add(24)add(18)add(10)add(7)add(5)add(2)
33
24
10
18
7
5
2
findisworstcaseO(N)!