#include <iostream>
#include <cstdlib>
#include<cstring>
using namespace std;
struct tree {
string info;
tree *Left, *Right;
};
tree* root;
class Binary_tree {
public:
Binary_tree();
void insert1(string);
tree* search(string);
tree* insert2(tree*, tree*);
tree* search(string,tree*);
void Delete(string);
void pretrav(tree*);
void intrav(tree*);
void posttrav(tree*);
};
Binary_tree::Binary_tree()
{
root = NULL;
}
tree* Binary_tree::insert2(tree* temp, tree* newnode)
{
if (temp == NULL) {
temp = newnode;
}
else if (temp->info < newnode->info) {
insert2(temp->Right, newnode);
if (temp->Right == NULL)
temp->Right = newnode;
}
else {
insert2(temp->Left, newnode);
if (temp->Left == NULL)
temp->Left = newnode;
}
return temp;
}
void Binary_tree::insert1(string n)
{
tree *temp = root, *newnode;
newnode = new tree;
newnode->Left = NULL;
newnode->Right = NULL;
newnode->info = n;
root = insert2(temp, newnode);
}
tree *Binary_tree ::search(string key, tree *leaf){
if(leaf != NULL){
if(key == leaf->info){
cout<<"Element found"<<endl;
return leaf;
}
if(key < leaf->info){
return search(key, leaf->Left);
}else{
return search(key, leaf->Right);
}
}else{
return NULL;
}
}
tree *Binary_tree ::search(string key){
return search(key, root);
}
void Binary_tree::pretrav(tree* t = root)
{
if (root == NULL) {
cout << "Nothing to display";
}
else if (t != NULL) {
cout << t->info << " ";
pretrav(t->Left);
pretrav(t->Right);
}
}
void Binary_tree::intrav(tree* t = root)
{
if (root == NULL) {
cout << "Nothing to display";
}
else if (t != NULL) {
intrav(t->Left);
cout << t->info << " ";
intrav(t->Right);
}
}
void Binary_tree::posttrav(tree* t = root)
{
if (root == NULL) {
cout << "Nothing to display";
}
else if (t != NULL) {
posttrav(t->Left);
posttrav(t->Right);
cout << t->info << " ";
}
}
void Binary_tree::Delete(string key)
{
tree *temp = root, *parent = root, *marker;
if (temp == NULL)
cout << "The tree is empty" << endl;
else {
while (temp != NULL && temp->info != key) {
parent = temp;
if (temp->info < key) {
temp = temp->Right;
}
else {
temp = temp->Left;
}
}
}
marker = temp;
if (temp == NULL)
cout << "No node present";
else if (temp == root) {
if (temp->Right == NULL && temp->Left == NULL)
{
root = NULL;
}
else if (temp->Left == NULL) {
root = temp->Right;
}
else if (temp->Right == NULL) {
root = temp->Left;
}
else {
tree* temp1;
temp1 = temp->Right;
while (temp1->Left != NULL) {
temp = temp1;
temp1 = temp1->Left;
}
if (temp1 != temp->Right) {
temp->Left = temp1->Right;
temp1->Right = root->Right;
}
temp1->Left = root->Left;
root = temp1;
}
}
else {
if (temp->Right == NULL && temp->Left == NULL)
{
if (parent->Right == temp)
parent->Right = NULL;
else
parent->Left = NULL;
}
else if (temp->Left == NULL) {
if (parent->Right == temp)
parent->Right = temp->Right;
else
parent->Left = temp->Right;
}
else if (temp->Right == NULL) {
if (parent->Right == temp)
parent->Right = temp->Left;
else
parent->Left = temp->Left;
}
else {
tree* temp1;
parent = temp;
temp1 = temp->Right;
while (temp1->Left != NULL) {
parent = temp1;
temp1 = temp1->Left;
}
if (temp1 != temp->Right) {
temp->Left = temp1->Right;
temp1->Right = parent->Right;
}
temp1->Left = parent->Left;
parent = temp1;
}
}
delete marker;
}
int main()
{
Binary_tree bt;
int choice;
string n, key,s;
while (1) {
cout << "\n\t1. Insert\n\t2. Delete\n\t3. Preorder
Traversal\n\t4. Inorder Treversal\n\t5. Postorder Traversal\n\t6.
search\n\t7.Exit"<<endl;;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter item: ";
cin >> n;
bt.insert1(n);
break;
case 2:
cout << "Enter element to delete: ";
cin >> key;
bt.Delete(key);
break;
case 3:
cout << endl;
bt.pretrav();
break;
case 4:
cout << endl;
bt.intrav();
break;
case 5:
cout << endl;
bt.posttrav();
break;
case 6:
cout<<"enter the element to search\n";
cin>>s;
bt.search(key);
break;
case 7:
exit(0);
}
}
return 0;
}
4G LTE 11:30 PM binarytree.cpp 6 1 #include <iostream> 2 #include <cstdlib> 3 #include<cstring> 4 using namespace std; 5 struct tree { string info; 7 tree *Left, *Right; 8 }; 9 tree* root; 10 class Binary_tree { 11 public: 12 Binary_tree(); 13 void inserti(string); 14 tree* search(string); 15 tree* insert2(tree*, tree*); 16 tree* search(string, tree*); void Delete(string); 18 void pretrav(tree*); 19 void intrav(tree*); 20 void posttrav(tree*); 21 }; 22 Binary_tree::Binary_tree() 23 { 24 root = NULL; 25 ) 26 tree* Binary_tree::insert2(tree* temp, tree* newnode) 27 { 28 if (temp NULL) { 29 temp = newnode; 30 31 else if (temp->info < newnode->info) { 17 == } Q Run
4G LTE 11:30 PM binarytree.cpp 6 1 #include <iostream> 2 #include <cstdlib> 3 #include<cstring> 4 using namespace std; 5 struct tree { string info; 7 tree *Left, *Right; 8 }; 9 tree* root; 10 class Binary_tree { 11 public: 12 Binary_tree(); 13 void inserti(string); 14 tree* search(string); 15 tree* insert2(tree*, tree*); 16 tree* search(string, tree*); void Delete(string); 18 void pretrav(tree*); 19 void intrav(tree*); 20 void posttrav(tree*); 21 }; 22 Binary_tree::Binary_tree() 23 { 24 root = NULL; 25 ) 26 tree* Binary_tree::insert2(tree* temp, tree* newnode) 27 { 28 if (temp NULL) { 29 temp = newnode; 30 31 else if (temp->info < newnode->info) { 17 == } Q Run
4G LTE 11:30 PM == } } } binarytree.cpp 27 { 28 if (temp NULL) { 29 temp = newnode; 30 31 else if (temp->info < newnode->info) { 32 insert2(temp->Right, newnode); 33 if (temp->Right == NULL) 34 temp->Right = newnode; 35 36 else { 37 insert2(temp->Left, newnode); 38 if (temp->Left == NULL) 39 temp->Left = newnode; 40 41 return temp; 42 } 43 void Binary_tree::inserti(string n) 44 { 45 tree *temp = root, *newnode; 46 newnode = new tree; 47 newnode->Left = NULL; 48 newnode->Right = NULL; 49 newnode->info = n; 50 root = insert2(temp, newnode); 51 } 52 tree *Binary_tree :: Search(string key, tree * leaf) { 53 if(leaf != NULL) { 54 if(key == leaf->info) { 55 cout<<"Element found"<<endl; 56 return leaf; 57 58. Q Run
4G LTE 11:30 PM == } } } binarytree.cpp 27 { 28 if (temp NULL) { 29 temp = newnode; 30 31 else if (temp->info < newnode->info) { 32 insert2(temp->Right, newnode); 33 if (temp->Right == NULL) 34 temp->Right = newnode; 35 36 else { 37 insert2(temp->Left, newnode); 38 if (temp->Left == NULL) 39 temp->Left = newnode; 40 41 return temp; 42 } 43 void Binary_tree::inserti(string n) 44 { 45 tree *temp = root, *newnode; 46 newnode = new tree; 47 newnode->Left = NULL; 48 newnode->Right = NULL; 49 newnode->info = n; 50 root = insert2(temp, newnode); 51 } 52 tree *Binary_tree :: Search(string key, tree * leaf) { 53 if(leaf != NULL) { 54 if(key == leaf->info) { 55 cout<<"Element found"<<endl; 56 return leaf; 57 58. Q Run
4G LTE 11:30 PM 19 } 60 } } binarytree.cpp 57 58 59 if(key < leaf->info) { return search(key, leaf->Left); 61 }else{ 62 return search(key, leaf->Right); 63 64 }else{ 65 return NULL; 66 67 } 68 tree *Binary_tree ::search(string key) { 69 return search(key, root); 70 } 71 void Binary_tree::pretrav(tree* t = root) 72 { 73 if (root == NULL) { 74 cout << "Nothing to display"; 75 76 else if (t != NULL) { 77 cout << t->info << 78 pretrav(t->Left); 79 pretrav(t->Right); 80 81 } 82 void Binary_tree:: intrav(tree* t = root) 83 { 84 if (root NULL) { 85 cout << "Nothing to display"; 86 87 else if (t != NULL) { 88 intrav(t->Left); 89 cout << t->info << Q Run } II. بہ }
4G LTE 11:30 PM 19 } 60 } } binarytree.cpp 57 58 59 if(key < leaf->info) { return search(key, leaf->Left); 61 }else{ 62 return search(key, leaf->Right); 63 64 }else{ 65 return NULL; 66 67 } 68 tree *Binary_tree ::search(string key) { 69 return search(key, root); 70 } 71 void Binary_tree::pretrav(tree* t = root) 72 { 73 if (root == NULL) { 74 cout << "Nothing to display"; 75 76 else if (t != NULL) { 77 cout << t->info << 78 pretrav(t->Left); 79 pretrav(t->Right); 80 81 } 82 void Binary_tree:: intrav(tree* t = root) 83 { 84 if (root NULL) { 85 cout << "Nothing to display"; 86 87 else if (t != NULL) { 88 intrav(t->Left); 89 cout << t->info << Q Run } II. بہ }
4G LTE 11:30 PM } mm Il } binarytree.cpp 91 92 } 93 void Binary_tree::posttrav(tree* t = root) 94 { 95 if (root == NULL) { 96 cout << "Nothing to display"; 97 98 else if (t != NULL) { 99 posttrav(t->Left); 100 posttrav(t->Right); 101 cout << t->info << 102 103 } 104 void Binary_tree::Delete(string key) 105 { 106 tree *temp = root, *parent = root, *marker; 107 if (temp == NULL) 108 cout << "The tree is empty" << endl; 109 else { 110 while (temp != NULL && temp->info != key ) { 111 parent = temp; 112 if (temp->info < key) { 113 temp = temp->Right; 114 115 else { 116 temp = temp->Left; 117 118 119 120 marker = temp; 121 if (temp NULL) 122 cout << "No node present": Q Run } } } }
4G LTE 11:30 PM } mm Il } binarytree.cpp 91 92 } 93 void Binary_tree::posttrav(tree* t = root) 94 { 95 if (root == NULL) { 96 cout << "Nothing to display"; 97 98 else if (t != NULL) { 99 posttrav(t->Left); 100 posttrav(t->Right); 101 cout << t->info << 102 103 } 104 void Binary_tree::Delete(string key) 105 { 106 tree *temp = root, *parent = root, *marker; 107 if (temp == NULL) 108 cout << "The tree is empty" << endl; 109 else { 110 while (temp != NULL && temp->info != key ) { 111 parent = temp; 112 if (temp->info < key) { 113 temp = temp->Right; 114 115 else { 116 temp = temp->Left; 117 118 119 120 marker = temp; 121 if (temp NULL) 122 cout << "No node present": Q Run } } } }
4G LTE 11:31 PM binarytree.cpp nVue р состо 123 124 == else if (temp == root) { if (temp->Right == NULL && temp->Left NULL) { root = NULL; } else if (temp->Left == NULL) { root = temp->Right; } == NULL) { else if (temp->Right root = temp->Left; } 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 else { tree* templ; templ = temp->Right; while (templ->Left != NULL) { temp = templ; templ = temp1->Left; } if (templ != temp->Right) { temp->Left = temp1->Right; temp1->Right = root->Right; } templ->Left = root->Left; root = templ; } } - - else { if (temp->Right NULL && temp->Left NULL) { if (parent->Right temp) parent->Right = NULL; else == 150 151 152 Q Run
4G LTE 11:31 PM binarytree.cpp nVue р состо 123 124 == else if (temp == root) { if (temp->Right == NULL && temp->Left NULL) { root = NULL; } else if (temp->Left == NULL) { root = temp->Right; } == NULL) { else if (temp->Right root = temp->Left; } 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 else { tree* templ; templ = temp->Right; while (templ->Left != NULL) { temp = templ; templ = temp1->Left; } if (templ != temp->Right) { temp->Left = temp1->Right; temp1->Right = root->Right; } templ->Left = root->Left; root = templ; } } - - else { if (temp->Right NULL && temp->Left NULL) { if (parent->Right temp) parent->Right = NULL; else == 150 151 152 Q Run
4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Right temp) 157 parent->Right = temp->Right; 158 else 159 parent->Left = temp->Right; 160 161 else if (temp->Right NULL) { 162 if (parent->Right == temp) 163 parent->Right = temp->Left; 164 else 165 parent->Left = temp->Left; 166 167 else { 168 tree* templ; 169 parent = temp; 170 temp1 = temp->Right; 171 while (templ->Left != NULL) { 172 parent = templ; 173 templ = templ->Left; 174 175 if (templ != temp->Right) { 176 temp->Left = temp1->Right; 177 templ->Right = parent->Right; 178 179 temp1->Left = parent->Left; 180 parent = templ; 181 182 183 delete marker; 184 } 185 int main() } m } } Q Run
4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Right temp) 157 parent->Right = temp->Right; 158 else 159 parent->Left = temp->Right; 160 161 else if (temp->Right NULL) { 162 if (parent->Right == temp) 163 parent->Right = temp->Left; 164 else 165 parent->Left = temp->Left; 166 167 else { 168 tree* templ; 169 parent = temp; 170 temp1 = temp->Right; 171 while (templ->Left != NULL) { 172 parent = templ; 173 templ = templ->Left; 174 175 if (templ != temp->Right) { 176 temp->Left = temp1->Right; 177 templ->Right = parent->Right; 178 179 temp1->Left = parent->Left; 180 parent = templ; 181 182 183 delete marker; 184 } 185 int main() } m } } Q Run
4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Right temp) 157 parent->Right = temp->Right; 158 else 159 parent->Left = temp->Right; 160 161 else if (temp->Right NULL) { 162 if (parent->Right == temp) 163 parent->Right = temp->Left; 164 else 165 parent->Left = temp->Left; 166 167 else { 168 tree* templ; 169 parent = temp; 170 temp1 = temp->Right; 171 while (templ->Left != NULL) { 172 parent = templ; 173 templ = templ->Left; 174 175 if (templ != temp->Right) { 176 temp->Left = temp1->Right; 177 templ->Right = parent->Right; 178 179 temp1->Left = parent->Left; 180 parent = templ; 181 182 183 delete marker; 184 } 185 int main() } m } } Q Run
4G LTE 11:31 PM — } == } == } binarytree.cpp 153 parent-lent NULL; 154 155 else if (temp->Left == NULL) { 156 if (parent->Right temp) 157 parent->Right = temp->Right; 158 else 159 parent->Left = temp->Right; 160 161 else if (temp->Right NULL) { 162 if (parent->Right == temp) 163 parent->Right = temp->Left; 164 else 165 parent->Left = temp->Left; 166 167 else { 168 tree* templ; 169 parent = temp; 170 temp1 = temp->Right; 171 while (templ->Left != NULL) { 172 parent = templ; 173 templ = templ->Left; 174 175 if (templ != temp->Right) { 176 temp->Left = temp1->Right; 177 templ->Right = parent->Right; 178 179 temp1->Left = parent->Left; 180 parent = templ; 181 182 183 delete marker; 184 } 185 int main() } m } } Q Run
4G LTE 11:31 PM binarytree.cpp 185 int main() 186 { 187 Binary_tree bt; 188 189 int choice; 190 string n, key,s; 191 while (1) { 192 cout << "\n\ti. Insert\n\t2. Delete\n\t3 Preorder Traversal\n\t4. Inorder Treversal\n\t5. Postorder Traversal\n t6. search\n\t7.Exit"<<endl;; 193 cout << "Enter your choice: "; 194 cin >> choice; 195 switch (choice) { 196 case 1: 197 cout << "Enter item: "; 198 cin >> n; bt.inserti(n); 200 break; 201 case 2: 202 cout << "Enter element to delete: 203 cin >> key; 204 bt.Delete(key); 205 break; 206 case 3: 207 cout << endl; 208 bt.pretrav(); 209 break; 210 case 4: 211 cout << endl; 212 bt. intrav(); 213 break; 214 Q Run 199 casa 5.
4G LTE 11:31 PM binarytree.cpp 185 int main() 186 { 187 Binary_tree bt; 188 189 int choice; 190 string n, key,s; 191 while (1) { 192 cout << "\n\ti. Insert\n\t2. Delete\n\t3 Preorder Traversal\n\t4. Inorder Treversal\n\t5. Postorder Traversal\n t6. search\n\t7.Exit"<<endl;; 193 cout << "Enter your choice: "; 194 cin >> choice; 195 switch (choice) { 196 case 1: 197 cout << "Enter item: "; 198 cin >> n; bt.inserti(n); 200 break; 201 case 2: 202 cout << "Enter element to delete: 203 cin >> key; 204 bt.Delete(key); 205 break; 206 case 3: 207 cout << endl; 208 bt.pretrav(); 209 break; 210 case 4: 211 cout << endl; 212 bt. intrav(); 213 break; 214 Q Run 199 casa 5.
4G LTE 11:31 PM binarytree.cpp CUCC CTut) 212 213 214 215 216 217 218 219 bt.intrav(); break; case 5: cout << endl; bt.posttrav(); break; case 6: cout<<"enter the element to search\n "; cin>>s; bt.search(key); break; case 7: exit(0); 220 221 222 223 224 225 226 227 228 } 229 m } return 0; 6 Q Run
4G LTE 11:31 PM binarytree.cpp CUCC CTut) 212 213 214 215 216 217 218 219 bt.intrav(); break; case 5: cout << endl; bt.posttrav(); break; case 6: cout<<"enter the element to search\n "; cin>>s; bt.search(key); break; case 7: exit(0); 220 221 222 223 224 225 226 227 228 } 229 m } return 0; 6 Q Run
4G LTE 11:29 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 1 Enter item: kindness 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 1 Enter item: rascal 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 1 Enter item: structures 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal Q Run Again
4G LTE 11:29 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 1 Enter item: kindness 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 1 Enter item: rascal 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 1 Enter item: structures 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal Q Run Again
11:30 PM * @ 4G LTE binarytree 3. Preorder Traversat 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 4 boy bubba forest free freedom help hope ill jack ja mmers jimbo kelvin kindhearted kindman kindness k inny man mankind manoman manup mice rascal rascal s rock rockback rocknroll rooster structures unti l yahoo 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 3 kindness forest boy bubba jack help freedom free il i hope jammers kindman kelvin jimbo kindhearted r ascal man kinny mice manoman mankind manup struct ures rascals rock rocknroll rockback rooster unti l yahoo 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 6 enter the element to search kindness Q Run Again
11:30 PM * @ 4G LTE binarytree 3. Preorder Traversat 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 4 boy bubba forest free freedom help hope ill jack ja mmers jimbo kelvin kindhearted kindman kindness k inny man mankind manoman manup mice rascal rascal s rock rockback rocknroll rooster structures unti l yahoo 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 3 kindness forest boy bubba jack help freedom free il i hope jammers kindman kelvin jimbo kindhearted r ascal man kinny mice manoman mankind manup struct ures rascals rock rocknroll rockback rooster unti l yahoo 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 6 enter the element to search kindness Q Run Again
4G LTE 11:30 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 4 boy bubba forest free freedom help ill jack jammers jimbo kelvin kindhearted kindman kindness kinny man mankind manoman manup mice rascal rascals roc k rockback rocknroll rooster structures until 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 2 Enter element to delete: manoman 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 2 Enter element to delete: rascals 1. Insert 2. Delete 3. Preorder Traversal Q Run Again
4G LTE 11:30 PM binarytree 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 4 boy bubba forest free freedom help ill jack jammers jimbo kelvin kindhearted kindman kindness kinny man mankind manoman manup mice rascal rascals roc k rockback rocknroll rooster structures until 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 2 Enter element to delete: manoman 1. Insert 2. Delete 3. Preorder Traversal 4. Inorder Treversal 5. Postorder Traversal 6. search 7. Exit Enter your choice: 2 Enter element to delete: rascals 1. Insert 2. Delete 3. Preorder Traversal Q Run Again