[tree] Simple tree implementation for processes

This commit is contained in:
Kevin Lange 2011-11-29 02:49:19 -06:00
parent f5d1d411cf
commit 47a6832606
4 changed files with 145 additions and 7 deletions

View File

@ -49,6 +49,15 @@ list_t * list_create() {
return out;
}
node_t * list_find(list_t * list, void * value) {
foreach(item, list) {
if (item->value == value) {
return item;
}
}
return NULL;
}
void list_remove(list_t * list, size_t index) {
/* remove index from the list */
if (index < list->length) return;

96
kernel/core/tree.c Normal file
View File

@ -0,0 +1,96 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* General-purpose tree implementation
*/
#include <system.h>
#include <list.h>
#include <tree.h>
tree_t * tree_create() {
tree_t * out = malloc(sizeof(tree_t));
out->nodes = 0;
out->root = NULL;
return out;
}
void tree_set_root(tree_t * tree, void * value) {
tree_node_t * root = tree_node_create(value);
tree->root = root;
tree->nodes = 1;
}
void tree_node_destroy(tree_node_t * node) {
foreach(child, node->children) {
tree_node_destroy((tree_node_t *)child->value);
}
free(node->value);
}
void tree_destroy(tree_t * tree) {
if (tree->root) {
tree_node_destroy(tree->root);
}
}
void tree_node_free(tree_node_t * node) {
if (!node) return;
foreach(child, node->children) {
tree_node_free(child->value);
}
free(node);
}
void tree_free(tree_t * tree) {
tree_node_free(tree->root);
}
tree_node_t * tree_node_create(void * value) {
tree_node_t * out = malloc(sizeof(tree_node_t));
out->value = value;
out->children = list_create();
out->parent = NULL;
return out;
}
void tree_node_insert_child_node(tree_t * tree, tree_node_t * parent, tree_node_t * node) {
list_insert(parent->children, node);
node->parent = parent;
tree->nodes++;
}
tree_node_t * tree_node_insert_child(tree_t * tree, tree_node_t * parent, void * value) {
tree_node_t * out = tree_node_create(value);
tree_node_insert_child_node(tree, parent, out);
return out;
}
tree_node_t * tree_node_find_parent(tree_node_t * haystack, tree_node_t * needle) {
tree_node_t * found = NULL;
foreach(child, haystack->children) {
if (child->value == needle) {
return haystack;
}
found = tree_node_find_parent((tree_node_t *)child->value, needle);
if (found) {
break;
}
}
return found;
}
tree_node_t * tree_find_parent(tree_t * tree, tree_node_t * node) {
if (!tree->root) return NULL;
return tree_node_find_parent(tree->root, node);
}
void tree_node_parent_remove(tree_t * tree, tree_node_t * parent, tree_node_t * node) {
tree->nodes--;
list_delete(parent->children, list_find(parent->children, node));
tree_node_free(node);
}
void tree_node_remove(tree_t * tree, tree_node_t * node) {
tree_node_t * parent = node->parent;
if (!parent) return;
tree_node_parent_remove(tree, parent, node);
}

View File

@ -2,6 +2,9 @@
*
* General-purpose list implementations.
*/
#ifndef LIST_H
#define LIST_H
#include <system.h>
typedef struct node {
@ -17,19 +20,15 @@ typedef struct {
} list_t;
void list_destroy(list_t * list);
void list_free(list_t * list);
void list_insert(list_t * list, void * item);
list_t * list_create();
node_t * list_find(list_t * list, void * value);
void list_remove(list_t * list, size_t index);
void list_delete(list_t * list, node_t * node);
void * list_pop(list_t * list);
list_t * list_copy(list_t * original);
#define foreach(i, list) for (node_t * i = list->head; i != NULL; i = i->next)
#endif

34
kernel/include/tree.h Normal file
View File

@ -0,0 +1,34 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* General-purpose tree implementation
*/
#ifndef TREE_H
#define TREE_H
#include <system.h>
#include <list.h>
typedef struct tree_node {
void * value;
list_t * children;
struct tree_node * parent;
} tree_node_t;
typedef struct {
size_t nodes;
tree_node_t * root;
} tree_t;
tree_t * tree_create();
void tree_set_root(tree_t * tree, void * value);
void tree_node_destroy(tree_node_t * node);
void tree_destroy(tree_t * tree);
void tree_free(tree_t * tree);
tree_node_t * tree_node_create(void * value);
void tree_node_insert_child_node(tree_t * tree, tree_node_t * parent, tree_node_t * node);
tree_node_t * tree_node_insert_child(tree_t * tree, tree_node_t * parent, void * value);
tree_node_t * tree_node_find_parent(tree_node_t * haystack, tree_node_t * needle);
void tree_node_parent_remove(tree_t * tree, tree_node_t * parent, tree_node_t * node);
void tree_node_remove(tree_t * tree, tree_node_t * node);
#endif