1 /* toolbox/tree.h - binary tree management
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
36 #include "mm/memory.h"
37 #include "toolbox/tree.h"
40 tree *tree_new(treeelementcomperator comperator)
45 t->comperator = comperator;
53 tree *tree_dnew(treeelementcomperator comperator)
58 t->comperator = comperator;
66 static void tree_nodefree(treenode *tn)
71 tree_nodefree(tn->left);
72 tree_nodefree(tn->right);
77 void tree_free(tree *t)
81 tree_nodefree(t->top);
86 static treenode *tree_nodeadd(tree *t, treenode *par, treenode *n,
87 void *element, void *key)
100 n->element = element;
103 if (t->comperator(key, n->element) < 0) {
104 n->left = tree_nodeadd(t, n, n->left, element, key);
107 n->right = tree_nodeadd(t, n, n->right, element, key);
115 void tree_add(tree *t, void *element, void *key)
117 t->top = tree_nodeadd(t, NULL, t->top, element, key);
122 static treenode *tree_nodefind(tree *t, treenode *n, void *key)
129 way = t->comperator(key, n->element);
134 return tree_nodefind(t, n->left, key);
137 return tree_nodefind (t, n -> right, key);
142 void *tree_find(tree *t, void *key)
144 treenode *tn = tree_nodefind(t, t->top, key);
156 void *tree_this(tree *t)
161 return t->active->element;
165 static treenode *leftmostnode(treenode *t)
175 void *tree_first(tree *t)
177 treenode *a = t->top;
189 void *tree_next (tree *t)
191 treenode *a = t->active;
192 treenode *comefrom = NULL;
198 if (a->left && (a->left == comefrom)) {
203 if (a->right && (a->right != comefrom)) {
204 a = leftmostnode(a->right);
220 * These are local overrides for various environment variables in Emacs.
221 * Please do not remove this and leave it at the end of the file, where
222 * Emacs will automagically detect them.
223 * ---------------------------------------------------------------------
226 * indent-tabs-mode: t