CS 2230 CS II: Data...
Transcript of CS 2230 CS II: Data...
CS2230CSII:Datastructures
Meeting23:binarysearchtreesBrandonMyers
UniversityofIowa
Learningobjectives
• Identifywhetherabinarytreeisabinarysearchtree(BST)
• RunaddandcontainsonaBST
• Runremove onaBST
• CalculatetherunningtimeofBSTmethods
Ourmotivationseeingtheforestforthetrees:)Thoughtexperiment:Let’susetheSetADTtoimplementadictionarythatcanlookupwords.
Writefor1minute:
IfthenumberofwordsinthedictionaryisN,howlongwillittaketolookupaword?
Ifahumanlookedupawordinabookwherethewordsareinarandomorder,howlongwillittake?Explain.
Ifthebookwasanormaldictionary(wordssorted),howlongwillittake?Explain.
// recall: ListSet is unsortedSet<String> dictionary = new ListSet<>();
Ourmotivationseeingtheforestforthetrees:)Thoughtexperiment:Let’susetheSetADTtoimplementadictionarythatcanlookupwords.
YouwillimplementadifferentkindofSetusinga“binarysearchtree”(BST)tolearnmoreaboutthisgeneralconcept:organizingdatacarefullyhelpsusaccessitwithalowerrunningtime
// recall: ListSet is unsortedSet<String> dictionary = new ListSet<>();
Set<String> dictionary = new BSTSet<>();
Binarysearchtree(BST)everyelementintheleftsubtreeislessthantherooteveryelementintherightsubtreeisgreaterthantheroot
trueforeverynode 10
6 20
3
4
12 33
31
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)
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+
find anelement(usefulhelpermethod)
19
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
RunfindonaBST
Listtheorderinwhichnodeswillbevisited whenwecallfind(17)
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
19
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+
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
addanelement
• Hereistheinitialtree.• Wewanttoinsert24intothistree
addanelement
• Startatrootandcheckif24islessthanorgreaterthan.• Moverightof18• Leftof25• Rightof24
RunaddonaBST1. Wherewillthekey11gowhenwecalladd(11)?2. Wherewillthekey22gowhenwecalladd(22)?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
10
6 20
3
4
12 33
312
Anexceptionalcase
add(30)add(24)add(18)add(10)add(7)add(5)add(2)
Ifweuseouralgorithmforadd(),whatwillthisBSTlooklike?
(drawit)
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)!