You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

91 lines
1.5 KiB

#include "tree.h"
#include <stdlib.h>
#include <stdio.h>
Tree* newRoot(Data data)
{
Tree* node = malloc(sizeof(Tree));
node->data = data;
for (int i=0; i<6; i++)
node->child[i] = NULL;
node->parent = NULL;
return node;
}
Tree* newNode(Tree* parent, int pos, Data data)
{
if (parent == NULL)
{
fprintf(stderr, "newNode(): parent == NULL");
return NULL;
}
Tree* node = newRoot(data);
node->parent = parent;
parent->child[pos] = node;
return node;
}
void deleteTree(Tree* root)
{
if (root == NULL)
return;
if (root->parent != NULL)
for (int i=0; i<6; i++)
if (root->parent->child[i] == root)
root->parent->child[i] = NULL;
for (int i=0; i<6; i++)
deleteTree(root->child[i]);
free(root);
}
int countChilds(Tree* node)
{
if (node == NULL)
{
fprintf(stderr, "countChilds: attempting to count childs of a NULL tree.\n");
return 0;
}
int count = 0;
for (int i=0; i<6; i++)
if (node->child[i] != NULL)
count ++;
return count;
}
int getParents(Tree* node, int** parents)
{
if (node == NULL)
{
*parents = NULL;
return -1;
}
Tree* current = node;
int count = 1;
while (current->parent != NULL)
{
current = current->parent;
count++;
}
*parents = malloc(count*sizeof(int));
current = node;
int i=0;
while (i < count)
{
(*parents)[i++] = current->data;
current = current->parent;
}
return i;
}