File: tree.h

package info (click to toggle)
faumachine 20180503-4
  • links: PTS
  • area: main
  • in suites: buster
  • size: 61,272 kB
  • sloc: ansic: 272,290; makefile: 6,199; asm: 4,251; sh: 3,022; perl: 886; xml: 563; pascal: 311; lex: 214; vhdl: 204
file content (57 lines) | stat: -rw-r--r-- 1,611 bytes parent folder | download
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
55
56
57
/*
 * Copyright (C) 2016 FAUmachine Team <info@faumachine.org>.
 *
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#ifndef __TREE_H_INCLUDED
#define __TREE_H_INCLUDED

/* LIST */
struct lst_node_t {
	struct lst_node_t *preceder;
	struct lst_node_t *succeder;
};
typedef struct lst_node_t lst_node;

typedef struct {
	lst_node node;
	void *data;
} lst_dnode;

typedef struct  {
	lst_node *first;
	lst_node *last;
	int num;
} lst;

#define lst_Foreach(list, iter)	for ((iter) = ((lst_dnode *)(list)->first); iter; (iter) = (lst_dnode *)(((lst_node *)(iter))->succeder))

/* LIST */

typedef struct tree_node_t tree_node;

tree_node *tree_new(void);
void tree_free(tree_node *node);

void* tree_get_data(tree_node *node);
void tree_set_data(tree_node *node, void *data);

tree_node* tree_get_parent(tree_node *node);
lst* tree_get_children(tree_node *node);

tree_node* tree_new_child(tree_node *parent, void *data);

/* Callback which is invoked on every tree node. The opaque pointer will
 * be passed to this function by the traversal function. Return 0 to continue
 * traversal or 1 to abort.
 */
typedef int (*tree_node_callback)(tree_node *node, void *opaque);

int tree_visit_preorder(tree_node *root, tree_node_callback cb, void *opaque);
int tree_visit_postorder(tree_node *root, tree_node_callback cb, void *opaque);
int tree_visit_children(tree_node *parent, tree_node_callback cb, void *opaque);

#endif /* __TREE_H_INCLUDED */