annotate lib/gl_avltree_ordered.h @ 40186:8964917f9574

autoupdate
author Karl Berry <karl@freefriends.org>
date Mon, 18 Feb 2019 08:02:49 -0800
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Ordered {set,map} data type implemented by a binary tree.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 40006
diff changeset
2 Copyright (C) 2006-2007, 2009-2019 Free Software Foundation, Inc.
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* An AVL tree is a binary tree where
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 1. The height of each node is calculated as
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 heightof(node) = 1 + max (heightof(node.left), heightof(node.right)).
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 2. The heights of the subtrees of each node differ by at most 1:
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 | heightof(right) - heightof(left) | <= 1.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 3. The index of the elements in the node.left subtree are smaller than
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 the index of node.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 The index of the elements in the node.right subtree are larger than
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 the index of node.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 /* Tree node implementation, valid for this file only. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 struct NODE_IMPL
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 struct NODE_IMPL *left; /* left branch, or NULL */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 struct NODE_IMPL *right; /* right branch, or NULL */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 /* Parent pointer, or NULL. The parent pointer is not needed for most
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 operations. It is needed so that a NODE_T can be returned without
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 memory allocation, on which the functions <container>_remove_node,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 <container>_add_before, <container>_add_after can be implemented. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 struct NODE_IMPL *parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 int balance; /* heightof(right) - heightof(left),
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 always = -1 or 0 or 1 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 NODE_PAYLOAD_FIELDS
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 };
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 typedef struct NODE_IMPL * NODE_T;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* Concrete CONTAINER_IMPL type, valid for this file only. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 struct CONTAINER_IMPL
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 struct CONTAINER_IMPL_BASE base;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 struct NODE_IMPL *root; /* root node or NULL */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 size_t count; /* number of nodes */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 };
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 /* An AVL tree of height h has at least F_(h+2) - 1 [Fibonacci number] and at
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 most 2^h - 1 elements. So, h <= 84 (because a tree of height h >= 85 would
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 have at least F_87 - 1 elements, and because even on 64-bit machines,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 sizeof (NODE_IMPL) * (F_87 - 1) > 2^64
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 this would exceed the address space of the machine. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 #define MAXHEIGHT 83
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* Ensure the tree is balanced, after an insertion or deletion operation.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 The height of NODE is incremented by HEIGHT_DIFF (1 or -1).
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 PARENT = NODE->parent. (NODE can also be NULL. But PARENT is non-NULL.)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 Rotation operations are performed starting at PARENT (not NODE itself!). */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 static void
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 rebalance (CONTAINER_T container,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 NODE_T node, int height_diff, NODE_T parent)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 for (;;)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 NODE_T child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 int previous_balance;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 int balance_diff;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 NODE_T nodeleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 NODE_T noderight;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 child = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 node = parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 previous_balance = node->balance;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 /* The balance of NODE is incremented by BALANCE_DIFF: +1 if the right
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 branch's height has increased by 1 or the left branch's height has
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 decreased by 1, -1 if the right branch's height has decreased by 1 or
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 the left branch's height has increased by 1, 0 if no height change. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 if (node->left != NULL || node->right != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 balance_diff = (child == node->right ? height_diff : -height_diff);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 /* Special case where above formula doesn't work, because the caller
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 didn't tell whether node's left or right branch shrunk from height 1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 to NULL. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 balance_diff = - previous_balance;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 node->balance += balance_diff;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 if (balance_diff == previous_balance)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 /* node->balance is outside the range [-1,1]. Must rotate. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 NODE_T *nodep;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 if (node->parent == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 /* node == container->root */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 nodep = &container->root;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 else if (node->parent->left == node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 nodep = &node->parent->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 else if (node->parent->right == node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 nodep = &node->parent->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 abort ();
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 nodeleft = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 noderight = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 if (balance_diff < 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 /* node->balance = -2. The subtree is heavier on the left side.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 Rotate from left to right:
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 *
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 h+2 h
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 NODE_T nodeleftright = nodeleft->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 if (nodeleft->balance <= 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 /*
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 * h+2|h+3
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 / \ / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 h+2 h --> / h+1|h+2
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 / \ | / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 h+1 h|h+1 h+1 h|h+1 h
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 node->left = nodeleftright;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 nodeleft->right = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 nodeleft->parent = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 node->parent = nodeleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 if (nodeleftright != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 nodeleftright->parent = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 nodeleft->balance += 1;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 node->balance = - nodeleft->balance;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 *nodep = nodeleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 height_diff = (height_diff < 0
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 ? /* noderight's height had been decremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 h+1 to h. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 h+3 to h+2|h+3. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 nodeleft->balance - 1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 : /* nodeleft's height had been incremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 h+1 to h+2. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 h+2 to h+2|h+3. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 nodeleft->balance);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 /*
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 * h+2
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 / \ / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 h+2 h --> h+1 h+1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 / \ / \ / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 h h+1 h L R h
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 L R
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 NODE_T L = nodeleft->right = nodeleftright->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 NODE_T R = node->left = nodeleftright->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 nodeleftright->left = nodeleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 nodeleftright->right = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 nodeleftright->parent = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 if (L != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 L->parent = nodeleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 if (R != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 R->parent = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 nodeleft->parent = nodeleftright;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 node->parent = nodeleftright;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 nodeleft->balance = (nodeleftright->balance > 0 ? -1 : 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 node->balance = (nodeleftright->balance < 0 ? 1 : 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 nodeleftright->balance = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 *nodep = nodeleftright;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 height_diff = (height_diff < 0
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 ? /* noderight's height had been decremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 h+1 to h. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 h+3 to h+2. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 -1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 : /* nodeleft's height had been incremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 h+1 to h+2. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 h+2 to h+2. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 /* node->balance = 2. The subtree is heavier on the right side.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 Rotate from right to left:
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 *
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 h h+2
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 NODE_T noderightleft = noderight->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 if (noderight->balance >= 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 /*
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 * h+2|h+3
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 / \ / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 h h+2 --> h+1|h+2 \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 / \ / \ |
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 h|h+1 h+1 h h|h+1 h+1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 node->right = noderightleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 noderight->left = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 noderight->parent = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 node->parent = noderight;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 if (noderightleft != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 noderightleft->parent = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 noderight->balance -= 1;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 node->balance = - noderight->balance;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 *nodep = noderight;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 height_diff = (height_diff < 0
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 ? /* nodeleft's height had been decremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 h+1 to h. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 h+3 to h+2|h+3. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 - noderight->balance - 1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 : /* noderight's height had been incremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 h+1 to h+2. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 h+2 to h+2|h+3. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 - noderight->balance);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 /*
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 * h+2
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 / \ / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 h h+2 --> h+1 h+1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 / \ / \ / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 h+1 h h L R h
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 / \
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 L R
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 NODE_T L = node->right = noderightleft->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 NODE_T R = noderight->left = noderightleft->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 noderightleft->left = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 noderightleft->right = noderight;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 noderightleft->parent = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 if (L != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 L->parent = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 if (R != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 R->parent = noderight;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 node->parent = noderightleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 noderight->parent = noderightleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 node->balance = (noderightleft->balance > 0 ? -1 : 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 noderight->balance = (noderightleft->balance < 0 ? 1 : 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 noderightleft->balance = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 *nodep = noderightleft;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 height_diff = (height_diff < 0
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 ? /* nodeleft's height had been decremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 h+1 to h. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 h+3 to h+2. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 -1
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 : /* noderight's height had been incremented from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 h+1 to h+2. The subtree's height changes from
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 h+2 to h+2. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 node = *nodep;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 /* No rotation needed. Only propagation of the height change to the
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 next higher level. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 if (height_diff < 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 height_diff = (previous_balance == 0 ? 0 : -1);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 height_diff = (node->balance == 0 ? 0 : 1);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 if (height_diff == 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 break;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 parent = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 if (parent == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 break;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 static NODE_T
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 gl_tree_nx_add_first (CONTAINER_T container, NODE_PAYLOAD_PARAMS)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 /* Create new node. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 NODE_T new_node =
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 (struct NODE_IMPL *) malloc (sizeof (struct NODE_IMPL));
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 if (new_node == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 return NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 new_node->left = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 new_node->right = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 new_node->balance = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 NODE_PAYLOAD_ASSIGN(new_node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 /* Add it to the tree. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 if (container->root == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 container->root = new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 new_node->parent = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 NODE_T node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
322 for (node = container->root; node->left != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 node->left = new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 new_node->parent = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 node->balance--;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329 /* Rebalance. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 if (node->right == NULL && node->parent != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 rebalance (container, node, 1, node->parent);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 container->count++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 return new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 static NODE_T
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 gl_tree_nx_add_before (CONTAINER_T container, NODE_T node, NODE_PAYLOAD_PARAMS)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 /* Create new node. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 NODE_T new_node =
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 (struct NODE_IMPL *) malloc (sizeof (struct NODE_IMPL));
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 bool height_inc;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 if (new_node == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 return NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 new_node->left = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 new_node->right = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 new_node->balance = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 NODE_PAYLOAD_ASSIGN(new_node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 /* Add it to the tree. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 if (node->left == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 node->left = new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 node->balance--;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 height_inc = (node->right == NULL);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 for (node = node->left; node->right != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 node->right = new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 node->balance++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 height_inc = (node->left == NULL);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 new_node->parent = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 /* Rebalance. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 if (height_inc && node->parent != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 rebalance (container, node, 1, node->parent);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 container->count++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 return new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 static NODE_T
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 gl_tree_nx_add_after (CONTAINER_T container, NODE_T node, NODE_PAYLOAD_PARAMS)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 /* Create new node. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 NODE_T new_node =
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 (struct NODE_IMPL *) malloc (sizeof (struct NODE_IMPL));
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 bool height_inc;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 if (new_node == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 return NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 new_node->left = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 new_node->right = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 new_node->balance = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 NODE_PAYLOAD_ASSIGN(new_node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 /* Add it to the tree. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396 if (node->right == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 node->right = new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399 node->balance++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 height_inc = (node->left == NULL);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 for (node = node->right; node->left != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 node->left = new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 node->balance--;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408 height_inc = (node->right == NULL);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 new_node->parent = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 /* Rebalance. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 if (height_inc && node->parent != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 rebalance (container, node, 1, node->parent);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 container->count++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 return new_node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 static bool
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421 gl_tree_remove_node (CONTAINER_T container, NODE_T node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 NODE_T parent = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425 if (node->left == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 /* Replace node with node->right. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
428 NODE_T child = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
429
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 if (child != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431 child->parent = parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432 if (parent == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 container->root = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
434 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
435 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 if (parent->left == node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
437 parent->left = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
438 else /* parent->right == node */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 parent->right = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
441 rebalance (container, child, -1, parent);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
442 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
443 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 else if (node->right == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
445 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
446 /* It is not absolutely necessary to treat this case. But the more
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 general case below is more complicated, hence slower. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 /* Replace node with node->left. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 NODE_T child = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
450
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
451 child->parent = parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
452 if (parent == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
453 container->root = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
454 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
455 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
456 if (parent->left == node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
457 parent->left = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
458 else /* parent->right == node */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
459 parent->right = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
460
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
461 rebalance (container, child, -1, parent);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
462 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
463 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
464 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
465 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
466 /* Replace node with the rightmost element of the node->left subtree. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
467 NODE_T subst;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
468 NODE_T subst_parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
469 NODE_T child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
470
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
471 for (subst = node->left; subst->right != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
472 subst = subst->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
473
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
474 subst_parent = subst->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
475
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
476 child = subst->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
477
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
478 /* The case subst_parent == node is special: If we do nothing special,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
479 we get confusion about node->left, subst->left and child->parent.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
480 subst_parent == node
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
481 <==> The 'for' loop above terminated immediately.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
482 <==> subst == subst_parent->left
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
483 [otherwise subst == subst_parent->right]
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
484 In this case, we would need to first set
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
485 child->parent = node; node->left = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
486 and later - when we copy subst into node's position - again
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
487 child->parent = subst; subst->left = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
488 Altogether a no-op. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
489 if (subst_parent != node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
490 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
491 if (child != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
492 child->parent = subst_parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
493 subst_parent->right = child;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
494 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
495
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
496 /* Copy subst into node's position.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
497 (This is safer than to copy subst's value into node, keep node in
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
498 place, and free subst.) */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
499 if (subst_parent != node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
500 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
501 subst->left = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
502 subst->left->parent = subst;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
503 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
504 subst->right = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
505 subst->right->parent = subst;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
506 subst->balance = node->balance;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
507 subst->parent = parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
508 if (parent == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
509 container->root = subst;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
510 else if (parent->left == node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
511 parent->left = subst;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
512 else /* parent->right == node */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
513 parent->right = subst;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
514
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
515 /* Rebalancing starts at child's parent, that is subst_parent -
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
516 except when subst_parent == node. In this case, we need to use
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
517 its replacement, subst. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
518 rebalance (container, child, -1, subst_parent != node ? subst_parent : subst);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
519 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
520
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
521 container->count--;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
522 NODE_PAYLOAD_DISPOSE
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
523 free (node);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
524 return true;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
525 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
526
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
527 /* For debugging. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
528 static unsigned int
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
529 check_invariants (NODE_T node, NODE_T parent, size_t *counterp)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
530 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
531 unsigned int left_height =
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
532 (node->left != NULL ? check_invariants (node->left, node, counterp) : 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
533 unsigned int right_height =
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
534 (node->right != NULL ? check_invariants (node->right, node, counterp) : 0);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
535 int balance = (int)right_height - (int)left_height;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
536
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
537 if (!(node->parent == parent))
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
538 abort ();
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
539 if (!(balance >= -1 && balance <= 1))
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
540 abort ();
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
541 if (!(node->balance == balance))
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
542 abort ();
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
543
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
544 (*counterp)++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
545
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
546 return 1 + (left_height > right_height ? left_height : right_height);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
547 }