-
Notifications
You must be signed in to change notification settings - Fork 4
/
tree.c
69 lines (59 loc) · 1.33 KB
/
tree.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "popc.h"
#include "linkedList.h"
#include "tree.h"
void treeInsertNode(treeNode ptr2d root, linkedListCompareDelegate compare, object ptr obj) {
treeNode ptr cell = (treeNode ptr) malloc (sizeof (treeNode));
dbcEnsure (cell != NULL, "Memory Allocation Error!");
cell->obj = obj;
cell->left = NULL;
cell->right = NULL;
if (*root == NULL) {
*root = cell;
return;
}
while (1) {
//if (compare((*root)->data, data) > 0) {
if (compare((*root)->obj, obj) > 0) {
if ((*root)->left != NULL) {
*root = (*root)->left;
}
else {
(*root)->left = cell;
break;
}
}
else {
if ((*root)->right != NULL) {
*root = (*root)->right;
}
else {
(*root)->right = cell;
break;
}
}
}
}
void inOrder(treeNode ptr root, linkedListDisplayDelegate display) {
if (root != NULL) {
inOrder(root->left, display);
display(root->obj);
inOrder(root->right, display);
}
}
void postOrder(treeNode ptr root, linkedListDisplayDelegate display) {
if (root != NULL) {
postOrder(root->left, display);
postOrder(root->right, display);
display(root->obj);
}
}
void preOrder(treeNode ptr root, linkedListDisplayDelegate display) {
if (root != NULL) {
display(root->obj);
preOrder(root->left, display);
preOrder(root->right, display);
}
}