chapter 12
Post on 09-Jan-2016
26 Views
Preview:
DESCRIPTION
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.
20071130 chap12 Hsiu-Hui Lee 2
Binary Search Tree
• Binary-search property: Let x be a node in a binary search tree. If y i
s 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].
20071130 chap12 Hsiu-Hui Lee 3
Binary search Tree
20071130 chap12 Hsiu-Hui Lee 4
Inorder tree walk
INORDER_TREE_WALK(x)1 if
2 then INORDER_TREE_WALK(left[x])
3 print key[x]
4 INORDER_TREE_WALK(right[x])
x nil
20071130 chap12 Hsiu-Hui Lee 5
Theorem 12.1 If x is the root of an n-node subtree, then th
e call INORDER-TREE-WALK(x) takes (n) time.
Proved by substitution method.
20071130 chap12 Hsiu-Hui Lee 6
• Preorder tree walk• Postorder tree walk
20071130 chap12 Hsiu-Hui Lee 7
Querying a binary search tree
20071130 chap12 Hsiu-Hui Lee 8
TREE_SEARCH(x, k)1 if or
2 then return x
3 if
4 then return TREE_SEARCH(left[x],k)
5 else return TREE_SEARCH(right[x],k)
x nil k key x [ ]
k key x [ ]
20071130 chap12 Hsiu-Hui Lee 9
ITERATIVE_SEARCH (x, k)1 While or
2 do if
3 then
4 then
5 return x
x nil k key x [ ]k key x [ ]
x left x [ ]
x right x [ ]
20071130 chap12 Hsiu-Hui Lee 10
MAXIMUM and MINIMUM TREE_MINIMUM(x)
1 while left[x] NIL
2 do x left[x]3 return x
TREE_MAXIMUM(x)1 while right[x] NIL
2 do x right[x]3 return x
20071130 chap12 Hsiu-Hui Lee 11
TREE_SUCCESSOR1 if
2 then return TREE_MINIMUM(right[x])
3
4 while and
5 do
6
7 return y
right x nil[ ]
][xpy
y nil x right y [ ]x y
y p y [ ]
SUCCESSOR and PREDECESSOR
20071130 chap12 Hsiu-Hui Lee 12
• 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)
20071130 chap12 Hsiu-Hui Lee 13
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
20071130 chap12 Hsiu-Hui Lee 15
Tree-Insert(T, z)1 y NIL
2 x root[T]3 while x NIL
4 do y x5 if key[z] < key[x]6 then x left[x]7 else x right[x]8 p[z] y9 if y = NIL10 then root[T] z tree T was empty11 else if key[z] < key[y]12 then left[y] z13 else right[y] z
20071130 chap12 Hsiu-Hui Lee 16
Inserting an item with key 13 into a binary search tree
20071130 chap12 Hsiu-Hui Lee 17
Tree-Delete(T, z)1 if left[z] = NIL or right[z] = NIL2 then y z3 else y Tree-Successor(z)4 if left[y] NIL5 then x left[y]6 else x right[y]7 if x NIL8 then p[x] p[y] 9 if p[y] = NIL10 then root[T] x11 else if y = left[p[y]]12 then left[p[y]] x13 else right[p[y]] x14 if y z15 then key[z] key[y]16 copy y’s satellite data into z17 return y
20071130 chap12 Hsiu-Hui Lee 18
Case a: z has no children
20071130 chap12 Hsiu-Hui Lee 19
Case b: z has only one child
20071130 chap12 Hsiu-Hui Lee 20
Case c: z has two children
20071130 chap12 Hsiu-Hui Lee 21
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.
top related