-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
89 lines (73 loc) · 1.54 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* This file is tree.c
* Binary Search Tree (BST) imlpementation
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
FILE *g_log; // g stands for global
int init_log(void)
{
g_log = fopen("log.txt", "w+");
if (NULL == g_log)
{
printf("Can't create log file\n");
return FALSE;
}
fprintf(g_log, "init_log()\n");
return TRUE;
}
void deinit_log(void)
{
fprintf(g_log, "deinit_log()\n");
fclose(g_log);
}
PTREE_NODE add_node(PTREE_NODE tree, int key)
{
fprintf(g_log, "add_node(%p, %d)\n", tree, key);
if (NULL == tree)
{
PTREE_NODE tmp = (PTREE_NODE)malloc(sizeof(TREE_NODE));
if (tmp == NULL)
return FALSE;
tmp->left = tmp->right = NULL;
tmp->key = key;
}
else if (tree->key < key)
tree->left = add_node(tree->left, key);
else
tree->right = add_node(tree->right, key);
return tree;
}
PTREE_NODE delete_node(PTREE_NODE tree, int key)
{
fprintf(g_log, "delete_node(%p, %d)\n", tree, key);
return tree;
}
void print_tree(PTREE_NODE tree)
{
fprintf(g_log, "print_tree(%p)\n", tree);
if (NULL == tree)
{
printf("Empty\n");
return;
}
if (tree->key)
printf("%d ", tree->key);
print_tree(tree->left);
print_tree(tree->right);
}
PTREE_NODE find(int key)
{
fprintf(g_log, "find(%d)\n", key);
return NULL; // not found
}
void destroy_tree(PTREE_NODE tree)
{
fprintf(g_log, "destroy_tree(%p)\n", tree);
}
int main()
{
return 0;
}