Chapter 12
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

Chapter 12 PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on
  • Presentation posted in: General

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 :

Download Presentation

Chapter 12

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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.


Binary search tree

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

Hsiu-Hui Lee


Binary search tree1

Binary search Tree

Hsiu-Hui Lee


Inorder tree walk

Inorder tree walk

INORDER_TREE_WALK(x)

1if

2then INORDER_TREE_WALK(left[x])

3print key[x]

4INORDER_TREE_WALK(right[x])

Hsiu-Hui Lee


Theorem 12 1

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.

Hsiu-Hui Lee


Chapter 12

  • Preorder tree walk

  • Postorder tree walk

Hsiu-Hui Lee


Querying a binary search tree

Querying a binary search tree

Hsiu-Hui Lee


Chapter 12

TREE_SEARCH(x, k)

1ifor

2then returnx

3if

4then return TREE_SEARCH(left[x],k)

5else return TREE_SEARCH(right[x],k)

Hsiu-Hui Lee


Chapter 12

ITERATIVE_SEARCH (x, k)

1While or

2do if

3then

4then

5returnx

Hsiu-Hui Lee


Maximum and minimum

MAXIMUM and MINIMUM

TREE_MINIMUM(x)

1 while left[x]  NIL

2 dox  left[x]

  • returnx

    TREE_MAXIMUM(x)

    1 while right[x]  NIL

    2 dox  right[x]

    3 returnx

Hsiu-Hui Lee


Successor and predecessor

SUCCESSOR and PREDECESSOR

TREE_SUCCESSOR

1 if

2 then return TREE_MINIMUM(right[x])

3

4 whileand

5 do

6

7 returny

Hsiu-Hui Lee


Chapter 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)

Hsiu-Hui Lee


Theorem 12 2

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.

Hsiu-Hui Lee


12 3 insertion and deletion

12.3 Insertion and deletion


Chapter 12

Tree-Insert(T, z)

1 y  NIL

2 x root[T]

3 whilex NIL

4 doy x

5ifkey[z] < key[x]

6 thenx left[x]

7 elsex right[x]

  • p[z]  y

    9 ify = NIL

    10 thenroot[T]  z tree T was empty

    11 elseifkey[z] < key[y]

    12 thenleft[y]  z

    13elseright[y]  z

Hsiu-Hui Lee


Chapter 12

Inserting an item with key 13 into a binary search tree

Hsiu-Hui Lee


Chapter 12

Tree-Delete(T, z)

1 ifleft[z] = NILorright[z] = NIL

2 theny z

3 elsey Tree-Successor(z)

4 if left[y]  NIL

5thenx left[y]

6elsex right[y]

7 ifx NIL

  • thenp[x]  p[y]

    9 ifp[y] = NIL

    10 thenroot[T]  x

    11else ify = left[p[y]]

    12thenleft[p[y]]  x

    13elseright[p[y]]  x

    14 ify z

    15thenkey[z]  key[y]

    16 copy y’s satellite data into z

    17 returny

Hsiu-Hui Lee


Case a z has no children

Case a: z has no children

Hsiu-Hui Lee


Case b z has only one child

Case b: z has only one child

Hsiu-Hui Lee


Case c z has two children

Case c: z has two children

Hsiu-Hui Lee


Theorem 12 3

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.

Hsiu-Hui Lee


  • Login