-
Notifications
You must be signed in to change notification settings - Fork 0
/
bintree.template
133 lines (121 loc) · 3.18 KB
/
bintree.template
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
template <class Type>
void BinTree<Type>::add(Type dataIn){
// create new node for our data
BinNode<Type>* chair = new BinNode<Type>(dataIn);
if (isEmpty()){
root = chair; // first piece of data
return; // stop
}
BinNode<Type>* cursor = root; // pointer to go through the tree
while (cursor != nullptr){
if (dataIn <= cursor->data){ // LEFT SIDE
if (cursor->left == nullptr){ // NOT IN USE
cursor->left = chair; // ADDED NEW NODE TO TREE
cursor = nullptr; // RETURN
}
else{ // NOT AVAILABLE, MOVE DOWN BRANCH
cursor = cursor->left;
}
}
else{ // RIGHT SIDE
if (cursor->right == nullptr){ // NOT IN USE
cursor->right = chair; // ADDED NEW NODE TO TREE
cursor = nullptr; // RETURN
}
else{ // NOT AVAILABLE, MOVE DOWN BRANCH
cursor = cursor->right;
}
}
}
}
template <class Type>
bool BinTree<Type>::writeFile(std::string fileName) const{
std::ofstream fileObj;
BinNode<Type>* ptr = root;
fileObj.open(fileName);
if (!fileObj){
std::cout << "Error opening file\n";
return false;
}
writeFile(fileObj, ptr);
fileObj.close();
return true;
}
template <class Type>
void BinTree<Type>::writeFile(std::ofstream& ofs, BinNode<Type>* obj) const{
if (obj != nullptr){
ofs << obj->data;
writeFile(ofs, obj->left);
writeFile(ofs, obj->right);
}
}
template <class Type>
void BinTree<Type>::free(BinNode<Type>* cursor){
if (cursor != nullptr){ // stopping case
// post-order
free(cursor->left);
free(cursor->right);
delete cursor;
}
}
template <class Type>
bool BinTree<Type>::readFile(std::string fileName){
std::ifstream inFile_object;
inFile_object.open(fileName);
if (!inFile_object){
std::cout << "Error opening file\n";
return false;
}
Type data_to_read;
inFile_object >> data_to_read;
while (!inFile_object.eof()){
add(data_to_read);
inFile_object >> data_to_read;
}
inFile_object.close();
return true;
}
template <class Type>
void BinTree<Type>::printElement(BinNode<Type>* cursor){
if (cursor != nullptr){
std::cout << cursor->data << std::endl;
printElement(cursor->left);
printElement(cursor->right);
}
}
template <class Type>
void BinTree<Type>::inOrderTraversal(int count1, void process(Type , int , int& )){
int count2 = 0;
inOrder(root, count1, count2, process);
}
template <class Type>
void BinTree<Type>::inOrder(BinNode<Type>* cursor, int count1, int& count2, void process(Type , int , int& )){
if (cursor != nullptr){
inOrder(cursor->left, count1, count2, process);
count2++;
process(cursor->data, count1, count2);
inOrder(cursor->right, count1, count2, process);
}
}
template <class Type>
BinTree<Type>::BinTree(const BinTree<Type>& source){
root = nullptr;
copyTree(source.root);
}
template <class Type>
BinTree<Type>& BinTree<Type>::operator=(const BinTree<Type>& source){
if (this != &source){
free(root);
root = nullptr;
copyTree(source.root);
}
return *this;
}
template <class Type>
void BinTree<Type>::copyTree(BinNode<Type>* cursor){
if (cursor != nullptr){
add(cursor->data);
BinTree(cursor->left);
BinTree(cursor->right);
}
}