Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 Binary Search Trees

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

4 Example Binary Search Tree 20 10 6 28 15 40 30 25 Only keys are shown.

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

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

7 The Operation find(k) 20 10 6 28 15 40 30 25

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

9 The Operation insert() Insert a pair whose key is 7. 20 10 6 28 15 40 30 2535 7

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

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

12 The Operation erase() 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).

13 Erase From A Leaf Erase a leaf element. key = 7 20 10 6 28 15 40 30 2535 7 18

14 Erase From A Leaf (contd.) Erase a leaf element. key = 35 20 10 6 28 15 40 30 2535 7 18

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

16 Erase From A Degree 1 Node Erase from a degree 1 node. key = 15 20 10 6 28 15 40 30 2535 7 18

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

18 Erase From A Degree 1 Node(contd.) Erase from a degree 1 node. key = 40 20 10 6 28 15 40 30 2535 7 18

19 Remove node with two children If node n has two children, let x be node in n’s right subtree with smallest key, Remove x Replace n’s key with x’s key 20 10 40

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

21 Erase From A Degree 2 Node Erase from a degree 2 node. key = 10 20 10 6 28 15 40 30 2535 7 18

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

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

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

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

26 Another Erase From A Degree 2 Node Erase from a degree 2 node. key = 20 20 10 6 28 15 40 30 2535 7 18

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

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

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

30 Erase From A Degree 2 Node 18 10 6 28 15 40 30 2535 7

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

32

33 // 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 #ifndef bsTree_ #define bsTree_ #include "dictionary.h" using namespace std; template class bsTree : public dictionary { public: virtual void ascend() = 0; // output in ascending order of key }; #endif

34


Download ppt "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."

Similar presentations


Ads by Google