-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGROUP-D-19.cpp
More file actions
141 lines (135 loc) · 3.95 KB
/
GROUP-D-19.cpp
File metadata and controls
141 lines (135 loc) · 3.95 KB
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
/*
SAYALI RAHANE
CLASS- SE A
BATCH- C50
GROUP-D-19. A Dictionary stores keywords & its meanings. Provide facility for adding new keywords,
deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/
Descending order. Also find how many maximum comparisons may require for finding any keyword.
Use Height balance tree and find the complexity for finding a keyword.*/
#include <iostream> //Header Files
using namespace std;
struct HBTNode //Structure for HBTre Node
{
int Key;
char Mean[10];
HBTNode *left;
HBTNode *right;
} *Root;
void create_HBT() //Function to store keywords and meanings in HBT
{
int i;
int nodes;
int done;
struct HBTNode *Newnode, *current;
cout << "\n\n Enter the no of nodes to insert in HBT.? : ";
cin >> nodes;
for (i = 0; i < nodes; i++)
{
Newnode = new struct HBTNode; //memory allocation
cout << "\n\t Enter Keyword: "; //store keys
cin >> Newnode->Key;
cout << "\n\t Enter Meaning: "; //store meanings
cin >> Newnode->Mean;
Newnode->left = NULL; //left and right pointers initiall null
Newnode->right = NULL;
if (Root == NULL)
{
Root = Newnode;
}
else
{
done = 0;
current = Root;
while (!done)
{
if (Newnode->Key < current->Key)
{
if (current->left == NULL)
{
current->left = Newnode;
done = 1;
}
else
current = current->left;
}
else
{
if (current->right == NULL)
{
current->right = Newnode;
done = 1;
}
else
current = current->right;
}
} //while
} //else
} //for
} //function end
//Function to display keywords and meanings in HBT
void display_HBT(struct HBTNode *root)
{
if (root) //pre-order display
{
cout << "\n\t" << root->Key << " - " << root->Mean; //...Data
display_HBT(root->left); //...Left
display_HBT(root->right); //...Right
}
}
void Sorted_List(struct HBTNode *root)
{
if (root)
{
Sorted_List(root->left); //...Left
cout << "\n\t" << root->Key << " - " << root->Mean; //...Data
Sorted_List(root->right); //...Right
}
}
void Find_Keyword(int key)
{
int comp = 0;
int level = 0;
int done;
struct HBTNode *current;
done = 0;
current = Root;
while (!done)
{
if (key < current->Key)
{
current = current->left;
level++;
comp++;
}
else if (key > current->Key)
{
current = current->right;
level++;
comp++;
}
else
{
done = 1;
comp++;
cout << "\n\t Key : " << key;
cout << "\n\t Found at Level: " << level;
cout << "\n\t No. of Comparisons: " << comp;
}
}
}
int main()
{
cout << "\n -------***A C++ Program to Implement Dictionary using Height-Balanced Tree.* **-- -- -- -\n ";
cout<< "\n 1. Store Keywords and Meanings in Height-Balanced Tree.";
Root = NULL;
create_HBT();
cout << "\n 2. Display Keywords and Meanings in Height-Balanced Tree.";
cout << "\n Keyword - Meaning";
display_HBT(Root);
cout << "\n 3. Display a Sorted List of Keywords and Meanings.";
cout << "\n Keyword - Meaning";
Sorted_List(Root);
cout << "\n 4. Display Number of Comparisons required to find a keyword.";
Find_Keyword(1);
return 0;
}