-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.cpp
executable file
·76 lines (72 loc) · 1.67 KB
/
BinaryTree.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
#include <cstddef>
#include <iostream>
#include <stdexcept>
template <typename DataType>
class BinaryTree {
public:
BinaryTree() {
treeNodes = NULL;
sizee = 0;
}
DataType &operator[] (const int &index) {
if (index >= sizee)
throw std::invalid_argument("index out of bound");
return treeNodes[index];
}
void createWithSize (int newSize) {
sizee = newSize;
treeNodes = new DataType [sizee];
}
void createWithHeight (int height) {
sizee = (1 << height) - 1;
treeNodes = new DataType [sizee];
}
void getInput () {
for (int i = 0; i < sizee; ++i) {
std::cin >> treeNodes[i];
}
}
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) {
treeNodes[i] = assignee[i];
}
}
private:
DataType* treeNodes;
int sizee;
};
/*
void inOrderTraverse() {
bool* isPassed = new bool [sizee];
LinkedList<int> traverseStack;
traverseStack.push_front(0);
int root;
while(traverseStack.isEmpty() == false) {
root = traverseStack.begin()->getValue();
isPassed[root] = true;
if (2 * root + 1 < sizee and isPassed[2 * root + 1] == false) {
traverseStack.push_front(2 * root + 1);
} else {
if (treeNodes[root] != -1)
cout << treeNodes[root] << " ";
traverseStack.pop_front();
if (2 * root + 2 < sizee and isPassed[2 * root + 2] == false) {
traverseStack.push_front(2 * root + 2);
}
}
}
}/*