Download presentation

Presentation is loading. Please wait.

Published byAllison Creekmore Modified over 2 years ago

1
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.

2
20071130 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].

3
20071130 chap12Hsiu-Hui Lee3 Binary search Tree

4
20071130 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])

5
20071130 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.

6
20071130 chap12Hsiu-Hui Lee6 Preorder tree walk Postorder tree walk

7
20071130 chap12Hsiu-Hui Lee7 Querying a binary search tree

8
20071130 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)

9
20071130 chap12Hsiu-Hui Lee9 ITERATIVE_SEARCH (x, k) 1While or 2do if 3then 4then 5return x

10
20071130 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

11
20071130 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

12
20071130 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)

13
20071130 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.

14
12.3 Insertion and deletion

15
20071130 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

16
20071130 chap12Hsiu-Hui Lee16 Inserting an item with key 13 into a binary search tree

17
20071130 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

18
20071130 chap12Hsiu-Hui Lee18 Case a: z has no children

19
20071130 chap12Hsiu-Hui Lee19 Case b: z has only one child

20
20071130 chap12Hsiu-Hui Lee20 Case c: z has two children

21
20071130 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.

Similar presentations

Presentation is loading. Please wait....

OK

David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.

David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on earthquake information Ppt on surface water drains English 8 unit 13 read ppt online Ppt on series and parallel combination of resistors in series Free ppt on mobile number portability ppt Ppt on total parenteral nutrition ppt Ppt on water pollution download Ppt online shopping cart Ppt on polynomials in maths what does commutative mean Marketing mix ppt on sony blu-ray