binary search trees definition of binary search tree a binary tree. each node has a (key, value)...
TRANSCRIPT
![Page 1: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/1.jpg)
Binary Search Trees
![Page 2: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/2.jpg)
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.
![Page 3: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/3.jpg)
Example Binary Search Tree20
10
6
2 8
15
40
30
25
Only keys are shown.
![Page 4: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/4.jpg)
ADT bsTree
AbstractDataType bsTree
{Instances
OperationsFind(k)
Insert(p)
Erase(k)
Ascend() // in-order tree traversal
}
![Page 5: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/5.jpg)
The Operation ascend()20
10
6
2 8
15
40
30
25
Do an inorder traversal.
![Page 6: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/6.jpg)
The Operation find(k)20
10
6
2 8
15
40
30
25
![Page 7: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/7.jpg)
The Operation insert()20
10
6
2 8
15
40
30
25
Insert a pair whose key is 35.
35
![Page 8: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/8.jpg)
The Operation insert()
Insert a pair whose key is 7.
20
10
6
2 8
15
40
30
25 35
7
![Page 9: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/9.jpg)
The Operation insert()20
10
6
2 8
15
40
30
25
Insert a pair whose key is 18.
35
7
18
![Page 10: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/10.jpg)
The Operation insert()20
10
6
2 8
15
40
30
25
Complexity of insert() is O(height).
35
7
18
![Page 11: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/11.jpg)
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).
![Page 12: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/12.jpg)
Erase From A Leaf
Erase a leaf element. key = 7
20
10
6
2 8
15
40
30
25 35
7
18
![Page 13: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/13.jpg)
Erase From A Leaf (contd.)
Erase a leaf element. key = 35
20
10
6
2 8
15
40
30
25 35
7
18
![Page 14: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/14.jpg)
Remove node with one child
• If node n has one child, move n’s child up to take n’s place.
![Page 15: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/15.jpg)
Erase From A Degree 1 Node
Erase from a degree 1 node. key = 15
20
10
6
2 8
15
40
30
25 35
7
18
![Page 16: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/16.jpg)
Erase From A Degree 1 Node (contd.)
Erase from a degree 1 node. key = 15
20
10
6
2 8
18
40
30
25 35
7
![Page 17: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/17.jpg)
Erase From A Degree 1 Node(contd.)
Erase from a degree 1 node. key = 40
20
10
6
2 8
15
40
30
25 35
7
18
![Page 18: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/19.jpg)
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
![Page 20: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/20.jpg)
Erase From A Degree 2 Node
Erase from a degree 2 node. key = 10
20
10
6
2 8
15
40
30
25 35
7
18
![Page 21: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/21.jpg)
Erase From A Degree 2 Node20
10
6
2 8
15
40
30
25
Replace with largest key in left subtree (or smallest in right subtree).
35
7
18
![Page 22: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/22.jpg)
Erase From A Degree 2 Node20
10
6
2 8
15
40
30
25
Replace with largest key in left subtree (or smallest in right subtree).
35
7
18
![Page 23: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/23.jpg)
Erase From A Degree 2 Node20
8
6
2 8
15
40
30
25
Replace with largest key in left subtree (or smallest in right subtree).
35
7
18
![Page 24: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/24.jpg)
Erase From A Degree 2 Node20
8
6
2 8
15
40
30
25
Largest key must be in a leaf or degree 1 node.
35
7
18
![Page 25: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/25.jpg)
Another Erase From A Degree 2 Node
Erase from a degree 2 node. key = 20
20
10
6
2 8
15
40
30
25 35
7
18
![Page 26: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/26.jpg)
Erase From A Degree 2 Node20
10
6
2 8
15
40
30
25
Replace with largest in left subtree.
35
7
18
![Page 27: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/27.jpg)
Erase From A Degree 2 Node20
10
6
2 8
15
40
30
25
Replace with largest in left subtree.
35
7
18
![Page 28: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/28.jpg)
Erase From A Degree 2 Node18
10
6
2 8
15
40
30
25
Replace with largest in left subtree.
35
7
18
![Page 29: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/29.jpg)
Erase From A Degree 2 Node18
10
6
2 8
15
40
30
25 35
7
![Page 30: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/30.jpg)
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.
![Page 31: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/31.jpg)
![Page 32: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/32.jpg)
// 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 K, class E>
class bsTree : public dictionary<K,E>
{
public:
virtual void ascend() = 0;
// output in ascending order of key
};
#endif
![Page 33: 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](https://reader036.vdocuments.us/reader036/viewer/2022062304/56649ce25503460f949ae0e4/html5/thumbnails/33.jpg)