embox
1/**
2* @file
3* @brief Implementation of methods in util/tree.h
4*
5* @date Oct 8, 2011
6* @author Avdyukhin Dmitry
7*/
8
9#include <assert.h>10#include <stddef.h>11
12#include <lib/libds/dlist.h>13#include <lib/libds/tree.h>14
15struct tree_link *tree_postorder_next(struct tree_link *link) {16if (link == NULL) {17return NULL;18}19/* This node is a root. */20if (link->par == NULL) {21return NULL;22}23/* Search for the next element in brother list. */24if (&link->list_link != (dlist_last_or_null(&link->par->children))) {25/* It's not a last element in list. */26/* TODO dirty hack. XXX O(n) time. */27return tree_postorder_begin(28dlist_entry(link->list_link.next, struct tree_link, list_link));29}30else {31/* Next link is a parent */32return link->par;33}34}
35
36struct tree_link *tree_postorder_begin(struct tree_link *tree) {37if (tree == NULL) {38return NULL;39}40while (!dlist_empty(&tree->children)) {41tree = dlist_entry(dlist_first(&tree->children), struct tree_link,42list_link);43}44return tree;45}
46
47struct tree_link *tree_postorder_end(struct tree_link *tree) {48return tree_postorder_next(tree);49}
50