1 / 33

Binary Search Trees

Binary Search Trees. Definition Of Binary Search Tree. A binary tree. Each node has a (key, value) pair. For every node x , all keys in the left subtree of x are smaller than that in x . For every node x , all keys in the right subtree of x are greater than that in x. 20.

chin
Download Presentation

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. Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree of x are smaller than that in x. For every node x, all keys in the right subtree of x are greater than that in x.

  3. 20 Example Binary Search Tree 10 40 6 15 30 25 2 8 Only keys are shown.

  4. ADT bsTree AbstractDataTypebsTree { Instances Operations Find(k) Insert(p) Erase(k) Ascend() // in-order tree traversal }

  5. 20 10 40 6 15 30 25 2 8 The Operation ascend() Do an inorder traversal.

  6. 20 10 40 6 15 30 25 2 8 The Operation find(k)

  7. 20 10 40 6 15 30 25 2 8 The Operation insert() 35 Insert a pair whose key is 35.

  8. 20 10 40 6 15 30 25 35 2 8 The Operation insert() 7 Insert a pair whose key is 7.

  9. 20 The Operation insert() 10 40 6 15 30 18 25 35 2 8 7 Insert a pair whose key is 18.

  10. 20 The Operation insert() 10 40 6 15 30 18 25 35 2 8 7 Complexity of insert() is O(height).

  11. Three cases: • Element is in a leaf. • Element is in a degree 1 node (has one child). • Element is in a degree 2 node (has two children). The Operation erase()

  12. 20 10 40 6 15 30 18 25 35 2 8 7 Erase From A Leaf Erase a leaf element. key = 7

  13. 20 10 40 6 15 30 18 25 35 2 8 7 Erase From A Leaf (contd.) Erase a leaf element. key = 35

  14. Remove node with one child • If node n has one child, move n’s child up to take n’s place.

  15. 20 10 40 6 15 30 18 25 35 2 8 7 Erase From A Degree 1 Node Erase from a degree 1 node. key = 15

  16. 20 10 40 6 18 30 25 35 2 8 7 Erase From A Degree 1 Node (contd.) Erase from a degree 1 node. key = 15

  17. 20 10 40 6 15 30 18 25 35 2 8 7 Erase From A Degree 1 Node(contd.) Erase from a degree 1 node. key = 40

  18. Remove node with two children • If node n has two children, let x be node in n’sright subtree with smallest key, • Remove x • Replace n’s key with x’s key 20 10 40

  19. Remove node with two children • If node n has two children, let x be node in n’sleft subtree with largest key, • Remove x • Replace n’s key with x’s key 20 10 40

  20. 20 10 40 6 15 30 18 25 35 2 8 7 Erase From A Degree 2 Node Erase from a degree 2 node. key = 10

  21. 20 Erase From A Degree 2 Node 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree(or smallest in right subtree).

  22. 20 Erase From A Degree 2 Node 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

  23. 20 Erase From A Degree 2 Node 8 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

  24. 20 Erase From A Degree 2 Node 8 40 6 15 30 18 25 35 2 8 7 Largest key must be in a leaf or degree 1 node.

  25. 20 10 40 6 15 30 18 25 35 2 8 7 Another Erase From A Degree 2 Node Erase from a degree 2 node. key = 20

  26. 20 Erase From A Degree 2 Node 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

  27. 20 Erase From A Degree 2 Node 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

  28. 18 Erase From A Degree 2 Node 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

  29. 18 Erase From A Degree 2 Node 10 40 6 15 30 25 35 2 8 7

  30. Exercise • Start with an empty binary search tree. • Insert the keys 4,12,8,16,6,18,24,2,14,3, draw the tree following each insert. • From the tree above, delete the keys, 6,14,16,4 in order, draw the search tree following each deletion.

  31. // abstract class bsTree // abstract data type specification for binary search trees // all methods are pure virtual functions // K is key type and E is value type #ifndefbsTree_ #define bsTree_ #include "dictionary.h" using namespace std; template<class K, class E> class bsTree : public dictionary<K,E> { public: virtual void ascend() = 0; // output in ascending order of key }; #endif

More Related