1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and...
-
Upload
adrian-quinn -
Category
Documents
-
view
215 -
download
2
Transcript of 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and...
![Page 1: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/1.jpg)
1
Chapter 10Trees
![Page 2: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/2.jpg)
2
Definition of Tree
• A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root node) to every other node in the tree.
• A path exists from node A to node B if one can follow a chain of pointers to travel from node A to node B.
![Page 3: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/3.jpg)
3
A
E
F
B
C
D
G
A set of linked nodes
Paths
There is one path from A to B
There is a path from D to B
There is also a second path from D to B.
![Page 4: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/4.jpg)
4
A
E
F
B
C
D
G
There is no path from C to any other node.
Paths (cont.)
![Page 5: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/5.jpg)
5
Cycles
• There is no cycle (circle of pointers) in a tree.
• Any linked structure that has a cycle would have more than one path from the root node to another node.
![Page 6: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/6.jpg)
6
Example of a Cycle
A
C
D
B
E
C → D → B → E → C
![Page 7: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/7.jpg)
7
Tree Cannot Have a Cycle
A
C
D
B
E
2 paths exist from A to C:1. A → C2. A → C → D → B → E → C
![Page 8: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/8.jpg)
8
Example of a Tree
A
C B D
E F G
root
In a tree, every pair of linked nodes have a parent-child relationship (the parent is closer to the root)
![Page 9: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/9.jpg)
9
Example of a Tree(cont.)
A
C B D
E F G
root For example, C is a parent of G
![Page 10: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/10.jpg)
10
Example of a Tree(cont.)
A
C B D
E F G
root E and F are children of D
![Page 11: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/11.jpg)
11
Example of a Tree(cont.)
A
C B D
E F G
root The root node is the only node that has no parent.
![Page 12: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/12.jpg)
12
Example of a Tree(cont.)
A
C B D
E F G
rootLeaf nodes (or leaves for short) have no children.
![Page 13: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/13.jpg)
13
Subtrees
A
B C
I K D E F
J G H
subtree
root A subtree is a part of a tree that is a tree in itself
![Page 14: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/14.jpg)
14
Binary Trees
• A binary tree is a tree in which each node can only have up to two children…
![Page 15: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/15.jpg)
15
NOT a Binary Tree
A
B C
I K D E F
J G H
root C has 3 child nodes.
![Page 16: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/16.jpg)
16
Example of a Binary Tree
A
B C
I K E
J G H
root The links in a tree are often called edges
![Page 17: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/17.jpg)
17
Levels
A
B C
I K E
J G H
rootlevel 0
level 1
level 2
level 3
The level of a node is the number of edges in the path from the root node to this node
![Page 18: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/18.jpg)
18
Full Binary Tree
B
D
H
root
I
A
E
J K
C
F
L M
G
N O
In a full binary tree, each node has two children except for the nodes on the last level, which are leaf nodes
![Page 19: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/19.jpg)
19
Complete Binary Trees
• A complete binary tree is a binary tree that is either– a full binary tree– OR– a tree that would be a full binary tree but it is
missing the rightmost nodes on the last level
![Page 20: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/20.jpg)
20
NOT a Complete Binary Trees
B
D
H
root A
E
C
F G
I Missing non-rightmost nodes on the last level
![Page 21: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/21.jpg)
21
Complete Binary Trees(cont.)
B
D
H
root
I
A
E
J K
C
F
L
G
Missing rightmost nodes on the last level
![Page 22: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/22.jpg)
22
Complete Binary Trees(cont.)
B
D
H
root
I
A
E
J K
C
F
L M
G
N O
A full binary tree is also a complete binary tree.
![Page 23: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/23.jpg)
23
Binary Search Trees
• A binary search tree is a binary tree that allows us to search for values that can be anywhere in the tree.
• Usually, we search for a certain key value, and once we find the node that contains it, we retrieve the rest of the info at that node.
• Therefore, we assume that all values searched for in a binary search tree are distinct.
![Page 24: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/24.jpg)
24
Properties of Binary Search Trees
• A binary search tree does not have to be a complete binary tree.
• For any particular node, – the key in its left child (if any) is less than its
key.– the key in its right child (if any) is greater than
or equal to its key.• Left < Parent <= Right.
![Page 25: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/25.jpg)
25
Binary Search Tree (BST)Node
template <typename T>BSTNode {
T info;BSTNode<T> *left;BSTNode<T> *right;
};
The implementation of a binary search tree usually just maintains a single pointer in the private section called root, to point to the root node.
![Page 26: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/26.jpg)
26
Inserting Nodes Into a BST
37, 2, 45, 48, 41, 29, 20, 30, 49, 7
Objects that need to be inserted (only key values are shown):
root: NULL
BST starts off empty
![Page 27: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/27.jpg)
27
Inserting Nodes Into a BST (cont.)
37, 2, 45, 48, 41, 29, 20, 30, 49, 7
root 37
![Page 28: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/28.jpg)
28
Inserting Nodes Into a BST (cont.)
2, 45, 48, 41, 29, 20, 30, 49, 7
root 37
2 < 37, so insert 2 on the left side of 37
![Page 29: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/29.jpg)
29
Inserting Nodes Into a BST (cont.)
2, 45, 48, 41, 29, 20, 30, 49, 7
root 37
2
![Page 30: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/30.jpg)
30
Inserting Nodes Into a BST (cont.)
45, 48, 41, 29, 20, 30, 49, 7
root 37
2
45 > 37, so insert it at the right of 37
![Page 31: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/31.jpg)
31
Inserting Nodes Into a BST (cont.)
45, 48, 41, 29, 20, 30, 49, 7
root 37
2 45
![Page 32: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/32.jpg)
32
Inserting Nodes Into a BST (cont.)
48, 41, 29, 20, 30, 49, 7
root 37
2 45
When comparing, we always start at the root node
48 > 37, so look to the right
![Page 33: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/33.jpg)
33
Inserting Nodes Into a BST (cont.)
48, 41, 29, 20, 30, 49, 7
root 37
2 45
This time, there is a node already to the right of the root node. We then compare 48 to this node
48 > 45, and 45 has no right child, so we insert 48 on the right of 45
![Page 34: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/34.jpg)
34
Inserting Nodes Into a BST (cont.)
48, 41, 29, 20, 30, 49, 7
root 37
2 45
48
![Page 35: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/35.jpg)
35
Inserting Nodes Into a BST (cont.)
41, 29, 20, 30, 49, 7
root 37
2 45
4841 > 37, so look to the right
41 < 45, so look to the left – there is no left child, so insert
![Page 36: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/36.jpg)
36
Inserting Nodes Into a BST (cont.)
41, 29, 20, 30, 49, 7
root 37
2 45
4841
![Page 37: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/37.jpg)
37
Inserting Nodes Into a BST (cont.)
29, 20, 30, 49, 7
root 37
2 45
484129 < 37, left
29 > 2, right
![Page 38: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/38.jpg)
38
Inserting Nodes Into a BST (cont.)
29, 20, 30, 49, 7
root 37
2 45
484129
![Page 39: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/39.jpg)
39
Inserting Nodes Into a BST (cont.)
20, 30, 49, 7
root 37
2 45
48412920 < 37, left
20 > 2, right
20 < 29, left
![Page 40: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/40.jpg)
40
Inserting Nodes Into a BST (cont.)
20, 30, 49, 7
root 37
2 45
484129
20
![Page 41: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/41.jpg)
41
Inserting Nodes Into a BST (cont.)
30, 49, 7
root 37
2 45
484129
2030 < 37
30 > 2
30 > 29
![Page 42: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/42.jpg)
42
Inserting Nodes Into a BST (cont.)
30, 49, 7
root 37
2 45
484129
20 30
![Page 43: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/43.jpg)
43
Inserting Nodes Into a BST (cont.)
49, 7
root 37
2 45
484129
20 30
49 > 3749 > 4549 > 48
![Page 44: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/44.jpg)
44
Inserting Nodes Into a BST (cont.)
49, 7
root 37
2 45
484129
20 30 49
![Page 45: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/45.jpg)
45
Inserting Nodes Into a BST (cont.)
7
root 37
2 45
484129
20 30 49
7 < 377 > 27 < 297 < 20
![Page 46: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/46.jpg)
46
Inserting Nodes Into a BST (cont.)
7
root 37
2 45
484129
20 30 49
7
![Page 47: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/47.jpg)
47
Inserting Nodes Into a BST (cont.)
root 37
2 45
484129
20 30 49All elements have been inserted
7
![Page 48: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/48.jpg)
48
Searching for a Key in a BST
root 37
2 45
484129
20 30 49
Searching for a key in a BST uses the same logic
7Key to search for: 29
![Page 49: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/49.jpg)
49
Searching for a Key in a BST (cont.)root
37
2 45
484129
20 30 49
Key to search for: 29
29 < 37
7
![Page 50: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/50.jpg)
50
Searching for a Key in a BST (cont.)root
37
2 45
484129
20 30 49
Key to search for: 29
29 > 2
7
![Page 51: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/51.jpg)
51
Searching for a Key in a BST (cont.)root
37
2 45
484129
20 30 49
Key to search for: 29
29 == 29
FOUND IT!
7
![Page 52: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/52.jpg)
52
Searching for a Key in a BST (cont.)root
37
2 45
484129
20 30 49
Key to search for: 3 7
![Page 53: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/53.jpg)
53
Searching for a Key in a BST (cont.)root
37
2 45
484129
20 30 49
Key to search for: 3
3 < 7
7
3 < 20
3 < 29
3 > 2
3 < 37
![Page 54: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/54.jpg)
54
Searching for a Key in a BST (cont.)root
37
2 45
484129
20 30 49
Key to search for: 3
When the child pointer you want to follow is set to NULL, the key you are looking for is not in the BST
7
![Page 55: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/55.jpg)
55
Searching a BST is Quicker than Array or Linked List
root 37
2 45
484129
20 30 49
Search for a key in this BST takes max 5 comparisons (key = 7)
7
![Page 56: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/56.jpg)
56
Searching a BST is Quicker than Array or Linked List (cont.)
37, 2, 45, 48, 41, 29, 20, 30, 49, 7
Searching for a key in linear data structures, such as array or linked list, takes max 10 comparisons (key = 7). In fact searching for key 29, 20, 30, 49, and 7 all takes more than 5 comparisons. Overall slower than BST.
The main advantage of BST over linear data structures is quicker search.
![Page 57: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/57.jpg)
57
Deleting a BST Node
• Deleting a node in a BST is a little tricky – it has to be deleted so that the resulting structure is still a BST with each node greater than its left child and less than its right child.
• Deleting a node is handled differently depending on whether the node:– has no children– has one child– has two children
![Page 58: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/58.jpg)
58
Deletion Case 1: No Children
root 37
2 45
484129
20 30 49
Node 49 has no children – to delete it, we just remove it
![Page 59: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/59.jpg)
59
Deletion Case 1: No Children (cont.)
root 37
2 45
484129
20 30
![Page 60: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/60.jpg)
60
Deletion Case 2: One Child
root 37
2 45
484129
20 30 49
Node 48 has one child – to delete it, we just splice it out
![Page 61: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/61.jpg)
61
Deletion Case 2: One Child (cont.)
root 37
2 45
484129
20 30 49
Node 48 has one child – to delete it, we just splice it out
![Page 62: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/62.jpg)
62
Deletion Case 2: One Child (cont.)
root 37
2 45
4129
20 30 49
![Page 63: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/63.jpg)
63
Deletion Case 2: One Child (cont.)
root 37
2 45
484129
20 30 49
Another example: node 2 has one child – to delete it we also splice it out
![Page 64: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/64.jpg)
64
Deletion Case 2: One Child (cont.)
root 37
2 45
484129
20 30 49
Another example: node 2 has one child – to delete it we also splice it out
![Page 65: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/65.jpg)
65
Deletion Case 2: One Child (cont.)
root 37
45
484129
20 30 49
![Page 66: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/66.jpg)
66
Deletion Case 3: Two Children
root 37
2 45
484129
20 30 49
Node 37 has two children…
![Page 67: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/67.jpg)
67
Deletion Case 3: Two Children (cont.)root
37
2 45
484129
20 30 49
to delete it, first we find the greatest node in its left subtree
![Page 68: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/68.jpg)
68
Deletion Case 3: Two Children (cont.)root
37
2 45
484129
20 30 49
First, we go to the left once, then follow the right pointers as far as we can
![Page 69: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/69.jpg)
69
Deletion Case 3: Two Children (cont.)root
37
2 45
484129
20 30 4930 is the greatest node in the left subtree of node 37
![Page 70: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/70.jpg)
70
Deletion Case 3: Two Children (cont.)root
37
2 45
484129
20 30 49Next, we copy the object at node 30 into node 37
![Page 71: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/71.jpg)
71
Deletion Case 3: Two Children (cont.)root
30
2 45
484129
20 49
Finally, we delete the lower red node using case 1 or case 2 deletion
![Page 72: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/72.jpg)
72
Deletion Case 3: Two Children (cont.)root
30
2 45
484129
20 49Let’s delete node 30 now
![Page 73: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/73.jpg)
73
Deletion Case 3: Two Children (cont.)root
30
2 45
484129
20 49
29 is the greatest node in the left subtree of node 30
![Page 74: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/74.jpg)
74
Deletion Case 3: Two Children (cont.)root
30
2 45
484129
20 49
Copy the object at node 29 into node 30
![Page 75: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/75.jpg)
75
Deletion Case 3: Two Children (cont.)root
29
2 45
484129
20 49
This time, the lower red node has a child – to delete it we use case 2 deletion
![Page 76: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/76.jpg)
76
Deletion Case 3: Two Children (cont.)root
29
2 45
484129
20 49
This time, the lower red node has a child – to delete it we use case 2 deletion
![Page 77: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/77.jpg)
77
Deletion Case 3: Two Children (cont.)root
29
2 45
4841
20 49
![Page 78: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/78.jpg)
78
Searching a BST is Quicker than Array or Linked List
root 37
2 45
484129
20 30 49
Search for a key in this BST takes max 5 comparisons (key = 7)
7
![Page 79: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/79.jpg)
: 37 2 29 20 7 30 45 41 48 49
Traversing a BST• There are 3 ways to traversal a BST (visit
every node in BST):– 1. Preorder (parent → left → right)– 2. Inorder (left → parent → right)– 3. Postorder (left → right → parent)
• Result of traversing BST (at pp. 55):– Preorder: – Inorder:– Postorder:
2 7 20 29 30 37 41 45 48 497 20 30 29 2 41 49 48 45 37
![Page 80: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/80.jpg)
80
1 template <typename T>2 class BinarySearchTree {3 BSTNode *root;4 void preOrderInternal (BST<T> *parent) { … }5 void inOrderInternal (BST<T> *parent) { … }6 void postOrderInternal (BST<T> *parent) { … }7 void insertInternal (BST<T> *parent, 8 T& newElement) { … }9 bool searchInternal (BST<T> *parent, T& target,10 T& foundElement) { … }
Binary Search Tree Class Template
![Page 81: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/81.jpg)
81
11 public:12 BinarySearchTree() { … }13 ~BinarySearchTree() { … }14 bool isEmpty() { … }15 void preOrderTraversal() { … }16 void inOrderTraversal() { … }17 void postOrderTraversal() { … }18 void insert (T element) { … }19 bool search (T element, T& foundElement) { … }20 void makeEmpty() { … }21 }
Binary Search Tree Class Template (cont.)
![Page 82: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/82.jpg)
82
1 BinarySearchTree()2 : root(NULL) { }34 ~BinarySearchTree()5 {6 makeEmpty();7 }8 9 bool isEmpty()10 {11 return root == NULL:12 }
Constructor, Destructor, IsEmpty()
![Page 83: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/83.jpg)
83
2 void inOrderTraversal()3 {4 inOrderInternal (root);5 }
The client uses the driver called InOrderTraversal.
Printing ElementsIn Order
The recursive function inOrderInternal is called. inOrderInternal prints all element in BST in sorted order, starting from root node.
![Page 84: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/84.jpg)
84
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
The base case occurs when parent is NULL – just returns.
Printing ElementsIn Order (cont.)
![Page 85: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/85.jpg)
85
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
There are 2 recursive calls under the if – the first advances the pointer to the left child…
and the second advances the pointer to the right child.
Printing ElementsIn Order (cont.)
![Page 86: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/86.jpg)
86
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
Each recursive call approaches the base case…
it goes down one level through the tree, and it get closer to the case where parent->left or parent->right is NULL.
Printing ElementsIn Order (cont.)
![Page 87: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/87.jpg)
87
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
If the BST contains only one node, the root node is the only node – the left and right pointers of the root node will be set to NULL.
Printing ElementsIn Order (cont.)
![Page 88: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/88.jpg)
88
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
parent->left is NULL, so NULL is passed into parent of the new inOrderInternal function.
Printing ElementsIn Order (cont.)
![Page 89: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/89.jpg)
89
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
Since parent is NULL in the new inOrderInternal function, it will return right away.
Printing ElementsIn Order (cont.)
![Page 90: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/90.jpg)
90
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
parent->right is NULL – thus, the recursive function call immediately returns as before.
Printing ElementsIn Order (cont.)
![Page 91: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/91.jpg)
91
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
Printing ElementsIn Order (cont.)
In fact inOrderInternal works correctly when there is only 0, 1 or many node in the BST.
![Page 92: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/92.jpg)
92
2 void inOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 inOrderInternal (parent->left);6 cout << parent->info << " ";7 inOrderInternal (parent->right);8 }9 }
Printing ElementsIn Order (cont.)
inOrderInternal is called once from the driver inOrderTraversal. Then, for each node in the tree, 2 calls to inOrderInternal are made, giving us a total of 2n + 1 calls where n is the number of nodes.
![Page 93: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/93.jpg)
93
2 void preOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 cout << parent->info << " ";6 preOrderInternal (parent->left);7 preOrderInternal (parent->right);8 }9 }
Printing ElementsPre Order
preOrderInternal prints the parent first.
![Page 94: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/94.jpg)
94
2 void postOrderInternal (BSTNode<T> *parent)3 {4 if (parent != NULL) {5 postOrderInternal (parent->left);6 postOrderInternal (parent->right);7 cout << parent->info << " ";8 }9 }
Printing ElementsPost Order
postOrderInternal prints the parent last.
![Page 95: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/95.jpg)
95
1 bool search (T target, T& foundElement) {2 return searchInternal (root, target, foundElement);3 }45 void insert ( T& newElement) {6 insertInternal (root, newElement);7 }
Searching and Insertion in BST
![Page 96: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/96.jpg)
96
Searching and Insertion in BST
• We can actually figure out searchInternal and insertInternal using the same recursive logic as traversal in BST.
![Page 97: 1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f175503460f94c2e234/html5/thumbnails/97.jpg)
References
• Childs, J. S. (2008). Trees. C++ Classes and Data Structures. Prentice Hall.
97