-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary.cpp
169 lines (150 loc) · 3.44 KB
/
Dictionary.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
164
165
166
167
168
169
#include <iostream>
#include <string>
using namespace std;
class Node
{
//public:
string word, meaning;
Node *left, *right; //Node * because node is storing the address of another Node
Node()
{
left = NULL;
right = NULL;
}
Node(string word, string meaning) //Parameterised constructor
{
this->word = word;
this->meaning = meaning;
left = NULL;
right = NULL;
}
friend class Dictionary; // means iska matlab Dictionary uska access le sakta hai
};
class Dictionary
{
Node *root;
public:
Dictionary()
{
root = NULL;
}
void create();
Node *insert(Node *, string,string);
void inorder(Node *);
void preorder(Node *);
void postorder(Node *);
void update();
void search(string,int);
void input();
void searchMain();
void inorderMain()
{
cout<<"\nInorder is: ";
inorder(root);
}
void preorderMain()
{
cout<<"\n\nPreorder is: ";
preorder(root);
}
void postorderMain()
{
cout<<"\n\nPostorder is: \n";
postorder(root);
}
};
void Dictionary::create()
{
string ipWord, ipMeaning;
int noOfNodes;
cout<<"Enter number of nodes: ";
cin>>noOfNodes;
for(int i=0;i<noOfNodes;i++)
{
cout<<"\n Enter Word: ";
cin>>ipWord;
cout<<"\n Enter Meaning: ";
cin>>ipMeaning;
root = insert(root, ipWord, ipMeaning);
cout<<"Root address: "<<root<<"\n";
}
//inorder(root);
}
Node *Dictionary::insert(Node *tempRoot, string tempWord, string tempMeaning)
{
Node *newNode = new Node(tempWord,tempMeaning);
if(tempRoot == NULL)
{
tempRoot = newNode;
cout<<tempRoot->word<<":"<<tempRoot->meaning;
return tempRoot;
}
if(tempWord < tempRoot->word)
tempRoot->left = insert(tempRoot->left, tempWord, tempMeaning);
else
tempRoot->right = insert(tempRoot->right, tempWord, tempMeaning);
return tempRoot;
}
void Dictionary::inorder(Node *inRoot)
{
if(inRoot!=NULL)
{
inorder(inRoot->left);
cout<<endl<<inRoot->word<<" "<<inRoot->meaning;
inorder(inRoot->right);
}
}
void Dictionary::preorder(Node *preRoot)
{
if(preRoot!=NULL)
{
cout<<endl<<preRoot->word<<" "<<preRoot->meaning;
preorder(preRoot->left);
preorder(preRoot->right);
}
}
void Dictionary::postorder(Node *postRoot)
{
if(postRoot!=NULL)
{
postorder(postRoot->left);
postorder(postRoot->right);
cout<<postRoot->word<<" "<<postRoot->meaning<<endl;
}
}
// void Dictionary :: input(){
// string data;
// cout<<"Enter the data you want to search for = ";
// cin>>data;
// }
void Dictionary::searchMain(){
int depth = 0;
string data;
cout<<"Enter the data you want to search for";
cin>>data;
// Node *tempRoot = new Node;
Node *tempRoot = root;
while(tempRoot != NULL) {
depth++;
if(tempRoot->word == data) {
cout<<" Your Data was found at = "<<depth;
return;
} else if(tempRoot->word > data)
tempRoot = tempRoot->left;
else
tempRoot= tempRoot->right;
}
cout<<"\n item not found";
return;
}
int main()
{
Dictionary dict;
dict.create();
dict.inorderMain();
dict.preorderMain();
dict.postorderMain();
// dict.input();
dict.searchMain();
return 0;
}