-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
191 lines (119 loc) · 3 KB
/
BinaryTree.cpp
File metadata and controls
191 lines (119 loc) · 3 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
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
190
191
#include <iostream>
#include <cstdlib>
#include "BinaryTree.h"
#include "Queue.h"
#define MAX_ITEMS 23
#include "Data.h"
using namespace std;
BTNode * createBTNode (Movie data) {
//creating a BT node for the Movie struct
BTNode * newNode;
newNode = new BTNode;
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
newNode->parent = NULL;
return newNode;
}
void preOrder (BTNode * root) {
if (root == NULL)
return;
cout << root->data.ID << " ";
preOrder (root->left);
preOrder (root->right);
}
void inOrder (BTNode * root) {
if (root == NULL)
return;
inOrder (root->left);
cout << root->data.ID << " ";
inOrder (root->right);
}
void postOrder (BTNode * root) {
if (root == NULL)
return;
postOrder (root->left);
postOrder (root->right);
cout << root->data.ID << " ";
}
int moment (BTNode * root) {
if (root == NULL)
return 0;
return (1 + moment (root->left) + moment (root->right));
}
int numOneChild (BTNode * root) {
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 0;
if (root->left == NULL)
return 1 + numOneChild (root->right);
else
if (root->right == NULL)
return 1 + numOneChild (root->left);
else
return (numOneChild (root->left) + numOneChild (root->right));
}
int numTerminal (BTNode * root) {
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
return (numTerminal (root->left) + numTerminal (root->right));
}
int nodeDepth (BTNode * node) {
int branch;
branch = 0;
if (node == NULL)
return -1;
while (node->parent != NULL) {
node = node->parent;
branch++;
}
return branch;
}
int height (BTNode * root) {
//finds the height of the tree from the root, go down
if(root==NULL){
return 0;
}
int heightLeft=height(root->left);
int heightRight=height(root->right);
return max(heightLeft, heightRight)+1;
}
void levelOrder (BTNode * root) {
if (root==NULL){
return;
}
Queue * q = initQueue(MAX_ELEMENTS); //initialize queue
int levelNum=0;
int numNodes;
enqueue(q,root); //enqueue root
while(!isEmptyQueue(q)){
//set the number of nodes in the level to the size of the queue from the iteration before when we used enqueue
numNodes=sizeQueue(q);
cout << numNodes << " nodes at Level " << levelNum << ": ";
for(int i=1; i<=numNodes; i++){
BTNode * p = dequeue (q); //dequeuing to display the nodes on a particular level
cout << p->data.ID << " ";
if(p->left!=NULL){
enqueue(q,p->left);
}
if(p->right!=NULL){
enqueue(q,p->right);
}
}
cout << endl;
levelNum++; //iterate the number of levels
}
}
void clearBT (BTNode * root) {
//recursively deletes a binary tree
if (root==NULL){
return;
}
clearBT(root->left);
clearBT(root->right);
delete root;
root=NULL;
}