-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.h
65 lines (47 loc) · 1.55 KB
/
tree.h
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
#ifndef _TREE_
#define _TREE_
//tree structure
typedef struct tree {
void* data;
struct tree* left;
struct tree* right;
} Tree;
/*set the data on the tree node */
void t_set_data(Tree* t, void* data) ;
/*let l be the left node of tree *t */
void t_set_left(Tree* t, Tree* l);
/*let r be the left node of tree *t */
void t_set_right(Tree* t, Tree* r);
/* simply return left node of the tree */
Tree* t_left(Tree* t);
/* simply return right node of the tree */
Tree* t_right(Tree* t);
/* simply return the data of the tree */
void* t_data(Tree* t);
/* make the node of the tree and allocate space for it*/
Tree* t_make();
/*
print the whole tree in the following format
Root is printed with zero trailing spaces to the left
Every node/subtree is printed with one additional space
Below is an example for a tree with depth 2:
Root
<space>Left-Child
<space><space>Left-Left-Child
<space><space>Left-Right-Child
<space>Right-Child
.... and so on ...
Use (void(* p))(function pointer) to print.
*/
void t_print( Tree* t ,int space, void(* p)(void*) );
/* Find a node using compare function pointer p to navigate; d is the data
to be found do f at the node. return d if found, otherwise return NULL
f is a function pointer which returns a void pointer.
Hint: You could use it to print the node or perform other functions.
*/
void* t_navigate( Tree* t , void* d, int(*p)(void*, void*), void*(*f)(void*));
/*
Frees the tree. Uses free_function to free the data stored in a node.
*/
void t_free(Tree *t, void (*free_function)(void *));
#endif