-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment2.cpp
More file actions
206 lines (139 loc) · 4.96 KB
/
Assignment2.cpp
File metadata and controls
206 lines (139 loc) · 4.96 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
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
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>
#include "HashTable.h"
#include "BinaryTree.h"
#include "BinarySearchTree.h"
#include "Data.h"
using namespace std;
#define MAX_ITEMS 23
#define QUIT 99
int main () {
char fileName [25] = "Movies.txt";
char commandsFile [25] = "Commands.txt";
ifstream inputFile;
int command;
string movieString;
Movie movieRead;
HashTable * ht;
cout << "Creating hash table ... " << endl;
ht = initHashTableFromFile(MAX_ITEMS, fileName);
BTNode * moviesBST = initBSTFromFile(fileName);
cout << "Processing commands from commands file ..." << endl << endl;
ifstream inCommand;
inCommand.open(commandsFile);
if (!inCommand.is_open()) {
cout << "Commands file " << commandsFile << " could not be opened. Aborting ..." << endl;
exit(1);
}
inCommand >> command;
while(command!=QUIT){
cout << "COMMAND " << command << ":" << endl << endl;
if(command==10){
inCommand >> movieRead.ID >>movieRead.yearReleased >> movieRead.duration >> movieRead.genre;
inCommand >> ws;
getline (inCommand, movieRead.title);
displayMovie(movieRead);
BTNode * bstContains = containsBST(moviesBST,movieRead.ID );
if(containsHT(ht,movieRead.ID)==-1 && bstContains==NULL){ //checking if the movie is the HT and BST, inserting if it isn't
insertHT(ht, movieRead);
insertBST(moviesBST, movieRead);
cout << "--> Movie " << movieRead.ID << " inserted in hash table and BST." << endl;
}
else{
cout << "--> The movie is already in the hash table and BST" << endl;
}
}
if(command==11){
inCommand >> movieString;
int loc = containsHT(ht, movieString);
// if the movie is in the HT, display and find the length of the linked list it is stored in
if(loc!=-1){
cout << "--> The movie " << movieString << " is in the hash table." << endl << endl;
displayMovieHT(ht, movieString);
int lengthChainHT= lengthChain(ht, loc);
cout << "--> The length of the chain is " << lengthChainHT << endl << endl;
}
else{
cout << "The movie " << movieString << " is NOT in the hashtable." << endl;
}
//checking if it is in the BST by assigning it to a node and checking if it is null
BTNode * bstContains = containsBST(moviesBST, movieString);
if(bstContains!=NULL){
cout << "The movie " << movieString << " is in the BST." << endl << endl;
displayMovie(bstContains->data);
}
else{
cout << "The movie " << movieString << " is NOT in the BST." << endl;
}
}
if (command==12){
inCommand >> movieString;
//if the HT contains the movie, delete it
if(containsHT(ht, movieString)!=-1){
deleteHT(ht, movieString);
cout << "Movie " << movieString << " deleted from hash table." << endl;
}
else{
cout << "The movie " << movieString << " is NOT in the hashtable." << endl;
}
// same logic here, if the BST contains the movie, delete it
BTNode * bstContains = containsBST(moviesBST, movieString);
if(bstContains!=NULL){
deleteBST(moviesBST, movieString);
cout << "Movie " << movieString << " deleted from BST." << endl;
}
}
if(command==13){
// call to functions to display HT and BST statistics
statisticsHT(ht);
cout << endl;
statisticsBST(moviesBST);
cout << endl;
}
if(command==20){
// call to inOrder function
cout << "Inorder traversal of BST: " << endl << endl;
inOrder(moviesBST);
cout << endl;
}
if(command==21){
// call to level order function
cout << "Level order traversal of BST: " << endl << endl;
levelOrder(moviesBST);
cout << endl;
}
if(command==22){
string lowerID, upperID;
inCommand >> lowerID >> upperID;
cout << "Displaying all keys in the BST between " << lowerID << " and " << upperID << ": " << endl << endl;
rangeBST(moviesBST, lowerID, upperID);
cout << endl;
}
if(command==23){
string filename;
inCommand >> filename;
//filename is a string, convert to c-string then use it in the initBSTFromFile function to create the BST
char filenameChar[filename.length() + 1];
strcpy(filenameChar, filename.c_str());
BTNode * bstFile = initBSTFromFile(filenameChar);
// level order traversal
levelOrder(bstFile);
//checking if the bst from the file and the original BST we created are isomorphic
bool isomorphic = isIsomorphic (bstFile, moviesBST);
if (isomorphic==true){
cout << "--> BSTs are isomorphic." << endl;
}
else{
cout << "--> BSTs are NOT isomorphic." << endl;
}
//releasing memory from the BST created
clearBT(bstFile);
}
cout << endl << endl;
inCommand >> command;
}
inCommand.close();
return 0;
}