-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
150 lines (148 loc) · 5.72 KB
/
Source.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
#include"RBTree.h"
#include<fstream>
#include<iostream>
using namespace std;
void Menu();
void Readfile(RBTree<int>& obj);
int main()
{
int value = 0;
RBTree<int> obj;
cout << "////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n";
cout << " \n";
cout << " [ RED BLACK TREE ]\n";
cout << " \n";
cout << "////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n";
int choice = 0;
while (choice != 13)
{
Menu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "=======================================================================================================================\n\n";
obj.insert(1);
obj.insert(9);
obj.insert(10);
/*obj.insert(11);
obj.insert(8);
obj.insert(12);*/
/*obj.insert(10);
obj.insert(10);*/
cout << "Value is inserted...:)" << endl;
break;
case 2:
cout << "=======================================================================================================================\n\n";
int value;
cout << "Enter value: ";
cin >> value;
obj.SearchByValue(value);
cout << endl;
break;
case 3:
cout << "=======================================================================================================================\n\n";
cout << "Pre-order1 display(NLR):" << endl;
obj.preorder1();
cout << endl;
break;
case 4:
cout << "=======================================================================================================================\n\n";
cout << "Inorder1 display(LNR):" << endl;
obj.inorder1();
cout << endl;
break;
case 5:
cout << "=======================================================================================================================\n\n";
cout << "Post-order1 display(LRN):" << endl;
obj.postorder1();
cout << endl;
break;
case 6:
cout << "=======================================================================================================================\n\n";
cout << "Pre-order2 display(NRL):" << endl;
obj.preorder2();
cout << endl;
break;
case 7:
cout << "=======================================================================================================================\n\n";
cout << "Inorder2 display(RNL):" << endl;
obj.inorder2();
cout << endl;
break;
case 8:
cout << "=======================================================================================================================\n\n";
cout << "Post-order2 display(RLN):" << endl;
obj.postorder2();
cout << endl;
break;
case 9:
cout << "=======================================================================================================================\n\n";
cout << "Enter node to find parent: ";
cin >> value;
obj.FindParent(value);
cout << endl;
break;
case 10:
cout << "=======================================================================================================================\n\n";
Readfile(obj);
break;
case 11:
cout << "=======================================================================================================================\n\n";
cout << "this is not possible to delete dublicate values in Red Black becuase (in BST we can not insert dublicate values)." << endl;
break;
case 12:
cout << "=======================================================================================================================\n\n";
obj.DestroyallTree();
cout << "Tree destroyed..." << endl;
break;
case 13:
cout << "=======================================================================================================================\n";
cout << " Bye Bye 4th semester...:)" << endl;
cout << " Exiting..........." << endl;
cout << "=======================================================================================================================\n";
break;
default:
cout << "Invalid choice...!" << endl;
}
}
return 0;
}
void Menu()
{
cout << "=======================================================================================================================\n\n";
cout << "Press 1 to insert values in the tree(one by one)" << endl;
cout << "Press 2 for searching a value from the tree" << endl;
cout << "Press 3 for pre - order traversal NLR" << endl;
cout << "Press 4 for in - order traversal LNR" << endl;
cout << "Press 5 for post - order traversal LRN" << endl;
cout << "Press 6 for pre - order traversal 2 NRL" << endl;
cout << "Press 7 for in - order traversal 2 RNL" << endl;
cout << "Press 8 for post - order traversal 2 RLN" << endl;
cout << "Press 9 for displaying parent of a node present in Tree" << endl;
cout << "Press 10 to read integer values from the file “input.txt” to create a red - black tree" << endl;
cout << "Press 11 to delete all duplicate values from the tree" << endl;
cout << "Press 12 to destroy the complete tree" << endl;
cout << "Press 13 to EXIT" << endl;
}
void Readfile(RBTree<int>& obj)
{
ofstream fout;
ifstream fin;
int data;
fin.open("input.txt");
if (!fin.is_open())
{
cout << "Error in opening file";
}
else
{
while (!fin.eof())
{
fin >> data;
obj.insert(data);
}
cout << "Values from file insert successfully...:)" << endl;
}
}