Presentation on theme: "Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from."— Presentation transcript:
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web.
chap12Hsiu-Hui Lee2 Binary Search Tree Binary-search property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] key[x]. If y is a node in the right subtree of x, then key[x] key[y].
chap12Hsiu-Hui Lee3 Binary search Tree
chap12Hsiu-Hui Lee4 Inorder tree walk INORDER_TREE_WALK(x) 1if 2then INORDER_TREE_WALK(left[x]) 3print key[x] 4INORDER_TREE_WALK(right[x])
chap12Hsiu-Hui Lee5 Theorem 12.1 If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x) takes (n) time. Proved by substitution method.
chap12Hsiu-Hui Lee6 Preorder tree walk Postorder tree walk
chap12Hsiu-Hui Lee7 Querying a binary search tree
chap12Hsiu-Hui Lee8 TREE_SEARCH(x, k) 1if or 2then return x 3if 4then return TREE_SEARCH(left[x],k) 5else return TREE_SEARCH(right[x],k)
chap12Hsiu-Hui Lee9 ITERATIVE_SEARCH (x, k) 1While or 2do if 3then 4then 5return x
chap12Hsiu-Hui Lee10 MAXIMUM and MINIMUM TREE_MINIMUM(x) 1 while left[x] NIL 2 do x left[x] 3return x TREE_MAXIMUM(x) 1 while right[x] NIL 2 do x right[x] 3 return x
chap12Hsiu-Hui Lee11 TREE_SUCCESSOR 1 if 2 then return TREE_MINIMUM(right[x]) 3 4 while and 5 do 6 7 return y SUCCESSOR and PREDECESSOR
chap12Hsiu-Hui Lee12 Successor of the node with key value 15. (Answer: 17) Successor of the node with key value 6. (Answer: 7) Successor of the node with key value 4. (Answer: 6) Predecessor of the node with key value 6. (Answer: 4)
chap12Hsiu-Hui Lee13 Theorem 12.2 The dynamic-set operations, SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR can be made to run in O(h) time on a binary search tree of height h.
12.3 Insertion and deletion
chap12Hsiu-Hui Lee15 Tree-Insert(T, z) 1 y NIL 2 x root[T] 3 while x NIL 4 do y x 5if key[z] < key[x] 6 then x left[x] 7 else x right[x] 8p[z] y 9 if y = NIL 10 then root[T] z tree T was empty 11 else if key[z] < key[y] 12 then left[y] z 13else right[y] z
chap12Hsiu-Hui Lee16 Inserting an item with key 13 into a binary search tree
chap12Hsiu-Hui Lee17 Tree-Delete(T, z) 1 if left[z] = NIL or right[z] = NIL 2 then y z 3 else y Tree-Successor(z) 4 if left[y] NIL 5then x left[y] 6else x right[y] 7 if x NIL 8then p[x] p[y] 9 if p[y] = NIL 10 then root[T] x 11else if y = left[p[y]] 12then left[p[y]] x 13else right[p[y]] x 14 if y z 15then key[z] key[y] 16 copy y ’ s satellite data into z 17 return y
chap12Hsiu-Hui Lee18 Case a: z has no children
chap12Hsiu-Hui Lee19 Case b: z has only one child
chap12Hsiu-Hui Lee20 Case c: z has two children
chap12Hsiu-Hui Lee21 Theorem 12.3 The dynamic-set operations, INSERT and DELETE can be made to run in O(h) time on a binary search tree of height h.