1 / 23

12.Binary Search Trees

12.Binary Search Trees. Hsu, Lih-Hsing. 12.1 What is a binary search tree?. Binary-search property :

vartan
Download Presentation

12.Binary Search Trees

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 12.Binary Search Trees Hsu, Lih-Hsing

  2. 12.1 What is a 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. Binary search Tree

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

  5. Theorem 12.1 If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x) takes (n) time.

  6. Preorder tree walk • Postorder tree walk

  7. 12.2 Querying a binary search tree

  8. TREE_SEARCH(x,k) TREE_SEARCH(x,k) 1 ifor 2 then returnx 3 if 4 then return TREE_SEARCH(left[x],k) 5 else return TREE_SEARCH(right[x],k)

  9. ITERATIVE_SEARCH(x,k) ITERATIVE_SEARCH(x,k) 1 While or 2 do if 3 then 4 then 5 returnx

  10. MAXIMUM and MINIMUM • TREE_MINIMUM(x) 1 while left[x]  NIL 2 dox  left[x] 3 returnx • TREE_MAXIMUM(x) 1 while right[x]  NIL 2 dox  right[x] 3 returnx

  11. SUCCESSOR and PREDECESSOR

  12. TREE_SUCCESSOR TREE_SUCCESSOR 1 if 2 then returnTREE_MINIMUM(right[x]) 3 4 whileand 5 do 6 7 returny

  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.

  14. 12.3 Insertion and deletion

  15. Insertion Tree-Insert(T,z) 1 y  NIL 2 x root[T] 3 whilex NIL 4 doy x 5 ifkey[z] < key[x] 6 thenx left[x] 7 elsex right[x] 8 p[z]  y

  16. 9 ify = NIL 10 thenroot[T]  z tree T was empty 11 elseifkey[z] < key[y] 12 thenleft[y]  z 13 elseright[y]  z

  17. Inserting an item with key 13 into a binary search tree

  18. Deletion Tree-Delete(T,z) 1 ifleft[z] = NILorright[z] = NIL 2 theny z 3 elsey Tree-Successor(z) 4 if left[y]  NIL 5 thenx left[y] 6 elsex right[y] 7 ifx NIL 8 thenp[x]  p[y]

  19. 9 ifp[y] = NIL 10 thenroot[T]  x 11 else ify = left[p[y]] 12 thenleft[p[y]]  x 13 elseright[p[y]]  x 14 ify z 15 thenkey[z]  key[y] 16 copy y’s satellite data into z 17 returny

  20. z has no children

  21. z has only one child

  22. z has two children

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

More Related