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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
#include <iostream>
#include <iomanip>
using namespace std;
class TNode
{
int data;
TNode *left, *right;
public:
TNode(int Data = 0, TNode *Left = NULL, TNode *Right = NULL) :data(Data), left(Left), right(Right)
{
}
friend class BinarySearchTree;
};
class BinarySearchTree
{
TNode *root;
void insert(int & x, TNode *&t)
{
if (t == nullptr)
{
t = new TNode{ x, nullptr, nullptr };
}
else if (x < t->data)
{
insert(x, t->left);
}
else if (t->data < x)
{
insert(x, t->right);
}
else
;
}
bool Search(int &x, TNode *&t)
{
if (t == nullptr)
return false;
else if (x < t->data)
return Search(x, t->left);
else if (t->data < x)
return Search(x, t->right);
else
return true;
}
void remove(int & x, TNode *&t)
{
if (t == nullptr)
return;
if (x < t->data)
{
remove(x, t->left);
}
else if (t->data < x)
{
remove(x, t->right);
}
else if (t->left != nullptr && t->right != nullptr)
{
t->data = findMin(t->right)->data;
remove(t->data, t->right);
}
else
{
TNode *oldNode = t;
t = (t->left != nullptr) ? t->left : t->right;
delete oldNode;
}
}
void makeEmpty(TNode * & t)
{
if (t != nullptr)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = nullptr;
}
TNode * findMin(TNode *t)
{
if (t == nullptr)
return nullptr;
if (t->left == nullptr)
return t;
return findMin(t->left);
}
TNode * findMax(TNode *t)
{
if (t != nullptr)
{
while (t->right != nullptr)
t = t->right;
}
return t;
}
void printTree(TNode *&t, int indent = 0)
{
if (t != NULL)
{
if (t->left) printTree(t->left, indent + 4);
if (indent)
{
std::cout << std::setw(indent) << ' ';
}
cout << " " << t->data << endl;
if (t->right) printTree(t->right, indent + 4);
}
}
public:
BinarySearchTree() :root(NULL)
{
}
BinarySearchTree(int D)
{
root->data = D;
root->left = NULL;
root->right = NULL;
}
~BinarySearchTree()
{
makeEmpty(root);
}
void insert(int d)
{
insert(d, root);
}
bool Search(int d)
{
return Search(d, root);
}
void remove(int & x)
{
remove(x, root);
}
void findMin()
{
TNode *tmp = findMin(root);
if (tmp == NULL)
{
cout << "Tree Is Empty " << endl;
}
else
{
cout << "the min value is : " << tmp->data << endl;
}
}
void findMax()
{
TNode *tmp = findMax(root);
if (tmp == NULL)
{
cout << "Tree Is Empty " << endl;
}
else
{
cout << "the mAx value is : " << tmp->data << endl;
}
}
void makeEmpty()
{
makeEmpty(root);
}
void PrintTree()
{
if (root == NULL)
{
cout << "tree is empty " << endl;
return;
}
printTree(root);
}
};
int main()
{
BinarySearchTree b;
int ch, tmp, tmp1;
while (1)
{
cout << endl << endl;
cout << " Binary Search Tree Operations " << endl;
cout << " ----------------------------- " << endl;
cout << " 1. Insertion/Creation " << endl;
cout << " 2. In-Order Traversal " << endl;
cout << " 3. Find Max " << endl;
cout << " 4. Find Min " << endl;
cout << " 5. Removal " << endl;
cout << " 6. search " << endl;
cout << " 7. Clear the tree " << endl;
cout << " 8. Exit " << endl;
cout << " Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1: cout << " Enter Number to be inserted : ";
cin >> tmp;
b.insert(tmp);
break;
case 2: cout << endl;
cout << " In-Order Traversal " << endl;
cout << " -------------------" << endl;
b.PrintTree();
break;
case 3: cout << endl;
cout << " MAX vALUE " << endl;
cout << " -------------------" << endl;
b.findMax();
break;
case 4: cout << endl;
cout << " Min Value " << endl;
cout << " --------------------" << endl;
b.findMin();
break;
case 5: cout << " Enter data to be deleted : ";
cin >> tmp1;
b.remove(tmp1);
break;
case 6: cout << " Enter data to be searched : ";
cin >> tmp1;
cout<<b.Search(tmp1);
break;
case 7: cout << "Tree cleared ";
b.makeEmpty();
case 8:
return 0;
}
}
return 0;
}
|