- Implement Case 3 of RtlSplay
authorAlex Ionescu <aionescu@gmail.com>
Tue, 8 Nov 2005 23:40:37 +0000 (23:40 +0000)
committerAlex Ionescu <aionescu@gmail.com>
Tue, 8 Nov 2005 23:40:37 +0000 (23:40 +0000)
svn path=/trunk/; revision=19074

reactos/lib/rtl/splaytree.c

index 50d28a5..ecce34f 100644 (file)
@@ -136,7 +136,7 @@ RtlSplay(PRTL_SPLAY_LINKS Links)
 
                 /*
                  * If they exist, update their parent pointers too, 
 
                 /*
                  * If they exist, update their parent pointers too, 
-                 * since they've changed trees
+                 * since they've changed trees.
                  */
                 if (RtlLeftChild(P)) RtlParent(RtlLeftChild(P)) = P;
                 if (RtlLeftChild(G)) RtlParent(RtlLeftChild(G)) = G;
                  */
                 if (RtlLeftChild(P)) RtlParent(RtlLeftChild(P)) = P;
                 if (RtlLeftChild(G)) RtlParent(RtlLeftChild(G)) = G;
@@ -177,18 +177,74 @@ RtlSplay(PRTL_SPLAY_LINKS Links)
                     }
                 }
 
                     }
                 }
 
-                /* Now N is on top, so P has become its child */
+                /* Now N is on top, so P has become its child. */
                 RtlRightChild(N) = P;
                 RtlParent(P) = N;
 
                 RtlRightChild(N) = P;
                 RtlParent(P) = N;
 
-                /* N is on top, P is its child, so G is grandchild */
+                /* N is on top, P is its child, so G is grandchild. */
                 RtlRightChild(P) = G;
                 RtlParent(G) = P;
             }
             /* Case 3: P is the right child of G */
             else if (RtlIsRightChild(P))
             {
                 RtlRightChild(P) = G;
                 RtlParent(G) = P;
             }
             /* Case 3: P is the right child of G */
             else if (RtlIsRightChild(P))
             {
+                /*
+                 * N's left-child becomes G's right child and
+                 * N's right-child becomes P's left child.
+                 */
+                RtlRightChild(G) = RtlLeftChild(N);
+                RtlLeftChild(P) = RtlRightChild(N);
+
+                /*
+                 * If they exist, update their parent pointers too, 
+                 * since they've changed trees.
+                 */
+                if (RtlRightChild(G)) RtlParent(RtlRightChild(G)) = G;
+                if (RtlLeftChild(P)) RtlParent(RtlLeftChild(P)) = P;
+
+                /*
+                 * Now we'll shove N all the way to the top.
+                 * Check if G is the root first.
+                 */
+                if (RtlIsRoot(G))
+                {
+                    /* G doesn't have a parent, so N will become the root! */
+                    RtlParent(N) = N;
+                }
+                else
+                {
+                    /* G has a parent, so inherit it since we take G's place */
+                    RtlParent(N) = RtlParent(G);
 
 
+                    /*
+                     * Now find out who was referencing G and have it reference
+                     * N instead, since we're taking G's place.
+                     */
+                    if (RtlIsLeftChild(G))
+                    {
+                        /*
+                         * G was a left child, so change its parent's left
+                         * child link to point to N now.
+                         */
+                        RtlLeftChild(RtlParent(G)) = N;
+                    }
+                    else
+                    {
+                        /*
+                         * G was a right child, so change its parent's right
+                         * child link to point to N now.
+                         */
+                        RtlLeftChild(RtlParent(G)) = N;
+                    }
+                }
+
+                /* Now N is on top, so G has become its left child. */
+                RtlLeftChild(N) = G;
+                RtlParent(G) = N;
+
+                /* N is on top, G is its left child, so P is right child. */
+                RtlRightChild(N) = P;
+                RtlParent(P) = N;
             }
             /* "Finally" case: N doesn't have a grandparent => P is root */
             else
             }
             /* "Finally" case: N doesn't have a grandparent => P is root */
             else