1 |
/* |
2 |
** Copyright (C) 2003 by Kevin L. Mitchell <klmitch@mit.edu> |
3 |
** |
4 |
** This library is free software; you can redistribute it and/or |
5 |
** modify it under the terms of the GNU Library General Public |
6 |
** License as published by the Free Software Foundation; either |
7 |
** version 2 of the License, or (at your option) any later version. |
8 |
** |
9 |
** This library is distributed in the hope that it will be useful, |
10 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
** Library General Public License for more details. |
13 |
** |
14 |
** You should have received a copy of the GNU Library General Public |
15 |
** License along with this library; if not, write to the Free |
16 |
** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
17 |
** MA 02111-1307, USA |
18 |
** |
19 |
** @(#)$Id: _rb_rotate.c,v 1.1 2003/06/28 18:48:21 klmitch Exp $ |
20 |
*/ |
21 |
#include "dbprim.h" |
22 |
#include "dbprim_int.h" |
23 |
|
24 |
RCSTAG("@(#)$Id: _rb_rotate.c,v 1.1 2003/06/28 18:48:21 klmitch Exp $"); |
25 |
|
26 |
/* Swap a child with its parent, effecting a rotation */ |
27 |
void |
28 |
_rb_rotate(rb_tree_t *tree, rb_node_t *child) |
29 |
{ |
30 |
rb_node_t *parent; |
31 |
|
32 |
if (!(parent = child->rn_parent)) /* make sure it has a parent... */ |
33 |
return; |
34 |
|
35 |
/* Figure out what points to the parent and repoint it to the child */ |
36 |
if (!parent->rn_parent) /* is the parent the root? */ |
37 |
tree->rt_root = child; |
38 |
else if (rn_isleft(parent)) /* OK, is it a left node? */ |
39 |
parent->rn_parent->rn_left = child; |
40 |
else /* ok, must be a right node */ |
41 |
parent->rn_parent->rn_right = child; |
42 |
|
43 |
/* Now update the parent pointers of parent and child... */ |
44 |
child->rn_parent = parent->rn_parent; |
45 |
parent->rn_parent = child; |
46 |
|
47 |
/* Finally, move the child nodes around a little... */ |
48 |
if (parent->rn_right == child) { /* right child? */ |
49 |
if (child->rn_left) /* update grandchild's parent pointer */ |
50 |
child->rn_left->rn_parent = parent; |
51 |
parent->rn_right = child->rn_left; /* then move the child to the parent */ |
52 |
child->rn_left = parent; /* Finally, link the parent to the child */ |
53 |
} else { /* OK, left child! */ |
54 |
if (child->rn_right) /* update grandchild's parent pointer */ |
55 |
child->rn_right->rn_parent = parent; |
56 |
parent->rn_left = child->rn_right; /* then move the child to the parent */ |
57 |
child->rn_right = parent; /* Finally, link the parent to the child */ |
58 |
} |
59 |
} |