cse 373: data structures and algorithms · •structure property (binary tree) •each node has...
TRANSCRIPT
Instructor:LiliandeGreefQuarter:Summer2017
CSE373:DataStructuresandAlgorithmsLecture9:BinarySearchTrees
Today
• Announcements• BinaryTrees• Height• Traversals
• BinarySearchTrees• Definition• find• insert• delete• buildTree
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
Reminder:Treeterminology
A
E
B
D F
C
G
IH
LJ MK N
Node/Vertex
Edges
Root
Leaves
LeftsubtreeRightsubtree
BinaryTrees• Binarytree:Eachnodehasatmost2children(branchingfactor2)• Binarytreeis
• Aroot(withdata)• Aleftsubtree(maybeempty)• Arightsubtree(maybeempty)
• SpecialCases:
(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.
BinaryTrees:SomeNumbersRecall:heightofatree=longestpathfromroottoleaf(countedges)
Forbinarytreeofheighth:
• max#ofleaves:
• max#ofnodes:
• min#ofleaves:
• min#ofnodes:
Forn nodes,theminheight(best-case)is
themaxheight(worst-case)is
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
TreeTraversals:Practice
Whichonemakessenseforevaluatingthisexpressiontree?
• Pre-order: root,leftsubtree,rightsubtree
• In-order: leftsubtree,root,rightsubtree
• Post-order: leftsubtree,rightsubtree,root
+
*
2 4
5
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
3
1171
84
5
4
181062
115
8
20
21
7
15
Practice:aretheseBSTs?
Howdowefind(value) inBST’s?
2092
155
12
307 1710
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;
}
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;
}
OtherBST“Finding”Operations
findMin:Findminimum node
findMax:Findmaximumnode 2092
155
12
307 1710
insert inBST
insert(13)insert(8)insert(31)
Worst-caserunningtime:
2092
155
12
307 1710
Practicewithinsert,primerfordelete
Startwithanemptytree.Insertthefollowingvalues,inthegivenorder:14, 2, 5, 20, 42, 1, 4, 16
Then,changingasfewnodesaspossible,deletethefollowinginorder:42, 14
Whatwouldtherootoftheresultingtreebe?A. 2B. 4C. 5D. 16
(Extraspaceforscratchwork/notes)
delete inBST
• Whymightdelete beharderthaninsert?
• Basicidea:
• Threepotentialcasestofix:
delete case:Leaf
delete(17)
2092
155
12
307 1710
delete case:OneChild
delete(15)
2092
155
12
307 10
delete case:TwoChildren
delete(5)
2092
5
12
307 10
Whatcanwereplace5 with?
delete case:TwoChildren
Whatcanwereplacethenodewith?
Options:
delete case:TwoChildren(example#2)
delete(23)
3092
235
12
7 10
18
1915 3225
Changingasfewnodesaspossible,deletethefollowinginorder:42, 14
Practicewithinsert,primerfordelete
4251
202
14
4
16
delete throughLazyDeletion
• LazydeletioncanworkwellforaBST• Simpler• Cando“realdeletions”laterasabatch• Someinsertscanjust“undelete”atreenode
• But• Canwastespaceandslowdownfindoperations• Makesomeoperationsmorecomplicated:
• e.g.,findMin andfindMax?
buildTree forBST
Let’sconsiderbuildTree (insertvaluesstartingfromanemptytree)
Insertvalues1,2,3,4,5,6,7,8,9intoanemptyBST
• Ifinsertedingivenorder,whatisthetree?
• Whatbig-OruntimeforbuildTree onthissortedinput?
• Isinsertinginthereverseorderanybetter?
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?