-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoraha_BinaryTree.h
More file actions
134 lines (116 loc) · 4.13 KB
/
Loraha_BinaryTree.h
File metadata and controls
134 lines (116 loc) · 4.13 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
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
#ifndef LORAHA_BINARYTREE_H
#define LORAHA_BINARYTREE_H
#include <iostream>
using namespace std;
#include "Loraha_BinaryTree_Node.h"
template <typename T>
class MyBinaryTreeType{
private:
BSTNode<T>* root = nullptr;
int count = 0;
public:
void insert(T data){
if (root == nullptr){
root = new BSTNode<T>(data);
count++;
} else {
BSTNode<T>* current = root;
while (current){
if (data < current->data){ //insert into left
if (current->left == nullptr){
current->left = new BSTNode<T>(data);
current->left->previous = current;
count++;
return;
} else {
current = current->left; //so if there already is someone on left, move to left leaf and check again.
}
} else {
if (current->right == nullptr){
current->right = new BSTNode<T>(data);
current->right->previous = current;
count++;
return;
} else {
current = current->right; //so if there already is someone on right, move to right leaf and check again.
}
}
}
}
}
string find(T data){
if (root == nullptr){
return "False"; //means that the binary tree is empty.
} else {
BSTNode<T>* current = root;
while (current){
if (data == current->data){
return "True";
} else if (data < current->data){ //if true, set current to left one to check again.
current = current->left;
} else if (data > current->data){
current = current->right;
}
}
}
return "False";
}
int size(){
return count;
}
int* in_order_traversal(){
int* array = new int[count];
int i = 0;
in_order_traversal_helper(root, array, i);
return array;
}
void in_order_traversal_helper(BSTNode<T>* check, int* arr, int &j){
if (root->data){ //checks to see if the tree is empty
if(check->left){
in_order_traversal_helper(check->left, arr, j);
}
arr[j] = check->data;
j++;
if(check->right){
in_order_traversal_helper(check->right, arr, j);
}
}
}
int* pre_order_traversal(){
int* array = new int[count];
int i = 0;
pre_order_traversal_helper(root, array, i);
return array;
}
void pre_order_traversal_helper(BSTNode<T>* check, int* arr, int &j){
if (root->data){
arr[j] = check->data;
j++;
if(check->left){
pre_order_traversal_helper(check->left, arr, j);
}
if(check->right){
pre_order_traversal_helper(check->right, arr, j);
}
}
}
int* post_order_traversal(){
int* array = new int[count];
int i = 0;
post_order_traversal_helper(root, array, i);
return array;
}
void post_order_traversal_helper(BSTNode<T>* check, int* arr, int &j){
if (root->data){
if(check->left){
post_order_traversal_helper(check->left, arr, j);
}
if(check->right){
post_order_traversal_helper(check->right, arr, j);
}
arr[j] = check->data;
j++;
}
}
};
#endif