-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTree.c
202 lines (165 loc) · 5.46 KB
/
binaryTree.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright 2012-2014 Infinitycoding all rights reserved
This file is part of the Ultrashell.
The Ultrashell is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
The Ultrashell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Ultrashell. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Peter Hösch aka. BlitzBasic <[email protected]>
**/
#include <binaryTree.h>
#include <stdlib.h>
#include <string.h>
/**
* @brief deletes a binary tree
* @param tree the tree to delete
* @return no return value
*/
void deleteTree(binary_tree *tree)
{
deleteBranch((struct branch *)tree);
}
/**
* @brief deletes a branch of a binary tree and all it's subbranches
* @param b the branch which should be deleted
* @return no return value
*/
void deleteBranch(struct branch *b)
{
if(b->type == branchEnd)
{
free(b->element->f->command);
free(b->element->f);
}
else
{
deleteBranch(b->element->b->left);
deleteBranch(b->element->b->right);
free(b->element->b->c);
free(b->element->b);
}
free(b->element);
free(b);
}
/**
* @brief creates a function struct in the ram and adds the function to a binary tree
* @param tree the tree in which the new function should be added
* @param cmd the command with which the function should be called later on
* @param fct a pointer to the function which should be added to the tree
* @return 0 = success
* @return -1 = binary tree corrupted
* @return -2 = command allready added
* @return -3 = allocate error
*/
int loadFunction(binary_tree **tree, char *cmd, int (*fct)(int, char **))
{
struct function *newFunction = NULL;
newFunction = (struct function *) malloc(sizeof(struct function));
if(newFunction == NULL)
return ALLOCATE_ERROR;
newFunction->command = cmd;
newFunction->f = fct;
return addFunction(tree, newFunction);
}
/**
* @brief adds a new function to a binary tree
* @param tree the tree in which the new function should be added
* @param new_function the function struct which should be added to the binary tree
* @return 0 = success
* @return -1 = binary tree corrupted
* @return -2 = command allready added
*/
int addFunction(binary_tree **tree, struct function *new_function)
{
int i = 0;
struct branch *current_node = (*tree);
union branchElement *newElement = NULL;
union branchElement *oldElement = NULL;
if((*tree) == NULL)
{
(*tree) = (binary_tree *) malloc(sizeof(binary_tree));
(*tree)->type = branchEnd;
(*tree)->element = (union branchElement *) malloc(sizeof(union branchElement));
(*tree)->element->f = new_function;
return SUCCESS;
}
while(current_node != NULL && current_node->type != branchEnd)
{
if(new_function->command[current_node->element->b->c->number] == current_node->element->b->c->value)
current_node = current_node->element->b->left;
else
current_node = current_node->element->b->right;
}
if(current_node == NULL)
{
return BINARY_TREE_CORRUPTED;
}
else if(!strncmp(current_node->element->f->command, new_function->command, MAX_COMMAND_LENGTH))
{
return COMMAND_ALREADY_ADDED;
}
else
{
for(i = 0; current_node->element->f->command[i] == new_function->command[i]; i++);
current_node->type = moreBranching;
oldElement = current_node->element;
newElement = (union branchElement *) malloc(sizeof(union branchElement));
current_node->element = newElement;
current_node->element->b = (struct branching *) malloc(sizeof(struct branching));
current_node->element->b->c = (struct condition *) malloc(sizeof(struct condition));
current_node->element->b->left = (struct branch *) malloc(sizeof(struct branch));
current_node->element->b->right = (struct branch *) malloc(sizeof(struct branch));
current_node->element->b->left->type = branchEnd;
current_node->element->b->right->type = branchEnd;
current_node->element->b->c->number = i;
current_node->element->b->c->value = new_function->command[i];
current_node->element->b->left->element = (union branchElement *) malloc(sizeof(union branchElement));
current_node->element->b->left->element->f = new_function;
current_node->element->b->right->element = oldElement;
return SUCCESS;
}
}
/**
* @brief returns you a function out of a binary tree
* @param tree the tree in which the function should be searched
* @param function_name the name of the function which should be searched
* @return the function struct of the function
*/
struct function *searchFunction(binary_tree *tree, char *function_name)
{
struct branch *current_node = tree;
while(current_node != NULL && current_node->type != branchEnd)
{
if(current_node->type == moreBranching)
{
if(function_name[current_node->element->b->c->number] == current_node->element->b->c->value)
{
current_node = current_node->element->b->left;
}
else
{
current_node = current_node->element->b->right;
}
}
}
if(current_node == NULL)
{
return NULL;
}
else if(strncmp(current_node->element->f->command, function_name, MAX_COMMAND_LENGTH))
{
return NULL;
}
else
{
return current_node->element->f;
}
}