chapter 12

21
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as w ell as various materials from the web.

Upload: taro

Post on 09-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

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. Binary Search Tree. Binary-search property : - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 12

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.

Page 2: Chapter 12

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

Page 3: Chapter 12

20071130 chap12 Hsiu-Hui Lee 3

Binary search Tree

Page 4: Chapter 12

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

Page 5: Chapter 12

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.

Page 6: Chapter 12

20071130 chap12 Hsiu-Hui Lee 6

• Preorder tree walk• Postorder tree walk

Page 7: Chapter 12

20071130 chap12 Hsiu-Hui Lee 7

Querying a binary search tree

Page 8: Chapter 12

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 [ ]

Page 9: Chapter 12

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 [ ]

Page 10: Chapter 12

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

Page 11: Chapter 12

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

Page 12: Chapter 12

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)

Page 13: Chapter 12

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.

Page 14: Chapter 12

12.3 Insertion and deletion

Page 15: Chapter 12

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

Page 16: Chapter 12

20071130 chap12 Hsiu-Hui Lee 16

Inserting an item with key 13 into a binary search tree

Page 17: Chapter 12

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

Page 18: Chapter 12

20071130 chap12 Hsiu-Hui Lee 18

Case a: z has no children

Page 19: Chapter 12

20071130 chap12 Hsiu-Hui Lee 19

Case b: z has only one child

Page 20: Chapter 12

20071130 chap12 Hsiu-Hui Lee 20

Case c: z has two children

Page 21: Chapter 12

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.