1 /* toolbox/tree.h - binary tree management
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 Institut f. Computersprachen, TU Wien
5 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
6 S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
9 This file is part of CACAO.
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2, or (at
14 your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 Contact: cacao@complang.tuwien.ac.at
28 Authors: Reinhard Grafl
30 $Id: tree.c 1621 2004-11-30 13:06:55Z twisti $
38 #include "mm/memory.h"
39 #include "toolbox/tree.h"
42 tree *tree_new(treeelementcomperator comperator)
47 t->comperator = comperator;
55 tree *tree_dnew(treeelementcomperator comperator)
60 t->comperator = comperator;
68 static void tree_nodefree(treenode *tn)
73 tree_nodefree(tn->left);
74 tree_nodefree(tn->right);
79 void tree_free(tree *t)
83 tree_nodefree(t->top);
88 static treenode *tree_nodeadd(tree *t, treenode *par, treenode *n,
89 void *element, void *key)
102 n->element = element;
105 if (t->comperator(key, n->element) < 0) {
106 n->left = tree_nodeadd(t, n, n->left, element, key);
109 n->right = tree_nodeadd(t, n, n->right, element, key);
117 void tree_add(tree *t, void *element, void *key)
119 t->top = tree_nodeadd(t, NULL, t->top, element, key);
124 static treenode *tree_nodefind(tree *t, treenode *n, void *key)
131 way = t->comperator(key, n->element);
136 return tree_nodefind(t, n->left, key);
139 return tree_nodefind (t, n -> right, key);
144 void *tree_find(tree *t, void *key)
146 treenode *tn = tree_nodefind(t, t->top, key);
158 void *tree_this(tree *t)
163 return t->active->element;
167 static treenode *leftmostnode(treenode *t)
177 void *tree_first(tree *t)
179 treenode *a = t->top;
191 void *tree_next (tree *t)
193 treenode *a = t->active;
194 treenode *comefrom = NULL;
200 if (a->left && (a->left == comefrom)) {
205 if (a->right && (a->right != comefrom)) {
206 a = leftmostnode(a->right);
222 * These are local overrides for various environment variables in Emacs.
223 * Please do not remove this and leave it at the end of the file, where
224 * Emacs will automagically detect them.
225 * ---------------------------------------------------------------------
228 * indent-tabs-mode: t