1 /* Balanced binary trees using treaps.
2 Copyright (C) 2000, 2002, 2003, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The idea is to balance the tree using pseudorandom numbers. The
23 main constraint on this implementation is that we have several
24 distinct structures that have to be arranged in a binary tree.
25 These structures all contain a BBT_HEADER() in front that gives the
26 treap-related information. The key and value are assumed to reside
27 in the rest of the structure.
29 When calling, we are also passed a comparison function that
30 compares two nodes. We don't implement a separate 'find' function
31 here, but rather use separate functions for each variety of tree.
32 We are also restricted to not copy treap structures, which most
33 implementations find convenient, because we otherwise would need to
34 know how long the structure is.
36 This implementation is based on Stefan Nilsson's article in the
37 July 1997 Doctor Dobb's Journal, "Treaps in Java". */
41 #include "coretypes.h"
44 typedef struct gfc_treap
46 BBT_HEADER (gfc_treap
);
50 /* Simple linear congruential pseudorandom number generator. The
51 period of this generator is 44071, which is plenty for our
59 x0
= (22611 * x0
+ 10) % 44071;
64 /* Rotate the treap left. */
67 rotate_left (gfc_bbt
*t
)
72 t
->right
= t
->right
->left
;
79 /* Rotate the treap right. */
82 rotate_right (gfc_bbt
*t
)
87 t
->left
= t
->left
->right
;
94 /* Recursive insertion function. Returns the updated treap, or
95 aborts if we find a duplicate key. */
98 insert (gfc_bbt
*new_bbt
, gfc_bbt
*t
, compare_fn compare
)
105 c
= (*compare
) (new_bbt
, t
);
109 t
->left
= insert (new_bbt
, t
->left
, compare
);
110 if (t
->priority
< t
->left
->priority
)
111 t
= rotate_right (t
);
115 t
->right
= insert (new_bbt
, t
->right
, compare
);
116 if (t
->priority
< t
->right
->priority
)
119 else /* if (c == 0) */
120 gfc_internal_error("insert_bbt(): Duplicate key found!");
126 /* Given root pointer, a new node and a comparison function, insert
127 the new node into the treap. It is an error to insert a key that
131 gfc_insert_bbt (void *root
, void *new_node
, compare_fn compare
)
135 r
= (gfc_bbt
**) root
;
136 n
= (gfc_bbt
*) new_node
;
137 n
->priority
= pseudo_random ();
138 *r
= insert (n
, *r
, compare
);
142 delete_root (gfc_bbt
*t
)
148 if (t
->right
== NULL
)
151 if (t
->left
->priority
> t
->right
->priority
)
153 temp
= rotate_right (t
);
154 temp
->right
= delete_root (t
);
158 temp
= rotate_left (t
);
159 temp
->left
= delete_root (t
);
166 /* Delete an element from a tree. The 'old' value does not
167 necessarily have to point to the element to be deleted, it must
168 just point to a treap structure with the key to be deleted.
169 Returns the new root node of the tree. */
172 delete_treap (gfc_bbt
*old
, gfc_bbt
*t
, compare_fn compare
)
179 c
= (*compare
) (old
, t
);
182 t
->left
= delete_treap (old
, t
->left
, compare
);
184 t
->right
= delete_treap (old
, t
->right
, compare
);
193 gfc_delete_bbt (void *root
, void *old
, compare_fn compare
)
197 t
= (gfc_bbt
**) root
;
198 *t
= delete_treap ((gfc_bbt
*) old
, *t
, compare
);