CS 2230 CS II: Data...

17
CS 2230 CS II: Data structures Meeting 23: binary search trees Brandon Myers University of Iowa

Transcript of CS 2230 CS II: Data...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)!