-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceiling.cpp
More file actions
100 lines (91 loc) · 3.13 KB
/
ceiling.cpp
File metadata and controls
100 lines (91 loc) · 3.13 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
// This program takes in n positive integers. Inserts them into a binary
// search tree. It creates k such trees. Prints to screen how many different
// shapes are creted by k trees.
// Build binary trees using struct Node
// Use queue to do a level order treversal
// As the nodes are poped off, p r l n
//
#include <iostream>
#include <queue>
#include <string>
#include <set>
struct Node {
int value;
Node* leftP;
Node* rightP;
};
Node* ConstructNode(int);
void AddNode(int);
void LevelOrderTraversal();
Node* rootP;
std::set<std::string> globalSet;
int main(int argc, char const *argv[]) {
int numTrees, numLeaves, leafVal;
std::cin >> numTrees;
std::cin >> numLeaves;
// read all the trees
for(int i = 0; i<numTrees; i++) {
// build the tree
rootP = NULL;
for(int j = 0; j<numLeaves; j++) {
std::cin >> leafVal;
AddNode(leafVal);
}
LevelOrderTraversal();
}
std::cout << globalSet.size()<<'\n';
return 0;
}
Node* ConstructNode(int leafVal){
Node* tempP = new Node();
tempP->value = leafVal;
tempP->leftP = NULL;
tempP->rightP = NULL;
return tempP;
}
void AddNode(int leafVal){
if(rootP == NULL) {
rootP = ConstructNode(leafVal);
} else {
bool foundHome = false;
Node* parentP = rootP;
while(!foundHome) {
if(leafVal<parentP->value) {
if(parentP->leftP!=NULL) {
parentP = parentP->leftP;
} else {
parentP->leftP = ConstructNode(leafVal);
foundHome = true;
}
}else {
if(parentP->rightP!=NULL) {
parentP = parentP->rightP;
} else {
parentP->rightP = ConstructNode(leafVal);
foundHome = true;
}
}
}
}
}
void LevelOrderTraversal(){
std::queue<Node*> leafQueue;
std::string shape = "";
if(rootP!=NULL) {
leafQueue.push(rootP);
while(!leafQueue.empty()) {
shape = shape+"p";
Node* currentNodeP = leafQueue.front();
if(currentNodeP->leftP !=NULL) {
leafQueue.push(currentNodeP->leftP);
shape = shape+"l";
}
if(currentNodeP->rightP !=NULL) {
leafQueue.push(currentNodeP->rightP);
shape = shape+"r";
}
leafQueue.pop();
}
}
globalSet.insert(shape);
}