Added more functions to tree.
This commit is contained in:
parent
cac747f6f6
commit
ad7ad7d974
|
@ -18,7 +18,8 @@ Please contact the author regarding bugs and/or feature requests.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Tree:
|
Tree:
|
||||||
A data structure in which each node has some child nodes, and data about itself. A tree has only one root node, can have a great number of leaf nodes.
|
A data structure in which each node has some child nodes, and data about itself. A tree has only one root node, can have a great number of leaf nodes. Each node contains some data, and pointers to child
|
||||||
|
nodes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -45,6 +46,7 @@ TREE_NODE* TEMPLATE(new,TREE_NODE)(TREE_TYPE data, int childNum, ...){
|
||||||
assert(node != NULL);
|
assert(node != NULL);
|
||||||
node->data = data;
|
node->data = data;
|
||||||
node->childCount = childNum;
|
node->childCount = childNum;
|
||||||
|
//If there are children to add, add as many as given.
|
||||||
if (childNum > 0){
|
if (childNum > 0){
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, childNum);
|
va_start(args, childNum);
|
||||||
|
@ -58,4 +60,39 @@ TREE_NODE* TEMPLATE(new,TREE_NODE)(TREE_TYPE data, int childNum, ...){
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Tree node creation: Empty tree node.
|
||||||
|
Creates a new tree node with no children, just data.
|
||||||
|
*/
|
||||||
|
TREE_NODE* TEMPLATE(newEmpty,TREE_NODE)(TREE_TYPE data){
|
||||||
|
TREE_NODE* node = malloc(sizeof(TREE_NODE));
|
||||||
|
assert(node != NULL);
|
||||||
|
node->data = data;
|
||||||
|
node->childCount = 0;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Free tree node:
|
||||||
|
Frees a node, and all children of this node.
|
||||||
|
*/
|
||||||
|
void TEMPLATE(free,TREE_NODE)(TREE_NODE* node){
|
||||||
|
for (int i = 0; i < node->childCount; i++){
|
||||||
|
TEMPLATE(free,TREE_NODE)(node->children[i]);
|
||||||
|
}
|
||||||
|
free(node->children);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add node:
|
||||||
|
Adds a node to a given parent node.
|
||||||
|
*/
|
||||||
|
void TEMPLATE(addNode,TREE_NODE)(TREE_NODE* node, TREE_NODE newChild){
|
||||||
|
node->childCount = node->childCount + 1;
|
||||||
|
TREE_NODE** oldChildren = node->children;
|
||||||
|
node->children = malloc(node->childCount * sizeof(TREE_NODE*));
|
||||||
|
assert(node->children != NULL);
|
||||||
|
memcpy(node->children, oldChildren, node->)
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue