2 * Copyright (C) 2010 Timo Kokkonen <kaapeli@itanic.dy.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * bintree_add - Add to tree
25 * Adds a node to a sorted tree. If the new node is already found from
26 * the tree, the existing node is returned and the new node is not
27 * added. If new node is not found, the new one is added and the tree
30 struct bintree *bintree_add(struct bintree *tree, struct bintree *new,
31 struct bintree_ops *ops)
38 ret = ops->compare(tree, new);
41 return bintree_add(tree->left, new, ops);
48 return bintree_add(tree->right, new, ops);
57 * bintree_find - Find a node from a tree.
59 * If node is found, pointer to the node is returned. If no node is
60 * found, null is returned.
62 struct bintree *bintree_find(struct bintree *tree, struct bintree *new,
63 struct bintree_ops *ops)
70 ret = ops->compare(tree, new);
73 return bintree_find(tree->left, new, ops);
80 return bintree_find(tree->right, new, ops);
89 * bintree_walk - walk through a binary tree from left to right
91 * Returns the number of nodes in the tree
93 int bintree_walk(struct bintree *tree, struct bintree_ops *ops)
98 count += bintree_walk(tree->left, ops);
101 ops->callback(tree, ops);
106 count += bintree_walk(tree->right, ops);