-
Notifications
You must be signed in to change notification settings - Fork 1
/
assign4.cpp
146 lines (128 loc) · 3.19 KB
/
assign4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Assignment No. 4
Beginning with an empty binary search tree, Construct binary searchtree by inserting the values
in the order given. After constructing a binary tree –
i. Insert new node
ii. Find number of nodes in longest path
iii. Minimum data value found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped atevery node
v. Search a value*/
#include <iostream>
using namespace std;
// Definition of a binary search tree node
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Function to insert a new node in a binary search tree
TreeNode *insert(TreeNode *root, int val)
{
if (root == NULL)
{
return new TreeNode(val);
}
if (val < root->val)
{
root->left = insert(root->left, val);
}
else
{
root->right = insert(root->right, val);
}
return root;
}
// Function to find the number of nodes in the longest path in a binary search tree
int longestPath(TreeNode *root)
{
if (root == NULL)
{
return 0;
}
return 1 + max(longestPath(root->left), longestPath(root->right));
}
// Function to find the minimum data value in a binary search tree
int minimum(TreeNode *root)
{
while (root->left != NULL)
{
root = root->left;
}
return root->val;
}
// Function to swap the left and right pointers at every node in a binary search tree
void swapLeftRight(TreeNode *root)
{
if (root == NULL)
{
return;
}
swap(root->left, root->right);
swapLeftRight(root->left);
swapLeftRight(root->right);
}
// Function to search for a value in a binary search tree
bool search(TreeNode *root, int val)
{
if (root == NULL)
{
return false;
}
if (root->val == val)
{
return true;
}
if (val < root->val)
{
return search(root->left, val);
}
else
{
return search(root->right, val);
}
}
// Function to print the inorder traversal of a binary search tree
void inorder(TreeNode *root)
{
if (root == NULL)
{
return;
}
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}
int main()
{
TreeNode *root = NULL;
int arr[] = {5, 3, 7, 1, 9, 4, 6, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
// Construct binary search tree by inserting the values in the given order
for (int i = 0; i < n; i++)
{
root = insert(root, arr[i]);
}
// Insert new node with value 10
root = insert(root, 10);
// Find number of nodes in longest path
cout << "Number of nodes in longest path: " << longestPath(root) << endl;
// Find minimum data value in the tree
cout << "Minimum data value: " << minimum(root) << endl;
// Swap the left and right pointers at every node
swapLeftRight(root);
// Search for a value
int val = 7;
if (search(root, val))
{
cout << val << " found in tree." << endl;
}
else
{
cout << val << " not found in tree." << endl;
}
// Print the inorder traversal of the binary search tree
inorder(root);
cout << "\nCode by Pranav Mehendale" << endl;
return 0;
}