-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubtreeStack.cc
More file actions
71 lines (56 loc) · 1.31 KB
/
SubtreeStack.cc
File metadata and controls
71 lines (56 loc) · 1.31 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
#include "SubtreeStack.h"
#include "Node.h"
using namespace std;
SubtreeStack::Subtree::Subtree() {
data = nullptr;
next = nullptr;
}
SubtreeStack::Subtree::~Subtree() {
delete next;
}
SubtreeStack::SubtreeStack() {
size = 0;
head = nullptr;
}
SubtreeStack::~SubtreeStack() {
delete head;
}
unsigned int SubtreeStack::getSize() {
unsigned int returnSize = size;
return returnSize;
}
void SubtreeStack::push(Node *node) {
Subtree *newSubtree = new Subtree;
newSubtree->data = node;
if (head != nullptr) {
newSubtree->next = head;
}
head = newSubtree;
size += 1;
}
/* Removes two of the first subtrees and combines them into larger subtree
then pushes larger subtree back into stack */
void SubtreeStack::pop() {
Node *node1 = head->next->data;
Node *node2 = head->data;
Node *combined = new Node;
combined->set(node1->characters+node2->characters, 0, node1, node2);
size -= 2;
if (size == 0) {
head = nullptr;
} else {
head = head->next->next;
}
push(combined);
}
void SubtreeStack::print() {
if (head != nullptr) {
print(head);
}
}
void SubtreeStack::print(Subtree *startPoint) {
startPoint->data->print();
if (startPoint->next != nullptr) {
print(startPoint->next);
}
}