-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
142 lines (123 loc) · 3.51 KB
/
main.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
//main.cpp
//Michael Griffith
//Project 7
#include<iostream> //Used for standard out
#include"bst.h"
#include<vector> //Used for print
/*
* Utilized for printing both breadth and depth. Asks BST for a vector of the elements
* then runs through, printing them
*/
void printVec(std::vector<std::string>&);
int main()
{
std::string commandInput; //Used to interface with user
BST<std::string> tree; //Initialize the tree
while(std::cin.peek() != EOF) //Keep going while there is no end
{
std::cin >> commandInput;
//Echo Command
if(commandInput == "echo")
{
std::cin.ignore(); //That pesky whitespace
std::string output; //Stores what the user wants echo'd
getline(std::cin, output);
std::cout << output << std::endl;
}
//Insert Command
else if (commandInput == "insert")
{
std::cin.ignore(); //That pesky whitespace
std::string inputData;
getline(std::cin, inputData);
if(!tree.insert(inputData)) //Insert it
std::cerr << "insert <" << inputData << "> failed. String already in tree.\n";
}
//Size Command
else if (commandInput == "size")
std::cout << tree.size() << std::endl;
//Find Command
else if (commandInput == "find")
{
std::cin.ignore(); //That pesky whitespace
std::string inputData;
std::cin >> inputData;
std::cin.ignore(); //Set up for next cin
std::cout << "<" << inputData << "> is ";
if(!tree.find(inputData))
std::cout << "not ";
std::cout << "in tree.\n";
}
//Print Function (Depth First)
else if (commandInput == "print")
{
std::vector<std::string> stringVec;
std::cout << "{";
tree.print(stringVec); //Fill up the vec
printVec(stringVec); //Print out the vec
std::cout << "}\n";
std::cin.ignore();
}
//Print Function (Breadth First)
else if (commandInput == "breadth")
{
std::vector<std::string> stringVec;
std::cout << "{";
tree.breadth(stringVec); //Fill vector
printVec(stringVec); //Print vector
std::cout << "}\n";
std::cin.ignore();
}
//Distance Function
else if (commandInput == "distance")
{
std::cout << "Average distance of nodes to root = " << tree.distance() << std::endl;
std::cin.ignore();
}
//Balanced Function
//Balanced returns height if it is balanced, -1 if not balanced
else if (commandInput == "balanced")
{
std::cout << "Tree is ";
//Check if tree is balanced
if(tree.balanced() == -1)
std::cout << "not ";
std::cout << "balanced.\n";
std::cin.ignore();
}
//TODO
else if (commandInput == "rebalance")
{
tree.rebalance();
std::cin.ignore();
}
//Height command -- Added for debugging use
else if (commandInput == "height")
{
std::cout << tree.height() << std::endl;
std::cin.ignore();
}
//Clear command -- Added for debugging use
else if (commandInput == "clear")
{
tree.clear();
std::cin.ignore();
}
else
{
std::cerr << "Illegal command <" << commandInput << ">.\n";
std::string trash; //Gotta let the garbage man know
getline(std::cin, trash); //I've found some garbage.
}
}
return 0;
}
void printVec(std::vector<std::string> &stringVec)
{
for(unsigned int i = 0; i < stringVec.size(); i++)
{
std::cout << stringVec[i];
if(!(i==stringVec.size()-1))
std::cout << ", "; // Checks if it is the last thing printing out
}
}