1 /**************************************************************************
3 xserver-xorg-video-exynos
5 Copyright 2010 - 2011 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: Boram Park <boram1288.park@samsung.com>
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 **************************************************************************/
36 struct _BINARY_TREE_NODE
38 BINARY_TREE_NODE left;
39 BINARY_TREE_NODE right;
45 BINARY_TREE_NODE head;
48 BINARY_TREE bintree_create_tree (int size)
50 BINARY_TREE tree = calloc (sizeof (struct _BINARY_TREE) + size, 1);
51 if (!tree) return NULL;
59 BINARY_TREE_NODE bintree_create_node (BINARY_TREE tree)
61 BINARY_TREE_NODE node = calloc (sizeof (struct _BINARY_TREE_NODE) + tree->size, 1);
62 if (!node) return NULL;
70 BINARY_TREE_NODE bintree_get_head (BINARY_TREE tree)
75 void bintree_set_head (BINARY_TREE tree, BINARY_TREE_NODE head)
80 void bintree_set_left_child (BINARY_TREE_NODE node, BINARY_TREE_NODE child)
85 void bintree_set_right_child (BINARY_TREE_NODE node, BINARY_TREE_NODE child)
90 BINARY_TREE_NODE bintree_get_left_child (BINARY_TREE_NODE node)
95 BINARY_TREE_NODE bintree_get_right_child (BINARY_TREE_NODE node)
100 void * bintree_get_node_data (BINARY_TREE_NODE node)
102 return (void*)(node + 1);
105 void bintree_remove_node (BINARY_TREE_NODE node)
110 void bintree_remove_node_recursive (BINARY_TREE_NODE node)
113 bintree_remove_node_recursive (node->left);
115 bintree_remove_node_recursive (node->right);
117 bintree_remove_node (node);
120 void bintree_destroy_tree (BINARY_TREE tree)
123 bintree_remove_node_recursive (tree->head);
128 static int bintree_inorder_traverse_recursive (BINARY_TREE tree, BINARY_TREE_NODE node, BINARY_TREE_NODE parent, BINTREE_TRAVERSE_FUNC func, void * arg)
131 if (bintree_inorder_traverse_recursive (tree, node->left, node, func, arg) != 0)
134 if (func (tree, node, parent, arg))
138 if (bintree_inorder_traverse_recursive (tree, node->right, node, func, arg) != 0)
144 void bintree_inorder_traverse (BINARY_TREE tree, BINTREE_TRAVERSE_FUNC func, void * arg)
147 bintree_inorder_traverse_recursive (tree, tree->head, tree->head, func, arg);
150 static int bintree_postorder_traverse_recursive (BINARY_TREE tree, BINARY_TREE_NODE node, BINARY_TREE_NODE parent, BINTREE_TRAVERSE_FUNC func, void * arg)
153 if (bintree_postorder_traverse_recursive (tree, node->left, node, func, arg) != 0)
156 if (bintree_postorder_traverse_recursive (tree, node->right, node, func, arg) != 0)
159 return func (tree, node, parent, arg);
162 void bintree_postorder_traverse (BINARY_TREE tree, BINTREE_TRAVERSE_FUNC func, void * arg)
165 bintree_postorder_traverse_recursive (tree, tree->head, tree->head, func, arg);