-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11bst_LL.cpp
163 lines (146 loc) · 3.66 KB
/
11bst_LL.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 11. Implement Binary Search Tree ADT using Linked List.
// Theory:
// This code implements a binary search tree (BST) data structure
// with functionalities to insert elements into the tree, search for
// elements in the tree, and display the elements of the tree in
// sorted order. It utilizes a recursive approach for insertion and
// traversal. The main function offers a menu-driven interface for
// users to interact with the BST.
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* left;
Node* right;
};
class bst
{
private:
Node* root;
Node* createnode(int value)
{
Node* newNode = new Node;
newNode->data = value;
newNode->left = nullptr;
newNode->right = nullptr;
return newNode;
}
Node* insert(Node* root, int value)
{
if (root == nullptr)
{
return createnode(value);
}
if (value < root->data)
{
root->left = insert(root->left, value);
}
else if (value > root->data)
{
root->right = insert(root->right, value);
}
return root;
}
void traversal(Node* root)
{
if (root != nullptr)
{
traversal(root->left);
cout << root->data << " ";
traversal(root->right);
}
}
public:
bst()
{
root = nullptr;
}
void insert(int value)
{
root = insert(root, value);
}
bool search(int value)
{
Node* current = root;
while (current != nullptr)
{
if (value == current->data)
{
return true;
}
else if (value < current->data)
{
current = current->left;
}
else
{
current = current->right;
}
}
return false;
}
void display()
{
if (root == nullptr)
{
cout << "binary search tree is empty\n";
}
else
{
cout << "binary search tree: ";
traversal(root);
cout << endl;
}
}
};
int main()
{
bst bst;
char choice;
int value;
do
{
cout << "1. insert\n";
cout << "2. search\n";
cout << "3. display\n";
cout << "4. exit\n";
cout << "enter your choice: ";
cin >> choice;
switch(choice)
{
case '1':
cout << "enter element to insert: ";
cin >> value;
bst.insert(value);
break;
case '2':
cout << "enter element to search: ";
cin >> value;
if (bst.search(value))
{
cout << "found in binary search tree.\n";
} else {
cout << "not found in binary search tree.\n";
}
break;
case '3':
bst.display();
break;
case '4':
cout << "exited!\n";
break;
default:
cout << "invalid choice\n";
}
}
while(choice != '4');
return 0;
}
// Conclusion:
// The code provides a flexible implementation of a binary search
// tree, allowing users to insert elements, search for elements, and
// display the elements in sorted order. It utilizes a recursive
// approach for insertion and traversal, ensuring efficient operations
// on the tree. The provided menu interface makes it easy for users
// to perform operations on the binary search tree.