-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
40 lines (35 loc) · 787 Bytes
/
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tree.h"
#include "node.h"
#include "token.h"
#include "treehelper.h"
node_t*
buildtree (token_t** tk, int n)
{
node_t* root = NULL;
root = insert(root, tk[0], 0);
int i;
for (i = 1; i < n; i++) {
insert(root, tk[i], 0);
}
return root;
}
node_t*
insert (node_t* root, token_t* tk, int level)
{
if (tk == (token_t*) NULL)
return (node_t*) NULL;
if (root == NULL) {
// initialize a new node
root = initializenode(root, level);
addwordtonode(root, tk);
} else {
// give birth
int n = root->num_children;
root->num_children++;
root->children[n] = insert(root->children[n], tk, level);
}
return root;
}