-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinomialTree.h
189 lines (133 loc) · 4.58 KB
/
BinomialTree.h
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
#ifndef BINOMIAL_TREE_H
#define BINOMIAL_TREE_H
/**
* Contains an implementation of Binomial Heaps of integers.
*/
namespace integered {
/**
* A Binomial Tree.
*/
class Tree {
public:
// Constructors.
Tree();
Tree(int size);
Tree(int size, int value);
Tree(const Tree&);
// Destructor.
virtual ~Tree();
int getValue() const;
int getSize() const;
Tree* getChilderen() const;
void setValue(int value);
// Deep copy of tree. Needs the same size.
Tree& operator=(const Tree& tree);
// Check if the size, value and (recursively) childeren are equal.
bool operator==(const Tree& tree) const;
bool operator!=(const Tree& tree) const;
// The merge of two tree of the same size.
friend const Tree operator+(const Tree& tree1, const Tree& Tree2);
// Debugging
void print() const;
void printr(int prefix) const;
private:
// The size of the tree. This is the order of the tree.
int size;
// The value of the root node.
int value;
// An array of the childeren of this Trees root.
Tree* childeren;
};
class Leaf : public Tree {
public:
Leaf();
Leaf(const int value);
};
}
namespace Template {
template<class T> class Tree;
template<class T>
const Tree<T> operator+(const Tree<T>& tree1, const Tree<T>& tree2);
template<class T> class Tree {
public:
Tree();
Tree(int size);
Tree(int size, T value);
Tree(const Tree<T>&);
virtual ~Tree();
T getValue() const;
int getSize() const;
Tree<T>* copyChilderen() const;
void setValue(T value);
Tree<T>& operator=(const Tree<T>& tree);
bool operator==(const Tree<T>& tree) const;
bool operator!=(const Tree<T>& tree) const;
friend const Tree<T> operator+ <> (const Tree<T>& tree1, const Tree<T>& tree2);
private:
int size;
int value;
Tree* childeren;
};
template<class T> class Leaf : public Tree<T> {
public:
Leaf();
Leaf(const T value);
};
template<class T> Tree<T>::Tree() : size(0) {}
template<class T> Leaf<T>::Leaf() : Tree<T>() {}
template<class T> Leaf<T>::Leaf(const T value) : Tree<T>(0, value) {}
template<class T> Tree<T>::Tree(int size) : size(size) {
if(size != 0) childeren = new Tree[size];
}
template<class T>
Tree<T>::Tree(int size, T value) : size(size), value(value) {
if(size != 0) childeren = new Tree[size];
}
template<class T>
Tree<T>::Tree(const Tree<T>& tree) : size(tree.size), value(tree.value) {
if(size != 0) childeren = new Tree[size];
for(int i = 0; i < size; i++) childeren[i] = tree.childeren[i];
}
template<class T> Tree<T>::~Tree() {
if(size > 0) delete[] childeren;
}
template<class T> T Tree<T>::getValue() const { return value; }
template<class T> int Tree<T>::getSize() const { return size; }
template<class T> void Tree<T>::setValue(T value) { this->value = value; }
template<class T> Tree<T>* Tree<T>::copyChilderen() const {
Tree* copy = new Tree[size];
for(int i = 0; i < size; i++) copy[i] = childeren[i];
return copy;
}
template<class T> Tree<T>& Tree<T>::operator=(const Tree<T>& tree) {
if(size > 0) delete[] childeren;
size = tree.size;
value = tree.value;
if(size > 0) childeren = tree.copyChilderen();
return (*this);
}
template<class T> bool Tree<T>::operator==(const Tree<T>& tree) const {
if(size != tree.size || value != tree.value) return false;
int i = 0;
while(childeren[i] == tree.childeren[i]) i++;
return i == size;
}
template<class T> bool Tree<T>::operator!=(const Tree<T>& tree) const {
return ! operator==(tree);
}
template<class T> const Tree<T> operator+(const Tree<T>& tree1,
const Tree<T>& tree2) {
if(tree1.size != tree2.size)
throw "Operation + only for Trees of equal size.";
int size = tree1.size;
Tree<T> larger(size), smaller(size);
if(tree1.value < tree2.value) { smaller = tree1; larger = tree2; }
else { smaller = tree2; larger = tree1; }
Tree<T> temp(size + 1, smaller.value);
temp.childeren[0] = larger;
for(int i = 0; i < size; i++)
temp.childeren[i+1] = smaller.childeren[i];
return (temp);
}
}
#endif