xref: /netbsd-src/external/bsd/libbind/dist/isc/tree.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: tree.c,v 1.1.1.2 2012/09/09 16:08:00 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos #ifndef LINT
4b5677b36Schristos static const char rcsid[] = "Id: tree.c,v 1.4 2005/04/27 04:56:39 sra Exp ";
5b5677b36Schristos #endif
6b5677b36Schristos 
7b5677b36Schristos /*%
8b5677b36Schristos  * tree - balanced binary tree library
9b5677b36Schristos  *
10b5677b36Schristos  * vix 05apr94 [removed vixie.h dependencies; cleaned up formatting, names]
11b5677b36Schristos  * vix 22jan93 [revisited; uses RCS, ANSI, POSIX; has bug fixes]
12b5677b36Schristos  * vix 23jun86 [added delete uar to add for replaced nodes]
13b5677b36Schristos  * vix 20jun86 [added tree_delete per wirth a+ds (mod2 v.) p. 224]
14b5677b36Schristos  * vix 06feb86 [added tree_mung()]
15b5677b36Schristos  * vix 02feb86 [added tree balancing from wirth "a+ds=p" p. 220-221]
16b5677b36Schristos  * vix 14dec85 [written]
17b5677b36Schristos  */
18b5677b36Schristos 
19b5677b36Schristos /*%
20b5677b36Schristos  * This program text was created by Paul Vixie using examples from the book:
21b5677b36Schristos  * "Algorithms & Data Structures," Niklaus Wirth, Prentice-Hall, 1986, ISBN
22b5677b36Schristos  * 0-13-022005-1.  Any errors in the conversion from Modula-2 to C are Paul
23b5677b36Schristos  * Vixie's.
24b5677b36Schristos  */
25b5677b36Schristos 
26b5677b36Schristos /*
27b5677b36Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
28b5677b36Schristos  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
29b5677b36Schristos  *
30b5677b36Schristos  * Permission to use, copy, modify, and distribute this software for any
31b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
32b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
33b5677b36Schristos  *
34b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
35b5677b36Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
36b5677b36Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
37b5677b36Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38b5677b36Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
39b5677b36Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
40b5677b36Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41b5677b36Schristos  */
42b5677b36Schristos 
43b5677b36Schristos /*#define		DEBUG	"tree"*/
44b5677b36Schristos 
45b5677b36Schristos #include "port_before.h"
46b5677b36Schristos 
47b5677b36Schristos #include <stdio.h>
48b5677b36Schristos #include <stdlib.h>
49b5677b36Schristos 
50b5677b36Schristos #include "port_after.h"
51b5677b36Schristos 
52b5677b36Schristos #include <isc/memcluster.h>
53b5677b36Schristos #include <isc/tree.h>
54b5677b36Schristos 
55b5677b36Schristos #ifdef DEBUG
56b5677b36Schristos static int	debugDepth = 0;
57b5677b36Schristos static char	*debugFuncs[256];
58b5677b36Schristos # define ENTER(proc) { \
59b5677b36Schristos 			debugFuncs[debugDepth] = proc; \
60b5677b36Schristos 			fprintf(stderr, "ENTER(%d:%s.%s)\n", \
61b5677b36Schristos 				debugDepth, DEBUG, \
62b5677b36Schristos 				debugFuncs[debugDepth]); \
63b5677b36Schristos 			debugDepth++; \
64b5677b36Schristos 		}
65b5677b36Schristos # define RET(value) { \
66b5677b36Schristos 			debugDepth--; \
67b5677b36Schristos 			fprintf(stderr, "RET(%d:%s.%s)\n", \
68b5677b36Schristos 				debugDepth, DEBUG, \
69b5677b36Schristos 				debugFuncs[debugDepth]); \
70b5677b36Schristos 			return (value); \
71b5677b36Schristos 		}
72b5677b36Schristos # define RETV { \
73b5677b36Schristos 			debugDepth--; \
74b5677b36Schristos 			fprintf(stderr, "RETV(%d:%s.%s)\n", \
75b5677b36Schristos 				debugDepth, DEBUG, \
76b5677b36Schristos 				debugFuncs[debugDepth]); \
77b5677b36Schristos 			return; \
78b5677b36Schristos 		}
79b5677b36Schristos # define MSG(msg)	fprintf(stderr, "MSG(%s)\n", msg);
80b5677b36Schristos #else
81b5677b36Schristos # define ENTER(proc)	;
82b5677b36Schristos # define RET(value)	return (value);
83b5677b36Schristos # define RETV		return;
84b5677b36Schristos # define MSG(msg)	;
85b5677b36Schristos #endif
86b5677b36Schristos 
87b5677b36Schristos #ifndef TRUE
88b5677b36Schristos # define TRUE		1
89b5677b36Schristos # define FALSE		0
90b5677b36Schristos #endif
91b5677b36Schristos 
92b5677b36Schristos static tree *	sprout(tree **, tree_t, int *, int (*)(), void (*)());
93b5677b36Schristos static int	delete(tree **, int (*)(), tree_t, void (*)(), int *, int *);
94b5677b36Schristos static void	del(tree **, int *, tree **, void (*)(), int *);
95b5677b36Schristos static void	bal_L(tree **, int *);
96b5677b36Schristos static void	bal_R(tree **, int *);
97b5677b36Schristos 
98b5677b36Schristos void
tree_init(tree ** ppr_tree)99b5677b36Schristos tree_init(tree **ppr_tree) {
100b5677b36Schristos 	ENTER("tree_init")
101b5677b36Schristos 	*ppr_tree = NULL;
102b5677b36Schristos 	RETV
103b5677b36Schristos }
104b5677b36Schristos 
105b5677b36Schristos tree_t
tree_srch(tree ** ppr_tree,int (* pfi_compare)(tree_t,tree_t),tree_t p_user)106b5677b36Schristos tree_srch(tree **ppr_tree, int (*pfi_compare)(tree_t, tree_t), tree_t	p_user) {
107b5677b36Schristos 	ENTER("tree_srch")
108b5677b36Schristos 
109b5677b36Schristos 	if (*ppr_tree) {
110b5677b36Schristos 		int i_comp = (*pfi_compare)(p_user, (**ppr_tree).data);
111b5677b36Schristos 
112b5677b36Schristos 		if (i_comp > 0)
113b5677b36Schristos 			RET(tree_srch(&(**ppr_tree).right,
114b5677b36Schristos 				      pfi_compare,
115b5677b36Schristos 				      p_user))
116b5677b36Schristos 
117b5677b36Schristos 		if (i_comp < 0)
118b5677b36Schristos 			RET(tree_srch(&(**ppr_tree).left,
119b5677b36Schristos 				      pfi_compare,
120b5677b36Schristos 				      p_user))
121b5677b36Schristos 
122b5677b36Schristos 		/* not higher, not lower... this must be the one.
123b5677b36Schristos 		 */
124b5677b36Schristos 		RET((**ppr_tree).data)
125b5677b36Schristos 	}
126b5677b36Schristos 
127b5677b36Schristos 	/* grounded. NOT found.
128b5677b36Schristos 	 */
129b5677b36Schristos 	RET(NULL)
130b5677b36Schristos }
131b5677b36Schristos 
132b5677b36Schristos tree_t
tree_add(tree ** ppr_tree,int (* pfi_compare)(tree_t,tree_t),tree_t p_user,void (* pfv_uar)())133b5677b36Schristos tree_add(tree **ppr_tree, int (*pfi_compare)(tree_t, tree_t),
134b5677b36Schristos 	 tree_t p_user, void (*pfv_uar)())
135b5677b36Schristos {
136b5677b36Schristos 	int i_balance = FALSE;
137b5677b36Schristos 
138b5677b36Schristos 	ENTER("tree_add")
139b5677b36Schristos 	if (!sprout(ppr_tree, p_user, &i_balance, pfi_compare, pfv_uar))
140b5677b36Schristos 		RET(NULL)
141b5677b36Schristos 	RET(p_user)
142b5677b36Schristos }
143b5677b36Schristos 
144b5677b36Schristos int
tree_delete(tree ** ppr_p,int (* pfi_compare)(tree_t,tree_t),tree_t p_user,void (* pfv_uar)())145b5677b36Schristos tree_delete(tree **ppr_p, int (*pfi_compare)(tree_t, tree_t),
146b5677b36Schristos 	    tree_t p_user, void	(*pfv_uar)())
147b5677b36Schristos {
148b5677b36Schristos 	int i_balance = FALSE, i_uar_called = FALSE;
149b5677b36Schristos 
150b5677b36Schristos 	ENTER("tree_delete");
151b5677b36Schristos 	RET(delete(ppr_p, pfi_compare, p_user, pfv_uar,
152b5677b36Schristos 		   &i_balance, &i_uar_called))
153b5677b36Schristos }
154b5677b36Schristos 
155b5677b36Schristos int
tree_trav(tree ** ppr_tree,int (* pfi_uar)(tree_t))156b5677b36Schristos tree_trav(tree **ppr_tree, int (*pfi_uar)(tree_t)) {
157b5677b36Schristos 	ENTER("tree_trav")
158b5677b36Schristos 
159b5677b36Schristos 	if (!*ppr_tree)
160b5677b36Schristos 		RET(TRUE)
161b5677b36Schristos 
162b5677b36Schristos 	if (!tree_trav(&(**ppr_tree).left, pfi_uar))
163b5677b36Schristos 		RET(FALSE)
164b5677b36Schristos 	if (!(*pfi_uar)((**ppr_tree).data))
165b5677b36Schristos 		RET(FALSE)
166b5677b36Schristos 	if (!tree_trav(&(**ppr_tree).right, pfi_uar))
167b5677b36Schristos 		RET(FALSE)
168b5677b36Schristos 	RET(TRUE)
169b5677b36Schristos }
170b5677b36Schristos 
171b5677b36Schristos void
tree_mung(tree ** ppr_tree,void (* pfv_uar)(tree_t))172b5677b36Schristos tree_mung(tree **ppr_tree, void	(*pfv_uar)(tree_t)) {
173b5677b36Schristos 	ENTER("tree_mung")
174b5677b36Schristos 	if (*ppr_tree) {
175b5677b36Schristos 		tree_mung(&(**ppr_tree).left, pfv_uar);
176b5677b36Schristos 		tree_mung(&(**ppr_tree).right, pfv_uar);
177b5677b36Schristos 		if (pfv_uar)
178b5677b36Schristos 			(*pfv_uar)((**ppr_tree).data);
179b5677b36Schristos 		memput(*ppr_tree, sizeof(tree));
180b5677b36Schristos 		*ppr_tree = NULL;
181b5677b36Schristos 	}
182b5677b36Schristos 	RETV
183b5677b36Schristos }
184b5677b36Schristos 
185b5677b36Schristos static tree *
sprout(tree ** ppr,tree_t p_data,int * pi_balance,int (* pfi_compare)(tree_t,tree_t),void (* pfv_delete)(tree_t))186b5677b36Schristos sprout(tree **ppr, tree_t p_data, int *pi_balance,
187b5677b36Schristos        int (*pfi_compare)(tree_t, tree_t), void (*pfv_delete)(tree_t))
188b5677b36Schristos {
189b5677b36Schristos 	tree *p1, *p2, *sub;
190b5677b36Schristos 	int cmp;
191b5677b36Schristos 
192b5677b36Schristos 	ENTER("sprout")
193b5677b36Schristos 
194b5677b36Schristos 	/* are we grounded?  if so, add the node "here" and set the rebalance
195b5677b36Schristos 	 * flag, then exit.
196b5677b36Schristos 	 */
197b5677b36Schristos 	if (!*ppr) {
198b5677b36Schristos 		MSG("grounded. adding new node, setting h=true")
199b5677b36Schristos 		*ppr = (tree *) memget(sizeof(tree));
200b5677b36Schristos 		if (*ppr) {
201b5677b36Schristos 			(*ppr)->left = NULL;
202b5677b36Schristos 			(*ppr)->right = NULL;
203b5677b36Schristos 			(*ppr)->bal = 0;
204b5677b36Schristos 			(*ppr)->data = p_data;
205b5677b36Schristos 			*pi_balance = TRUE;
206b5677b36Schristos 		}
207b5677b36Schristos 		RET(*ppr);
208b5677b36Schristos 	}
209b5677b36Schristos 
210b5677b36Schristos 	/* compare the data using routine passed by caller.
211b5677b36Schristos 	 */
212b5677b36Schristos 	cmp = (*pfi_compare)(p_data, (*ppr)->data);
213b5677b36Schristos 
214b5677b36Schristos 	/* if LESS, prepare to move to the left.
215b5677b36Schristos 	 */
216b5677b36Schristos 	if (cmp < 0) {
217b5677b36Schristos 		MSG("LESS. sprouting left.")
218b5677b36Schristos 		sub = sprout(&(*ppr)->left, p_data, pi_balance,
219b5677b36Schristos 			     pfi_compare, pfv_delete);
220b5677b36Schristos 		if (sub && *pi_balance) {	/*%< left branch has grown */
221b5677b36Schristos 			MSG("LESS: left branch has grown")
222b5677b36Schristos 			switch ((*ppr)->bal) {
223b5677b36Schristos 			case 1:
224b5677b36Schristos 				/* right branch WAS longer; bal is ok now */
225b5677b36Schristos 				MSG("LESS: case 1.. bal restored implicitly")
226b5677b36Schristos 				(*ppr)->bal = 0;
227b5677b36Schristos 				*pi_balance = FALSE;
228b5677b36Schristos 				break;
229b5677b36Schristos 			case 0:
230b5677b36Schristos 				/* balance WAS okay; now left branch longer */
231b5677b36Schristos 				MSG("LESS: case 0.. balnce bad but still ok")
232b5677b36Schristos 				(*ppr)->bal = -1;
233b5677b36Schristos 				break;
234b5677b36Schristos 			case -1:
235b5677b36Schristos 				/* left branch was already too long. rebal */
236b5677b36Schristos 				MSG("LESS: case -1: rebalancing")
237b5677b36Schristos 				p1 = (*ppr)->left;
238b5677b36Schristos 				if (p1->bal == -1) {		/*%< LL */
239b5677b36Schristos 					MSG("LESS: single LL")
240b5677b36Schristos 					(*ppr)->left = p1->right;
241b5677b36Schristos 					p1->right = *ppr;
242b5677b36Schristos 					(*ppr)->bal = 0;
243b5677b36Schristos 					*ppr = p1;
244b5677b36Schristos 				} else {			/*%< double LR */
245b5677b36Schristos 					MSG("LESS: double LR")
246b5677b36Schristos 
247b5677b36Schristos 					p2 = p1->right;
248b5677b36Schristos 					p1->right = p2->left;
249b5677b36Schristos 					p2->left = p1;
250b5677b36Schristos 
251b5677b36Schristos 					(*ppr)->left = p2->right;
252b5677b36Schristos 					p2->right = *ppr;
253b5677b36Schristos 
254b5677b36Schristos 					if (p2->bal == -1)
255b5677b36Schristos 						(*ppr)->bal = 1;
256b5677b36Schristos 					else
257b5677b36Schristos 						(*ppr)->bal = 0;
258b5677b36Schristos 
259b5677b36Schristos 					if (p2->bal == 1)
260b5677b36Schristos 						p1->bal = -1;
261b5677b36Schristos 					else
262b5677b36Schristos 						p1->bal = 0;
263b5677b36Schristos 					*ppr = p2;
264b5677b36Schristos 				} /*else*/
265b5677b36Schristos 				(*ppr)->bal = 0;
266b5677b36Schristos 				*pi_balance = FALSE;
267b5677b36Schristos 			} /*switch*/
268b5677b36Schristos 		} /*if*/
269b5677b36Schristos 		RET(sub)
270b5677b36Schristos 	} /*if*/
271b5677b36Schristos 
272b5677b36Schristos 	/* if MORE, prepare to move to the right.
273b5677b36Schristos 	 */
274b5677b36Schristos 	if (cmp > 0) {
275b5677b36Schristos 		MSG("MORE: sprouting to the right")
276b5677b36Schristos 		sub = sprout(&(*ppr)->right, p_data, pi_balance,
277b5677b36Schristos 			     pfi_compare, pfv_delete);
278b5677b36Schristos 		if (sub && *pi_balance) {
279b5677b36Schristos 			MSG("MORE: right branch has grown")
280b5677b36Schristos 
281b5677b36Schristos 			switch ((*ppr)->bal) {
282b5677b36Schristos 			case -1:
283b5677b36Schristos 				MSG("MORE: balance was off, fixed implicitly")
284b5677b36Schristos 				(*ppr)->bal = 0;
285b5677b36Schristos 				*pi_balance = FALSE;
286b5677b36Schristos 				break;
287b5677b36Schristos 			case 0:
288b5677b36Schristos 				MSG("MORE: balance was okay, now off but ok")
289b5677b36Schristos 				(*ppr)->bal = 1;
290b5677b36Schristos 				break;
291b5677b36Schristos 			case 1:
292b5677b36Schristos 				MSG("MORE: balance was off, need to rebalance")
293b5677b36Schristos 				p1 = (*ppr)->right;
294b5677b36Schristos 				if (p1->bal == 1) {		/*%< RR */
295b5677b36Schristos 					MSG("MORE: single RR")
296b5677b36Schristos 					(*ppr)->right = p1->left;
297b5677b36Schristos 					p1->left = *ppr;
298b5677b36Schristos 					(*ppr)->bal = 0;
299b5677b36Schristos 					*ppr = p1;
300b5677b36Schristos 				} else {			/*%< double RL */
301b5677b36Schristos 					MSG("MORE: double RL")
302b5677b36Schristos 
303b5677b36Schristos 					p2 = p1->left;
304b5677b36Schristos 					p1->left = p2->right;
305b5677b36Schristos 					p2->right = p1;
306b5677b36Schristos 
307b5677b36Schristos 					(*ppr)->right = p2->left;
308b5677b36Schristos 					p2->left = *ppr;
309b5677b36Schristos 
310b5677b36Schristos 					if (p2->bal == 1)
311b5677b36Schristos 						(*ppr)->bal = -1;
312b5677b36Schristos 					else
313b5677b36Schristos 						(*ppr)->bal = 0;
314b5677b36Schristos 
315b5677b36Schristos 					if (p2->bal == -1)
316b5677b36Schristos 						p1->bal = 1;
317b5677b36Schristos 					else
318b5677b36Schristos 						p1->bal = 0;
319b5677b36Schristos 
320b5677b36Schristos 					*ppr = p2;
321b5677b36Schristos 				} /*else*/
322b5677b36Schristos 				(*ppr)->bal = 0;
323b5677b36Schristos 				*pi_balance = FALSE;
324b5677b36Schristos 			} /*switch*/
325b5677b36Schristos 		} /*if*/
326b5677b36Schristos 		RET(sub)
327b5677b36Schristos 	} /*if*/
328b5677b36Schristos 
329b5677b36Schristos 	/* not less, not more: this is the same key!  replace...
330b5677b36Schristos 	 */
331b5677b36Schristos 	MSG("FOUND: Replacing data value")
332b5677b36Schristos 	*pi_balance = FALSE;
333b5677b36Schristos 	if (pfv_delete)
334b5677b36Schristos 		(*pfv_delete)((*ppr)->data);
335b5677b36Schristos 	(*ppr)->data = p_data;
336b5677b36Schristos 	RET(*ppr)
337b5677b36Schristos }
338b5677b36Schristos 
339b5677b36Schristos static int
delete(tree ** ppr_p,int (* pfi_compare)(tree_t,tree_t),tree_t p_user,void (* pfv_uar)(tree_t),int * pi_balance,int * pi_uar_called)340b5677b36Schristos delete(tree **ppr_p, int (*pfi_compare)(tree_t, tree_t), tree_t p_user,
341b5677b36Schristos        void (*pfv_uar)(tree_t), int *pi_balance, int *pi_uar_called)
342b5677b36Schristos {
343b5677b36Schristos 	tree *pr_q;
344b5677b36Schristos 	int i_comp, i_ret;
345b5677b36Schristos 
346b5677b36Schristos 	ENTER("delete")
347b5677b36Schristos 
348b5677b36Schristos 	if (*ppr_p == NULL) {
349b5677b36Schristos 		MSG("key not in tree")
350b5677b36Schristos 		RET(FALSE)
351b5677b36Schristos 	}
352b5677b36Schristos 
353b5677b36Schristos 	i_comp = (*pfi_compare)((*ppr_p)->data, p_user);
354b5677b36Schristos 	if (i_comp > 0) {
355b5677b36Schristos 		MSG("too high - scan left")
356b5677b36Schristos 		i_ret = delete(&(*ppr_p)->left, pfi_compare, p_user, pfv_uar,
357b5677b36Schristos 			       pi_balance, pi_uar_called);
358b5677b36Schristos 		if (*pi_balance)
359b5677b36Schristos 			bal_L(ppr_p, pi_balance);
360b5677b36Schristos 	} else if (i_comp < 0) {
361b5677b36Schristos 		MSG("too low - scan right")
362b5677b36Schristos 		i_ret = delete(&(*ppr_p)->right, pfi_compare, p_user, pfv_uar,
363b5677b36Schristos 			       pi_balance, pi_uar_called);
364b5677b36Schristos 		if (*pi_balance)
365b5677b36Schristos 			bal_R(ppr_p, pi_balance);
366b5677b36Schristos 	} else {
367b5677b36Schristos 		MSG("equal")
368b5677b36Schristos 		pr_q = *ppr_p;
369b5677b36Schristos 		if (pr_q->right == NULL) {
370b5677b36Schristos 			MSG("right subtree null")
371b5677b36Schristos 			*ppr_p = pr_q->left;
372b5677b36Schristos 			*pi_balance = TRUE;
373b5677b36Schristos 		} else if (pr_q->left == NULL) {
374b5677b36Schristos 			MSG("right subtree non-null, left subtree null")
375b5677b36Schristos 			*ppr_p = pr_q->right;
376b5677b36Schristos 			*pi_balance = TRUE;
377b5677b36Schristos 		} else {
378b5677b36Schristos 			MSG("neither subtree null")
379b5677b36Schristos 			del(&pr_q->left, pi_balance, &pr_q,
380b5677b36Schristos 			    pfv_uar, pi_uar_called);
381b5677b36Schristos 			if (*pi_balance)
382b5677b36Schristos 				bal_L(ppr_p, pi_balance);
383b5677b36Schristos 		}
384b5677b36Schristos 		if (!*pi_uar_called && pfv_uar)
385b5677b36Schristos 			(*pfv_uar)(pr_q->data);
386b5677b36Schristos 		/* Thanks to wuth@castrov.cuc.ab.ca for the following stmt. */
387b5677b36Schristos 		memput(pr_q, sizeof(tree));
388b5677b36Schristos 		i_ret = TRUE;
389b5677b36Schristos 	}
390b5677b36Schristos 	RET(i_ret)
391b5677b36Schristos }
392b5677b36Schristos 
393b5677b36Schristos static void
del(tree ** ppr_r,int * pi_balance,tree ** ppr_q,void (* pfv_uar)(tree_t),int * pi_uar_called)394b5677b36Schristos del(tree **ppr_r, int *pi_balance, tree **ppr_q,
395b5677b36Schristos     void (*pfv_uar)(tree_t), int *pi_uar_called)
396b5677b36Schristos {
397b5677b36Schristos 	ENTER("del")
398b5677b36Schristos 
399b5677b36Schristos 	if ((*ppr_r)->right != NULL) {
400b5677b36Schristos 		del(&(*ppr_r)->right, pi_balance, ppr_q,
401b5677b36Schristos 		    pfv_uar, pi_uar_called);
402b5677b36Schristos 		if (*pi_balance)
403b5677b36Schristos 			bal_R(ppr_r, pi_balance);
404b5677b36Schristos 	} else {
405b5677b36Schristos 		if (pfv_uar)
406b5677b36Schristos 			(*pfv_uar)((*ppr_q)->data);
407b5677b36Schristos 		*pi_uar_called = TRUE;
408b5677b36Schristos 		(*ppr_q)->data = (*ppr_r)->data;
409b5677b36Schristos 		*ppr_q = *ppr_r;
410b5677b36Schristos 		*ppr_r = (*ppr_r)->left;
411b5677b36Schristos 		*pi_balance = TRUE;
412b5677b36Schristos 	}
413b5677b36Schristos 
414b5677b36Schristos 	RETV
415b5677b36Schristos }
416b5677b36Schristos 
417b5677b36Schristos static void
bal_L(tree ** ppr_p,int * pi_balance)418b5677b36Schristos bal_L(tree **ppr_p, int *pi_balance) {
419b5677b36Schristos 	tree *p1, *p2;
420b5677b36Schristos 	int b1, b2;
421b5677b36Schristos 
422b5677b36Schristos 	ENTER("bal_L")
423b5677b36Schristos 	MSG("left branch has shrunk")
424b5677b36Schristos 
425b5677b36Schristos 	switch ((*ppr_p)->bal) {
426b5677b36Schristos 	case -1:
427b5677b36Schristos 		MSG("was imbalanced, fixed implicitly")
428b5677b36Schristos 		(*ppr_p)->bal = 0;
429b5677b36Schristos 		break;
430b5677b36Schristos 	case 0:
431b5677b36Schristos 		MSG("was okay, is now one off")
432b5677b36Schristos 		(*ppr_p)->bal = 1;
433b5677b36Schristos 		*pi_balance = FALSE;
434b5677b36Schristos 		break;
435b5677b36Schristos 	case 1:
436b5677b36Schristos 		MSG("was already off, this is too much")
437b5677b36Schristos 		p1 = (*ppr_p)->right;
438b5677b36Schristos 		b1 = p1->bal;
439b5677b36Schristos 		if (b1 >= 0) {
440b5677b36Schristos 			MSG("single RR")
441b5677b36Schristos 			(*ppr_p)->right = p1->left;
442b5677b36Schristos 			p1->left = *ppr_p;
443b5677b36Schristos 			if (b1 == 0) {
444b5677b36Schristos 				MSG("b1 == 0")
445b5677b36Schristos 				(*ppr_p)->bal = 1;
446b5677b36Schristos 				p1->bal = -1;
447b5677b36Schristos 				*pi_balance = FALSE;
448b5677b36Schristos 			} else {
449b5677b36Schristos 				MSG("b1 != 0")
450b5677b36Schristos 				(*ppr_p)->bal = 0;
451b5677b36Schristos 				p1->bal = 0;
452b5677b36Schristos 			}
453b5677b36Schristos 			*ppr_p = p1;
454b5677b36Schristos 		} else {
455b5677b36Schristos 			MSG("double RL")
456b5677b36Schristos 			p2 = p1->left;
457b5677b36Schristos 			b2 = p2->bal;
458b5677b36Schristos 			p1->left = p2->right;
459b5677b36Schristos 			p2->right = p1;
460b5677b36Schristos 			(*ppr_p)->right = p2->left;
461b5677b36Schristos 			p2->left = *ppr_p;
462b5677b36Schristos 			if (b2 == 1)
463b5677b36Schristos 				(*ppr_p)->bal = -1;
464b5677b36Schristos 			else
465b5677b36Schristos 				(*ppr_p)->bal = 0;
466b5677b36Schristos 			if (b2 == -1)
467b5677b36Schristos 				p1->bal = 1;
468b5677b36Schristos 			else
469b5677b36Schristos 				p1->bal = 0;
470b5677b36Schristos 			*ppr_p = p2;
471b5677b36Schristos 			p2->bal = 0;
472b5677b36Schristos 		}
473b5677b36Schristos 	}
474b5677b36Schristos 	RETV
475b5677b36Schristos }
476b5677b36Schristos 
477b5677b36Schristos static void
bal_R(tree ** ppr_p,int * pi_balance)478b5677b36Schristos bal_R(tree **ppr_p, int *pi_balance) {
479b5677b36Schristos 	tree *p1, *p2;
480b5677b36Schristos 	int b1, b2;
481b5677b36Schristos 
482b5677b36Schristos 	ENTER("bal_R")
483b5677b36Schristos 	MSG("right branch has shrunk")
484b5677b36Schristos 	switch ((*ppr_p)->bal) {
485b5677b36Schristos 	case 1:
486b5677b36Schristos 		MSG("was imbalanced, fixed implicitly")
487b5677b36Schristos 		(*ppr_p)->bal = 0;
488b5677b36Schristos 		break;
489b5677b36Schristos 	case 0:
490b5677b36Schristos 		MSG("was okay, is now one off")
491b5677b36Schristos 		(*ppr_p)->bal = -1;
492b5677b36Schristos 		*pi_balance = FALSE;
493b5677b36Schristos 		break;
494b5677b36Schristos 	case -1:
495b5677b36Schristos 		MSG("was already off, this is too much")
496b5677b36Schristos 		p1 = (*ppr_p)->left;
497b5677b36Schristos 		b1 = p1->bal;
498b5677b36Schristos 		if (b1 <= 0) {
499b5677b36Schristos 			MSG("single LL")
500b5677b36Schristos 			(*ppr_p)->left = p1->right;
501b5677b36Schristos 			p1->right = *ppr_p;
502b5677b36Schristos 			if (b1 == 0) {
503b5677b36Schristos 				MSG("b1 == 0")
504b5677b36Schristos 				(*ppr_p)->bal = -1;
505b5677b36Schristos 				p1->bal = 1;
506b5677b36Schristos 				*pi_balance = FALSE;
507b5677b36Schristos 			} else {
508b5677b36Schristos 				MSG("b1 != 0")
509b5677b36Schristos 				(*ppr_p)->bal = 0;
510b5677b36Schristos 				p1->bal = 0;
511b5677b36Schristos 			}
512b5677b36Schristos 			*ppr_p = p1;
513b5677b36Schristos 		} else {
514b5677b36Schristos 			MSG("double LR")
515b5677b36Schristos 			p2 = p1->right;
516b5677b36Schristos 			b2 = p2->bal;
517b5677b36Schristos 			p1->right = p2->left;
518b5677b36Schristos 			p2->left = p1;
519b5677b36Schristos 			(*ppr_p)->left = p2->right;
520b5677b36Schristos 			p2->right = *ppr_p;
521b5677b36Schristos 			if (b2 == -1)
522b5677b36Schristos 				(*ppr_p)->bal = 1;
523b5677b36Schristos 			else
524b5677b36Schristos 				(*ppr_p)->bal = 0;
525b5677b36Schristos 			if (b2 == 1)
526b5677b36Schristos 				p1->bal = -1;
527b5677b36Schristos 			else
528b5677b36Schristos 				p1->bal = 0;
529b5677b36Schristos 			*ppr_p = p2;
530b5677b36Schristos 			p2->bal = 0;
531b5677b36Schristos 		}
532b5677b36Schristos 	}
533b5677b36Schristos 	RETV
534b5677b36Schristos }
535b5677b36Schristos 
536b5677b36Schristos /*! \file */
537