1 / 22

Binary Search Trees

Binary Search Trees. BINARY SEARCH TREES. Important data structure for dynamic sets – dictionary or priority queue. Many BST operations are performed in O(h) where h is height of tree.

keugene
Télécharger la présentation

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. Binary Search Trees

  2. BINARY SEARCH TREES Important data structure for dynamic sets – dictionary or priority queue. Many BST operations are performed in O(h) where h is height of tree. Node in tree is a record that can have 5 fields : pointer to parent node, pointer to left/right child, key and data.

  3. Quick Review Root? Leaf? Interior Node? Parent/child? Sub-tree? This nearly complete BST is well balanced –> log n runtimes.

  4. Another well balanced example.

  5. An unbalanced BST giving linear runtimes.

  6. BINARY SEARCH TREE PROPERTY: 1. if y is left sub-tree of x then key[y] <= key[x]. 2. if y is right sub-tree of x then key[y] > key[x]. Pulling keys out of tree in sorted order requires an INORDER walk of the tree.

  7. InorderTreeWalk(x) //start at root if x != null then InorderTreeWalk(Left[x]) print(key[x]) InorderTreeWalk(Right[x]) Show sequence of output using example tree. ABDFHK. Do walk. Runtime of tree walk with an n-node BST ?

  8. Other operations : Searching and Insertion. Where is minimum? maximum? TreeSearch(x,k) //x starts at root, k is key if (x = null) or (k = key[x]) then return x else if k < key[x] then return TreeSearch(left[x],k) else return TreeSearch(right[x],k) Trace algorithm by searching for 4 and 10 in example tree. If k is not in tree then return nil.

  9. Insertion of x in a BST: code is similar to search code above. It is modified by placing x at a leaf in the proper sub-tree. The code uses a trailing back-pointer to keep track of parent of current node (same idea as inserting into a linked list). Trace algorithm by inserting 18 in tree. Runtime of Insertion = _____________?

  10. Do BST Activity in pairs

  11. BSTOPERATIONS Minimum (maximum) is a required operation for priority queues. BST as a priority queue implements minimum as: TreeMinimum(x) //x is pointer to root while left[x] != null then x = left[x] return x Runtime for tree minimum ?

  12. Successor operation retrieves successor node of x. Successor node of x is node with key that immediately follows the key of x, in sorted order. TreeSuccessor(x) if right[x] != null then {if right, suc. is min in subtree} return TreeMinimum(right[x]) y = parent[x] while(y!=null) and(x=right[y]) do {else suc. is back up x=y tree to left of parent} y =parent[y] return y Do an example trace of algorithm Runtime of TreeSuccessor = ________? Note that predecessor algorithm is analogous.

  13. Deletion operation removes a node from the tree. The following is a very informal pseudo code for operation. TreeDelete(T,x) if x has no children then {case 0} remove x if x has one child then {case 1} make parent[x] point to child remove x if x has two children then {case 2] swap x with its successor perform case 0 or 1 to delete it

  14. Do example trace for three cases of algorithm. See pseudo code next slide. Note: the above swap could also be done with predecessor. Runtime of Delete operation ?

  15. D or A

  16. Minimizing runtimes: • Problem: the worst case for operations on BST are ____________, which is no better than using a linked list. • Solution: guarantee that the BST is balanced which minimizes its height. Method: restructure tree when necessary. Nothing special required for query operations but extra work needed when tree is modified via an insert or delete operation.

  17. Refer to Pointer Example in BSTree Code – run code and discuss. This shows that passing a pointer by value and passing a pointer by reference have a subtle difference. Simple query operations on a BST can be accomplished by passing a pointer by value. Operations that modify the tree – insert, remove, rebalance, must use the pass a pointer by refernce mode.

  18. Create abstract memory map for BST. Root is node with 70. Root

  19. Assume we insert 31 starting with a pointer to root, *bst – add this to the map Root Left of root

  20. Summary • Examine class design for BST – on course web page • BST properties • algorithms and run-times

More Related