cse 373: data structures and algorithms · •structure property (binary tree) •each node has...

28
Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 9: Binary Search Trees

Upload: others

Post on 28-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

Instructor:LiliandeGreefQuarter:Summer2017

CSE373:DataStructuresandAlgorithmsLecture9:BinarySearchTrees

Page 2: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

Today

• Announcements• BinaryTrees• Height• Traversals

• BinarySearchTrees• Definition• find• insert• delete• buildTree

Page 3: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

Announcements

• Changetoofficehoursforjustthisweek• Tuesday’s“office”officehours/privateofficehours

• 12:00pm– 12:30pm• (notat1:30pm!)

• DorothyandItrading2:00pm- 3:00pmofficehoursthisweek• Sametimeandlocation

• Homework1Statistics• Mean:39.7/50(+1extracredit)• Median:42.5/50(+0extracredit)• Max:49/50(+1)or47/50(+4)• StandardDeviation:10.18

Page 4: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

Reminder:Treeterminology

A

E

B

D F

C

G

IH

LJ MK N

Node/Vertex

Edges

Root

Leaves

LeftsubtreeRightsubtree

Page 5: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

BinaryTrees• Binarytree:Eachnodehasatmost2children(branchingfactor2)• Binarytreeis

• Aroot(withdata)• Aleftsubtree(maybeempty)• Arightsubtree(maybeempty)

• SpecialCases:

Page 6: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

(Lastweek’spractice)Whatdoesthefollowingmethoddo?

int height(Node root){if (root == null),

return -1;return 1 + max(height(root.left),

height(root.right);}

A. Itcalculatesthenumberofnodesinthetree.

B. Itcalculatesthedepthofthenodes.

C. Itcalculatestheheightofthetree.

D. Itcalculatesthenumberofleavesinthetree.

Page 7: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

BinaryTrees:SomeNumbersRecall:heightofatree=longestpathfromroottoleaf(countedges)

Forbinarytreeofheighth:

• max#ofleaves:

• max#ofnodes:

• min#ofleaves:

• min#ofnodes:

Forn nodes,theminheight(best-case)is

themaxheight(worst-case)is

Page 8: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

TreeTraversals

Atraversal isanorderforvisitingallthenodesofatree

• Pre-order: root,leftsubtree,rightsubtree

• In-order: leftsubtree,root,rightsubtree

• Post-order: leftsubtree,rightsubtree,root

A

B

D E

C

F

G

Page 9: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

TreeTraversals:Practice

Whichonemakessenseforevaluatingthisexpressiontree?

• Pre-order: root,leftsubtree,rightsubtree

• In-order: leftsubtree,root,rightsubtree

• Post-order: leftsubtree,rightsubtree,root

+

*

2 4

5

Page 10: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

4

121062

115

8

14

13

7 9

• Structureproperty(binarytree)• Eachnodehas£ 2 children• Result:keepsoperationssimple

• Orderproperty

• Result:straight-forwardtofindanygivenvalue

Abinarysearch tree isatypeofbinarytree(butnotallbinarytreesarebinarysearchtrees!)

BinarySearch Tree(BST)DataStructure

Page 11: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

3

1171

84

5

4

181062

115

8

20

21

7

15

Practice:aretheseBSTs?

Page 12: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

Howdowefind(value) inBST’s?

2092

155

12

307 1710

Page 13: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

find inBST:RecursiveVersion

Whatistherunningtime?2092

155

12

307 1710

Data find(Data value, Node root){if(root == null)

return null;if(key < root.value)

return find(value, root.left);if(key > root.value)

return find(value, root.right);return root.value;

}

Page 14: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

find inBST:IterativeVersion

2092

155

12

307 1710

Data find(Object value, Node root){while(root != null

&& root.value != value) {if (value < root.value)

root = root.left;else (value > root.value)

root = root.right;}if(root == null)

return null;return root.value;

}

Page 15: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

OtherBST“Finding”Operations

findMin:Findminimum node

findMax:Findmaximumnode 2092

155

12

307 1710

Page 16: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

insert inBST

insert(13)insert(8)insert(31)

Worst-caserunningtime:

2092

155

12

307 1710

Page 17: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

Practicewithinsert,primerfordelete

Startwithanemptytree.Insertthefollowingvalues,inthegivenorder:14, 2, 5, 20, 42, 1, 4, 16

Then,changingasfewnodesaspossible,deletethefollowinginorder:42, 14

Whatwouldtherootoftheresultingtreebe?A. 2B. 4C. 5D. 16

Page 18: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

(Extraspaceforscratchwork/notes)

Page 19: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

delete inBST

• Whymightdelete beharderthaninsert?

• Basicidea:

• Threepotentialcasestofix:

Page 20: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

delete case:Leaf

delete(17)

2092

155

12

307 1710

Page 21: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

delete case:OneChild

delete(15)

2092

155

12

307 10

Page 22: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

delete case:TwoChildren

delete(5)

2092

5

12

307 10

Whatcanwereplace5 with?

Page 23: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

delete case:TwoChildren

Whatcanwereplacethenodewith?

Options:

Page 24: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

delete case:TwoChildren(example#2)

delete(23)

3092

235

12

7 10

18

1915 3225

Page 25: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

Changingasfewnodesaspossible,deletethefollowinginorder:42, 14

Practicewithinsert,primerfordelete

4251

202

14

4

16

Page 26: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

delete throughLazyDeletion

• LazydeletioncanworkwellforaBST• Simpler• Cando“realdeletions”laterasabatch• Someinsertscanjust“undelete”atreenode

• But• Canwastespaceandslowdownfindoperations• Makesomeoperationsmorecomplicated:

• e.g.,findMin andfindMax?

Page 27: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

buildTree forBST

Let’sconsiderbuildTree (insertvaluesstartingfromanemptytree)

Insertvalues1,2,3,4,5,6,7,8,9intoanemptyBST

• Ifinsertedingivenorder,whatisthetree?

• Whatbig-OruntimeforbuildTree onthissortedinput?

• Isinsertinginthereverseorderanybetter?

Page 28: CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has £2children •Result: keeps operations simple •Order property •Result: straight-forward

buildTree forBST

Insertvalues1,2,3,4,5,6,7,8,9intoanemptyBST

Whatweifcouldsomehowre-arrangethem• medianfirst,thenleftmedian,rightmedian,etc.

5,3,7,2,1,4,8,6,9

• Whattreedoesthatgiveus?

• Whatbig-Oruntime?