summaryrefslogtreecommitdiff
path: root/tree.h
blob: 7bc180f74d13834e4b556ad174b71bcb3fbff2ec (plain)
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
#ifndef TREE_H
#define TREE_H

  #include <stdlib.h>

  /** \file tree.h tree.c
   *  most basic tree-implemenation: this is meant as base for all classes
   *  and kept as simple as possible in order to prevent overheads.
   *  relying classes should therefor be performant and easy to maintain.
   */

  /** \example ./tst/test.c
   *  This example generates a new tree and adds elements.
   *  and prints them on stdout.
   */

  /// basic structure of a tree- every node can have a child and is part of a
  /// linked list, so it has a next-element
  typedef struct Tree
  {
    const void* data;
    struct Tree* next;
    struct Tree* child;
  } Tree;

  /// creates a new Tree-structure with a dataentry...
  extern Tree* tree_new(const void* const data);

  /// appends the tree to the bottom (adds element to the list)
  extern Tree* tree_add(Tree* tree, const void* const data);

  /// appends the tree to the bottom (adds element to the list)
  extern Tree* tree_addChildTop(Tree* tree, const void* const data);

  /// appends the tree to the bottom (adds element to the list)
  extern Tree* tree_addtop(Tree* tree, const void* const data);

  /// adds a childnode. same as adding an element to the list of its child
  extern Tree* tree_addChild(Tree* tree, const void* const data);

  /// frees all nodes (data* has to be freed seperatly)
  extern void tree_free(Tree* tree, void((*free_function)(const void*)));

  /// adds another tree at the end of the tree-structure (last element addition)
  extern Tree* tree_join(Tree* tree, Tree* subtree);

  /// extends another tree as child of the given
  extern Tree* tree_extend(Tree* tree, Tree* subtree);

  /// iterates over all tree-elements, until return-value of fn is NULL
  /// - uses the return void* from fn() as next function, so that you
  ///   keep the order in deep structures.
  extern void tree_iterate(Tree* tree, void* (*fn)(const void* data));
#endif
..