CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has...
Transcript of CSE 373: Data Structures and Algorithms · •Structure property (binary tree) •Each node has...
![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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/6.jpg)
(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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/18.jpg)
(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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11aa30b92d905d933eeb7a/html5/thumbnails/28.jpg)
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?