This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Is libiberty's splay-tree.c really a splay tree?


OK, I know I'm embarrassing myself here, but is libiberty's splay-tree.c
doing the right thing for the zig-zig and zag-zag cases?  The code reads:

    /* Now we have the four cases of double-rotation.  */
    if (cmp1 < 0 && cmp2 < 0)
      {
	rotate_left (&n->left, c, c->left);
	rotate_left (&sp->root, n, n->left);
      }
    else if (cmp1 > 0 && cmp2 > 0)
      {
	rotate_right (&n->right, c, c->right);
	rotate_right (&sp->root, n, n->right);
      }
    else ...

whereas I thought you were supposed to rotate on the root first
for these cases.  E.g. instead of:

         X                     Y                Z
        / \                  /   \             / \
       Y   D                Z     X           A   Y
      / \                  / \   / \             / \
     Z   C      --->      A   B C   D   --->    B   X
    / \                                            / \
   A   B                                          C   D

the above seems to implement:

         X                 X                    Z
        / \               / \                  / \
       Y   D             Z   D                A   X
      / \               / \                      / \
     Z   C      --->   A   Y          ---->     Y   D
    / \                   / \                  / \
   A   B                 B   C                B   C

(The other two cases seem fine.)

Richard


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]