SFU CMPT-307 2008-2 1 Lecture: Week 9 SFU CMPT-307 2008-2 Lecture: Week 9 Ján Maňuch E-mail: jmanuch@sfu.ca Lecture on July 8, 2008, 5.30pm-8.20pm
SFU CMPT-307 2008-2 2 Lecture: Week 9 Binary search trees Support many dynamic-set operations, e.g. Search Minimum Maximum Insert Delete... Can be used as dictionary, priority queue... Running time depends on height of tree: if complete, then ÐÓ Òµ in the worst case if just one chain, then Òµ in the worst case if random, then ÐÓ Òµ expected
nodes Ý in left subtree of Ü have key Ý key Ü SFU CMPT-307 2008-2 3 Lecture: Week 9 First of all, what is a binary search tree? it s a binary tree represented using linked data structure nodes are objects objects store key data pointers to left child, right child, and parent (NULL if one is missing) root is only node with parent=null Binary-search-tree property Ü node in binary search tree nodes Ý in right subtree of Ü have key Ý key Ü Note: heaps are different!
SFU CMPT-307 2008-2 4 Lecture: Week 9 Definition: inorder walk of binary tree: for each node Ü 1. visit left subtree (recursively) 2. print key of Ü 3. visit right subtree (recursively) Inorder-Tree-Walk ܵ 1: Ü if NULL then 2: Inorder-Tree-Walk left ܵµ 3: print key ܵ 4: Inorder-Tree-Walk right ܵµ 5: end if Interesting property of BSTs: In-order walk of BST prints all keys in sorted order
SFU CMPT-307 2008-2 5 Lecture: Week 9 Example for inorder walk: 6 5 10 3 8 12 2 4 Result is 2 3 4 5 6 8 10 12
SFU CMPT-307 2008-2 6 Lecture: Week 9 Assignment Problem 9.1. (deadline: July 15, 5:30pm) Give a nonrecursive algorithm that performs an inorder tree walk using only a constant memory. Your algorithm can test two pointers for equality.
SFU CMPT-307 2008-2 7 Lecture: Week 9 Theorem. If Ü is root of Ò-node (sub)tree, then Inorder-Tree-Walk ܵ takes Òµ time. Proof. Ì Òµ time if procedure called on Ò-node (sub)tree Clearly Ì ¼µ for some constant (test Ü NULL) Otherwise, suppose left subtree has nodes, right subtree Ò ½ has nodes. Then Ò ¼ for Ì Òµ Ì µ Ì Ò ½µ with constant
SFU CMPT-307 2008-2 8 Lecture: Week 9 Ì Òµ Ì µ Ì Ò ½µ We will use substitution method to Ì Òµ µò show Ò ¼ For we µò Ì have ¼µ, OK Induction hypothesis: For Ñ every Ì Ñµ µñ Ò,. We will show that is true also for Ò: Ì Òµ Ì µ Ì Ò ½µ µ µ Ò ½µ µ µò µ µ µò µ µò
SFU CMPT-307 2008-2 9 Lecture: Week 9 Searching Want to search for a node with given key Return pointer to node if exists, otherwise NULL begin search at root follow path downward for Ü node on path, key Ü compare with if equal, done if key Ü, continue in left subtree (left subtree contains keys key Ü ) if key Ü, continue in right subtree (right subtree contains keys key Ü )
SFU CMPT-307 2008-2 10 Lecture: Week 9 µ Tree-Search Ü 1: Ü if NULL key Ü or then 2: return Ü 3: else if key Ü then 4: return Tree-Search left Ü µ 5: else 6: return Tree-Search right Ü µ 7: end if
SFU CMPT-307 2008-2 11 Lecture: Week 9 15 6 18 3 7 17 20 2 4 13 9 Running time is Ç µ, height of tree
SFU CMPT-307 2008-2 12 Lecture: Week 9 Minimum/maximum Minimum of the tree rooted in Ü can be found by following left pointers as long as possible (not necessarily to a leaf!) Tree-Minimum ܵ 1: left Ü while NULL do Ü left Ü 2: 3: end while 4: return Ü Maximum of the tree rooted in Ü can be found by following right pointers as long as possible (not necessarily to a leaf!) Tree-Maximum ܵ 1: right Ü while NULL do Ü right Ü 2: 3: end while 4: return Ü Both have running time Ç µ, height of tree
SFU CMPT-307 2008-2 13 Lecture: Week 9 Successor/predecessor Definition: successor/predecessor in sorted order given by inorder walk For instance, if Ü values Ü ¾ ÜÒ ½ are stored in a tree, then the successor Ü of is Ü ½. 6 5 10 3 8 12 2 4 Idea of an algorithm for finding successor: If right subtree of Ü is nonempty, then successor of Ü is leftmost node in right subtree ( the smallest among the larger ) Found by calling Tree-Minimum on right subtree Otherwise (right subtree is empty and Ü has a successor), then this is the lowest ancestor Ü of whose left child is also ancestor Ü of (A node is ancestor of itself)
SFU CMPT-307 2008-2 14 Lecture: Week 9 Tree-Successor ܵ 1: right Ü if NULL then 2: return Tree-Minimum right Ü µ 3: end if 4: Ý parent Ü 5: while Ý NULL and Ü right Ý do 6: Ü Ý Ý parent Ý 7: 8: end while 9: return Ý Running time clearly Ç µ, height of tree Tree-Predecessor symmetric Theorem. Operations Search, Minimum, Maximum, Successor, Predecessor run in Ç µ time in BST of height
SFU CMPT-307 2008-2 15 Lecture: Week 9 Assignment Problem 9.2. (deadline: July 15, 5:30pm) Consider a binary tree Ì whose keys are distinct. Show that if the right subtree of a node Ü in Ì is empty and Ü has a successor Ý, then Ý is the lowest ancestor of Ü whose left child is also an ancestor of Ü. (Recall that every node is its own ancestor.)
SFU CMPT-307 2008-2 16 Lecture: Week 9 Insertion Now we re talking about dynamic sets Insertion of new element easy. From root, walk down tree according to value of new key and open new leaf 12 5 18 2 9 15 19 13 17 Running time again Ç µ
SFU CMPT-307 2008-2 17 Lecture: Week 9 Want to insert new value Ú Given node Þ with key Þ Ú, left Þ right Þ NULL Tree-Insert Ì Þµ 10: end while 1: Ý NULL 2: Ü root Ì 3: while Ü NULL do 4: Ý Ü 5: if key Þ key Ü then 6: Ü left Ü 7: else 8: Ü right Ü 9: end if 11: parent Þ Ý 12: if Ý NULL then 13: root Ì Þ /* Ì was empty */ 14: else if key Þ key Ý then 15: left Ý Þ 16: else 17: right Ý Þ 18: end if
SFU CMPT-307 2008-2 18 Lecture: Week 9 Deletion Given pointer to some node Þ. Three cases. 1. Þ has no children At Þ s parent parent Þ, just replace link to Þ with NULL 2. Þ has one child splice out Þ, make new link between its parent and its child 3. Þ has two children splice out Þ s successor Ý (which has no left child, as seen from Homework 9.3), and replace Þ s key and data with Ý s key and data
SFU CMPT-307 2008-2 19 Lecture: Week 9 Assignment Problem 9.3. (deadline: July 15, 5:30pm) Show that if a node in a binary search tree has two children, then its successor has no left child and its predecessor has no right child.
SFU CMPT-307 2008-2 20 Lecture: Week 9 no children 15 15 5 16 3 12 20 z 10 13 18 23 6 7 5 16 3 12 20 10 18 23 6 7
SFU CMPT-307 2008-2 21 Lecture: Week 9 one child 15 15 5 16 z 5 20 3 12 20 3 12 18 23 10 13 18 23 10 13 6 6 7 7
SFU CMPT-307 2008-2 22 Lecture: Week 9 two children z 5 15 16 y 6 15 5 z 16 3 12 20 3 12 20 10 13 18 23 10 13 18 23 y 6 7 7 15 6 16 3 12 20 10 13 18 23 7
SFU CMPT-307 2008-2 23 Lecture: Week 9 Tree-Delete Ì Þµ 1: if left Þ NULL or right Þ NULL then 2: Ý Þ 3: else 4: Ý Tree-Successor Þµ 5: end if 6: if left Ý NULL then 7: Ü left Ý 8: else 9: Ü right Ý 10: end if 11: if Ü NULL then 12: parent Ü parent Ý 13: end if 14: if parent Ý NULL then 15: root Ì Ü 16: else if Ý left parent Ý then 17: left parent Ý Ü 18: else 19: right parent Ý Ü 20: end if 21: if Ý Þ then 22: key Þ key Ý 23: copy Ý s data into Þ 24: end if 25: return Ý