-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_queue_practice.c
More file actions
166 lines (125 loc) · 3.42 KB
/
binary_tree_queue_practice.c
File metadata and controls
166 lines (125 loc) · 3.42 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
#include <stdio.h>
#include <stdlib.h>
#include "queue.practice.h"
struct Node *root = NULL;
void createBinaryTree() {
struct Node *current, *temp;
int value;
struct Queue queue;
createQueue(&queue, 100);
printf("Enter a root value: ");
scanf("%d", &value);
root = (struct Node *)malloc(sizeof(struct Node));
root->value = value;
root->rightChild = NULL;
root->leftChild = NULL;
enqueue(&queue, root);
while (!isEmpty(&queue)) {
current = dequeue(&queue);
// left child
printf("Enter the left child of %d node: ", current->value);
scanf("%d", &value);
if (value != -1) {
temp = (struct Node *)malloc(sizeof(struct Node));
temp->value = value;
temp->rightChild = NULL;
temp->leftChild = NULL;
current->leftChild = temp;
enqueue(&queue, temp);
}
// right child
printf("Enter the right child of %d node: ", current->value);
scanf("%d", &value);
if (value != -1) {
temp = (struct Node *)malloc(sizeof(struct Node));
temp->value = value;
temp->rightChild = NULL;
temp->leftChild = NULL;
current->rightChild = temp;
enqueue(&queue, temp);
}
}
}
void preorder(struct Node *current) {
if (current != NULL) {
printf("%d", current->value);
preorder(current->leftChild);
preorder(current->rightChild);
}
}
void inorder(struct Node *current) {
if (current != NULL) {
inorder(current->leftChild);
printf("%d", current->value);
inorder(current->rightChild);
}
}
void postorder(struct Node *current) {
if (current != NULL) {
postorder(current->leftChild);
postorder(current->rightChild);
printf("%d", current->value);
}
}
void levelorder(struct Node *current) {
struct Queue queue;
createQueue(&queue, 100);
printf("%d", current->value);
enqueue(&queue, current);
while (!isEmpty(&queue)) {
current = dequeue(&queue);
if (current->leftChild != NULL) {
printf("%d", current->leftChild->value);
enqueue(&queue, current->leftChild);
}
if (current->rightChild != NULL) {
printf("%d", current->rightChild->value);
enqueue(&queue, current->rightChild);
}
}
}
int countNode(struct Node *current) {
if (current != NULL) {
return countNode(current->leftChild) + countNode(current->rightChild) + 1;
}
return 0;
}
// REVIEW THIS (confirm what the x and y are doing)
int getHeight(struct Node *current) {
int x = 0;
int y = 0;
if (current == 0) {
return 0;
}
x = getHeight(current->leftChild);
y = getHeight(current->rightChild);
if (x > y) {
return x + 1;
}
else {
return y + 1;
}
}
int main() {
createBinaryTree();
printf("\n");
printf("Preorder: ");
preorder(root);
printf("\n");
printf("Inorder: ");
inorder(root);
printf("\n");
printf("Postorder: ");
postorder(root);
printf("\n");
printf("Level Order: ");
levelorder(root);
printf("\n");
printf("\n");
printf("Number of Nodes: ");
printf("%d", countNode(root));
printf("\n");
printf("Height: ");
printf("%d", getHeight(root));
printf("\n");
}