-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathptree.c
104 lines (84 loc) · 2.2 KB
/
ptree.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "header.h"
/**
* ptree_new_node - addes a new node into the tree
* @parent: pointer to the parent node
*
* Return: pointer to the new child node
*/
ptree_t *ptree_new_node(ptree_t *parent)
{
ptree_t *node = safe_malloc(sizeof(ptree_t));
node->left = NULL;
node->right = NULL;
node->parent = parent;
node->strings = NULL;
node->stringsN = 0;
return (node);
}
/**
* ptree_new_string_node - inserts TOKEN_STRING type into a tree
* @parent: parent node
* @tokens: tokens_t struct
* @cur_token: current non-TOKEN_STRING token
*
* Return: pointer to the newly created child node or root node
*/
ptree_t *ptree_new_string_node(ptree_t *parent, tokens_t *tokens,
unsigned int *cur_token)
{
unsigned int i;
unsigned int start, finish;
ptree_t *node;
/* checks if the current token is a string or if we've run out of tokens */
if ((*cur_token >= tokens->tokensN) || (tokens->tokens[*cur_token].id !=
TOKEN_STRING))
return (NULL);
node = ptree_new_node(parent);
/* calculate number of strings after this token */
start = *cur_token;
while ((*cur_token < tokens->tokensN) && (tokens->tokens[*cur_token].id ==
TOKEN_STRING))
(*cur_token)++;
finish = *cur_token;
/* We now know how pointers to strings we need to malloc for */
/* Initialize everything else */
node->token_id = TOKEN_STRING;
node->stringsN = finish - start;
node->strings = safe_malloc((node->stringsN + 1) * sizeof(char *));
/* initialize stringsssssssss */
for (i = 0; i < node->stringsN; i++)
node->strings[i] = _strdup(tokens->tokens[i + start].str);
node->strings[i] = NULL;
return (node);
}
/**
* delete_ptree - cuts down the MEGA TREE
* @node: pointer to the root of the tree
*
* NOTE: On a recursive rollllll
* Return: 0
*/
int delete_ptree(ptree_t *node)
{
unsigned int i;
/* Base case: when node is NULL */
if (!node)
return (0);
/* Recussively delete child nodes */
delete_ptree(node->left);
delete_ptree(node->right);
/* free strings */
if (node->strings)
{
i = 0;
while (node->strings[i])
{
free(node->strings[i]);
i++;
}
free(node->strings);
}
/* delete input node (could be root node, could be some child node) */
free(node);
return (0);
}