-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeGraph.cpp
executable file
·73 lines (67 loc) · 1.46 KB
/
BinaryTreeGraph.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
#include <cstddef>
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename DataType>
class BinaryTree {
public:
BinaryTree() {
children = NULL;
weights = NULL;
sizee = 0;
}
int getLeft (const int &index) {
if (index >= sizee)
throw std::invalid_argument("index out of bound");
return children[index][0];
}
int getRight (const int &index) {
if (index >= sizee)
throw std::invalid_argument("index out of bound");
return children[index][1];
}
void getInput () {
cin >> sizee;
children = new int* [sizee];
for (int i = 0; i < sizee; ++i) {
children[i] = new int* [2];
children[i][0] = children[i][1] = -1;
}
int father;
for (int i = 1; i < sizee; ++i) {
cin >> father;
if (children[father][0] == -1)
children[father][0] = i;
else
children[father][1] = i;
}
weights = new int [sizee];
for (int i = 0; i < sizee; ++i)
cin >> weights[i];
return;
}
bool isEmpty() {
return ((sizee==0)? true : false);
;
}
bool isRoot (int index) {
return ((index==0)? true : false);
;
}
int size() {
return sizee;
;
}
void operator= (const BinaryTree<DataType> &assignee) {
sizee = assignee.size();
treeNodes = new DataType [sizee];
for (int i = 0; i < sizee; ++i) {
children[i][0] = assignee.getLeft(i);
children[i][1] = assignee.getRight(i);
}
}
private:
DataType* weights;
int** children;
int sizee;
};