This repository was archived by the owner on Dec 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.c
More file actions
83 lines (66 loc) · 1.67 KB
/
binaryTree.c
File metadata and controls
83 lines (66 loc) · 1.67 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
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "binaryTree.h"
binary_tree* new_binary_tree(binary_tree *left, binary_tree *right, void *data) {
binary_tree *tree = malloc(sizeof(binary_tree));
assert(tree != NULL);
tree->left = left;
tree->right = right;
tree->data = data;
return tree;
}
binary_tree* getLeft(binary_tree *tree) {
return tree->left;
}
binary_tree* getRight(binary_tree *tree) {
return tree->right;
}
void* getData(binary_tree *tree) {
return tree->data;
}
void traversePreorder(binary_tree *tree, void (*function)(void *data)) {
function(tree->data);
if (tree->left != NULL) {
traversePreorder(tree->left, function);
}
if (tree->right != NULL) {
traversePreorder(tree->right, function);
}
}
void traverseInorder(binary_tree *tree, void (*function)(void *data)) {
if (tree->left != NULL) {
traverseInorder(tree->left, function);
}
function(tree->data);
if (tree->right != NULL) {
traverseInorder(tree->right, function);
}
}
void traversePostorder(binary_tree *tree, void (*function)(void *data)) {
if (tree->left != NULL) {
traversePostorder(tree->left, function);
}
if (tree->right != NULL) {
traversePostorder(tree->right, function);
}
function(tree->data);
}
bool setData(binary_tree *tree, void *data) {
assert(tree != NULL);
bool val = data != NULL;
tree->data = data;
return val;
}
bool setLeft(binary_tree *tree, binary_tree* l) {
assert(tree != NULL);
bool val = tree->left != NULL;
tree->left = l;
return val;
}
bool setRight(binary_tree *tree, binary_tree* r) {
assert(tree != NULL);
bool val = tree->right != NULL;
tree->right = r;
return val;
}