2004-06-20 23:27:21 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* PURPOSE: Splay-Tree implementation
|
|
|
|
* FILE: lib/rtl/splaytree.c
|
2007-10-19 23:21:45 +00:00
|
|
|
* PROGRAMMER: Alex Ionescu (alex@relsoft.net)
|
2004-06-20 23:27:21 +00:00
|
|
|
*/
|
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2005-07-26 08:39:07 +00:00
|
|
|
#include <rtl.h>
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2004-08-05 18:17:37 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-08-01 03:23:53 +00:00
|
|
|
//#define VERIFY_SWAP_SPLAY_LINKS
|
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* FUNCTIONS ***************************************************************/
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2011-08-01 03:23:53 +00:00
|
|
|
static
|
|
|
|
VOID
|
|
|
|
FixupChildLinks(PRTL_SPLAY_LINKS Links, BOOLEAN Root, BOOLEAN LeftChild)
|
|
|
|
{
|
2014-10-18 23:59:25 +00:00
|
|
|
if (RtlLeftChild(Links))
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsLeftChild(Links, RtlLeftChild(Links));
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2014-10-18 23:59:25 +00:00
|
|
|
|
|
|
|
if (RtlRightChild(Links))
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsRightChild(Links, RtlRightChild(Links));
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2014-10-18 23:59:25 +00:00
|
|
|
|
|
|
|
if (!Root)
|
|
|
|
{
|
|
|
|
if (LeftChild)
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsLeftChild(RtlParent(Links), Links);
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2014-10-18 23:59:25 +00:00
|
|
|
else
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsRightChild(RtlParent(Links), Links);
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2011-08-01 03:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Given the tree:
|
|
|
|
D
|
|
|
|
B F
|
|
|
|
A C E G
|
|
|
|
|
|
|
|
Swap(Q,S):
|
|
|
|
|
|
|
|
Q S Q.P Q.L Q.R S.P S.L S.R
|
|
|
|
A C S.P S.L S.R Q.P Q.L Q.R
|
|
|
|
B A S S.L S.R Q.P Q Q.R
|
|
|
|
B C S S.L S.R Q.P Q.L Q
|
|
|
|
D A S.P S.L S.R S Q.L Q.R
|
|
|
|
D B S S.L S.R S Q Q.R
|
|
|
|
D F S S.L S.R S Q.L Q
|
|
|
|
|
|
|
|
When Q is the immediate parent of S,
|
|
|
|
Set Q's parent to S, and the proper child ptr of S to Q
|
|
|
|
When Q is the root,
|
|
|
|
Set S's parent to S
|
2014-10-18 23:59:25 +00:00
|
|
|
|
|
|
|
*/
|
2011-08-01 03:23:53 +00:00
|
|
|
|
|
|
|
static
|
2009-01-23 12:20:37 +00:00
|
|
|
VOID
|
|
|
|
SwapSplayLinks(PRTL_SPLAY_LINKS LinkA,
|
|
|
|
PRTL_SPLAY_LINKS LinkB)
|
|
|
|
{
|
2014-10-18 23:59:25 +00:00
|
|
|
if (RtlParent(LinkA) == LinkB || RtlIsRoot(LinkB))
|
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
PRTL_SPLAY_LINKS Tmp = LinkA;
|
|
|
|
LinkA = LinkB;
|
|
|
|
LinkB = Tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
RTL_SPLAY_LINKS Ta = *LinkA, Tb = *LinkB;
|
2014-10-18 23:59:25 +00:00
|
|
|
BOOLEAN RootA = RtlIsRoot(LinkA),
|
|
|
|
LeftA = RtlIsLeftChild(LinkA),
|
|
|
|
LeftB = RtlIsLeftChild(LinkB);
|
|
|
|
|
2011-08-01 03:23:53 +00:00
|
|
|
*LinkB = Ta; *LinkA = Tb;
|
|
|
|
|
|
|
|
// A was parent of B is a special case: A->Parent is now B
|
2014-10-18 23:59:25 +00:00
|
|
|
if (RtlParent(&Tb) == LinkA)
|
|
|
|
{
|
|
|
|
if (!RootA)
|
|
|
|
{
|
|
|
|
if (LeftA)
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsLeftChild(RtlParent(&Ta), LinkB);
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2014-10-18 23:59:25 +00:00
|
|
|
else
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsRightChild(RtlParent(&Ta), LinkB);
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2011-08-01 03:23:53 +00:00
|
|
|
}
|
2014-10-18 23:59:25 +00:00
|
|
|
|
|
|
|
if (LeftB)
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsLeftChild(LinkB, LinkA);
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2014-10-18 23:59:25 +00:00
|
|
|
else
|
2014-10-19 00:05:18 +00:00
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
RtlInsertAsRightChild(LinkB, LinkA);
|
2014-10-19 00:05:18 +00:00
|
|
|
}
|
2011-08-01 03:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FixupChildLinks(LinkA, FALSE, LeftB);
|
|
|
|
FixupChildLinks(LinkB, RootA, LeftA);
|
|
|
|
|
|
|
|
// A was root is a special case: B->Parent is now B
|
|
|
|
if (RootA)
|
|
|
|
RtlParent(LinkB) = LinkB;
|
|
|
|
|
|
|
|
#ifdef VERIFY_SWAP_SPLAY_LINKS
|
|
|
|
// Verify the distinct cases of node swap
|
2014-10-18 23:59:25 +00:00
|
|
|
if (RootA)
|
|
|
|
{
|
|
|
|
if (RtlParent(&Tb) == LinkA)
|
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
// LinkA = D, LinkB = B
|
|
|
|
// D B S S.L S.R S Q Q.R
|
|
|
|
ASSERT(RtlParent(LinkA) == LinkB);
|
|
|
|
ASSERT(RtlLeftChild(LinkA) == RtlLeftChild(&Tb));
|
|
|
|
ASSERT(RtlRightChild(LinkA) == RtlRightChild(&Tb));
|
|
|
|
ASSERT(RtlParent(LinkB) == LinkB);
|
|
|
|
ASSERT(RtlLeftChild(LinkB) == (LeftB ? LinkA : RtlLeftChild(&Ta)));
|
|
|
|
ASSERT(RtlRightChild(LinkB) == (LeftB ? RtlRightChild(&Ta) : LinkA));
|
2014-10-18 23:59:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
// LinkA = D, LinkB = A
|
|
|
|
// D A S.P S.L S.R S Q.L Q.R
|
|
|
|
ASSERT(RtlParent(LinkA) == RtlParent(&Tb));
|
|
|
|
ASSERT(RtlLeftChild(LinkA) == RtlLeftChild(&Tb));
|
|
|
|
ASSERT(RtlRightChild(LinkA) == RtlRightChild(&Tb));
|
|
|
|
ASSERT(RtlParent(LinkB) == LinkB);
|
|
|
|
ASSERT(RtlLeftChild(LinkB) == RtlLeftChild(&Ta));
|
|
|
|
ASSERT(RtlRightChild(LinkB) == RtlRightChild(&Ta));
|
|
|
|
}
|
2014-10-18 23:59:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (RtlParent(&Tb) == LinkA)
|
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
// LinkA = B, LinkB = A
|
|
|
|
// B A S S.L S.R Q.P Q Q.R
|
|
|
|
ASSERT(RtlParent(LinkA) == LinkB);
|
|
|
|
ASSERT(RtlLeftChild(LinkA) == RtlLeftChild(&Tb));
|
|
|
|
ASSERT(RtlRightChild(LinkA) == RtlRightChild(&Tb));
|
|
|
|
ASSERT(RtlParent(LinkB) == RtlParent(&Ta));
|
|
|
|
ASSERT(RtlLeftChild(LinkB) == (LeftB ? LinkA : RtlLeftChild(&Ta)));
|
|
|
|
ASSERT(RtlRightChild(LinkB) == (LeftB ? RtlRightChild(&Ta) : LinkA));
|
2014-10-18 23:59:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-01 03:23:53 +00:00
|
|
|
// LinkA = A, LinkB = C
|
|
|
|
// A C S.P S.L S.R Q.P Q.L Q.R
|
|
|
|
ASSERT(!memcmp(LinkA, &Tb, sizeof(Tb)));
|
|
|
|
ASSERT(!memcmp(LinkB, &Ta, sizeof(Ta)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2009-01-23 12:20:37 +00:00
|
|
|
}
|
|
|
|
|
2004-08-05 18:17:37 +00:00
|
|
|
/*
|
2005-11-09 02:16:03 +00:00
|
|
|
* @implemented
|
|
|
|
*/
|
2004-08-05 18:17:37 +00:00
|
|
|
PRTL_SPLAY_LINKS
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2005-11-09 02:16:03 +00:00
|
|
|
RtlDelete(PRTL_SPLAY_LINKS Links)
|
2004-08-05 18:17:37 +00:00
|
|
|
{
|
2005-11-09 02:16:03 +00:00
|
|
|
PRTL_SPLAY_LINKS N, P, C, SP;
|
|
|
|
N = Links;
|
|
|
|
|
|
|
|
/* Check if we have two children */
|
2014-10-18 23:51:35 +00:00
|
|
|
if (RtlLeftChild(N) && RtlRightChild(N))
|
2005-11-09 02:16:03 +00:00
|
|
|
{
|
|
|
|
/* Get the predecessor */
|
|
|
|
SP = RtlSubtreePredecessor(N);
|
|
|
|
|
2014-10-18 23:51:35 +00:00
|
|
|
/* Swap it with N, this will guarantee that N will only have a child */
|
2009-01-23 12:20:37 +00:00
|
|
|
SwapSplayLinks(SP, N);
|
2005-11-09 02:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have no children */
|
2014-10-18 23:51:35 +00:00
|
|
|
if (!RtlLeftChild(N) && !RtlRightChild(N))
|
2005-11-09 02:16:03 +00:00
|
|
|
{
|
|
|
|
/* If we are also the root, then the tree is gone */
|
2008-10-06 10:50:22 +00:00
|
|
|
if (RtlIsRoot(N)) return NULL;
|
2005-11-09 02:16:03 +00:00
|
|
|
|
|
|
|
/* Get our parent */
|
|
|
|
P = RtlParent(N);
|
|
|
|
|
|
|
|
/* Find out who is referencing us and delete the reference */
|
|
|
|
if (RtlIsLeftChild(N))
|
|
|
|
{
|
|
|
|
/* N was a left child, so erase its parent's left child link */
|
2014-10-18 23:51:35 +00:00
|
|
|
RtlLeftChild(P) = NULL;
|
2005-11-09 02:16:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* N was a right child, so erase its parent's right child link */
|
2014-10-18 23:51:35 +00:00
|
|
|
RtlRightChild(P) = NULL;
|
2005-11-09 02:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And finally splay the parent */
|
|
|
|
return RtlSplay(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we got here, we have a child (not two: we swapped above!) */
|
|
|
|
if (RtlLeftChild(N))
|
|
|
|
{
|
|
|
|
/* We have a left child, so get it */
|
|
|
|
C = RtlLeftChild(N);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-18 23:51:35 +00:00
|
|
|
/* We have a right child, get it instead */
|
2005-11-09 02:16:03 +00:00
|
|
|
C = RtlRightChild(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are the root entry */
|
|
|
|
if (RtlIsRoot(N))
|
|
|
|
{
|
2014-10-18 23:51:35 +00:00
|
|
|
/* Our child is now root, return it */
|
|
|
|
RtlParent(C) = C;
|
2005-11-09 02:16:03 +00:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2014-10-18 23:51:35 +00:00
|
|
|
/* Get our parent */
|
|
|
|
P = RtlParent(N);
|
|
|
|
|
2005-11-09 02:16:03 +00:00
|
|
|
/* Find out who is referencing us and link to our child instead */
|
|
|
|
if (RtlIsLeftChild(N))
|
|
|
|
{
|
|
|
|
/* N was a left child, so set its parent's left child as our child */
|
2014-10-18 23:51:35 +00:00
|
|
|
RtlLeftChild(P) = C;
|
2005-11-09 02:16:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* N was a right child, so set its parent's right child as our child */
|
2014-10-18 23:51:35 +00:00
|
|
|
RtlRightChild(P) = C;
|
2005-11-09 02:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, inherit our parent and splay the parent */
|
2014-10-18 23:51:35 +00:00
|
|
|
RtlParent(C) = P;
|
|
|
|
return RtlSplay(P);
|
2004-08-05 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-10-18 23:51:35 +00:00
|
|
|
* @implemented
|
|
|
|
*/
|
2004-08-05 18:17:37 +00:00
|
|
|
VOID
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2009-01-23 12:20:37 +00:00
|
|
|
RtlDeleteNoSplay(PRTL_SPLAY_LINKS Links,
|
|
|
|
PRTL_SPLAY_LINKS *Root)
|
2004-08-05 18:17:37 +00:00
|
|
|
{
|
2014-10-18 23:51:35 +00:00
|
|
|
PRTL_SPLAY_LINKS N, P, C, SP;
|
|
|
|
N = Links;
|
|
|
|
|
|
|
|
/* Check if we have two children */
|
|
|
|
if (RtlLeftChild(N) && RtlRightChild(N))
|
|
|
|
{
|
|
|
|
/* Get the predecessor */
|
|
|
|
SP = RtlSubtreePredecessor(N);
|
|
|
|
|
|
|
|
/* If we are the root, the new root will be our predecessor after swapping */
|
|
|
|
if (RtlIsRoot(N)) *Root = SP;
|
|
|
|
|
|
|
|
/* Swap the predecessor with N, this will guarantee that N will only have a child */
|
|
|
|
SwapSplayLinks(SP, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have no children */
|
|
|
|
if (!RtlLeftChild(N) && !RtlRightChild(N))
|
|
|
|
{
|
|
|
|
/* If we are also the root, then the tree is gone */
|
|
|
|
if (RtlIsRoot(N))
|
|
|
|
{
|
|
|
|
*Root = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get our parent */
|
|
|
|
P = RtlParent(N);
|
|
|
|
|
|
|
|
/* Find out who is referencing us and delete the reference */
|
|
|
|
if (RtlIsLeftChild(N))
|
|
|
|
{
|
|
|
|
/* N was a left child, so erase its parent's left child link */
|
|
|
|
RtlLeftChild(P) = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* N was a right child, so erase its parent's right child link */
|
|
|
|
RtlRightChild(P) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are done */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we got here, we have a child (not two: we swapped above!) */
|
|
|
|
if (RtlLeftChild(N))
|
|
|
|
{
|
|
|
|
/* We have a left child, so get it */
|
|
|
|
C = RtlLeftChild(N);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We have a right child, get it instead */
|
|
|
|
C = RtlRightChild(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are the root entry */
|
|
|
|
if (RtlIsRoot(N))
|
|
|
|
{
|
|
|
|
/* Our child is now root, return it */
|
|
|
|
RtlParent(C) = C;
|
|
|
|
*Root = C;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get our parent */
|
|
|
|
P = RtlParent(N);
|
|
|
|
|
|
|
|
/* Find out who is referencing us and link to our child instead */
|
|
|
|
if (RtlIsLeftChild(N))
|
|
|
|
{
|
|
|
|
/* N was a left child, so set its parent's left child as our child */
|
|
|
|
RtlLeftChild(P) = C;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* N was a right child, so set its parent's right child as our child */
|
|
|
|
RtlRightChild(P) = C;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, inherit our parent and we are done */
|
|
|
|
RtlParent(C) = P;
|
|
|
|
return;
|
2004-08-05 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-10-18 23:59:25 +00:00
|
|
|
* @implemented
|
|
|
|
*/
|
2004-08-05 18:17:37 +00:00
|
|
|
PRTL_SPLAY_LINKS
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2005-11-09 01:39:39 +00:00
|
|
|
RtlRealPredecessor(PRTL_SPLAY_LINKS Links)
|
2004-08-05 18:17:37 +00:00
|
|
|
{
|
2005-11-09 01:39:39 +00:00
|
|
|
PRTL_SPLAY_LINKS Child;
|
|
|
|
|
|
|
|
/* Get the left child */
|
|
|
|
Child = RtlLeftChild(Links);
|
|
|
|
if (Child)
|
|
|
|
{
|
|
|
|
/* Get right-most child */
|
|
|
|
while (RtlRightChild(Child)) Child = RtlRightChild(Child);
|
|
|
|
return Child;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't have a left child, keep looping until we find our parent */
|
|
|
|
Child = Links;
|
|
|
|
while (RtlIsLeftChild(Child)) Child = RtlParent(Child);
|
|
|
|
|
|
|
|
/* The parent should be a right child, return the real predecessor */
|
|
|
|
if (RtlIsRightChild(Child)) return RtlParent(Child);
|
|
|
|
|
|
|
|
/* The parent isn't a right child, so no real precessor for us */
|
|
|
|
return NULL;
|
2004-08-05 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-10-18 23:59:25 +00:00
|
|
|
* @implemented
|
|
|
|
*/
|
2004-08-05 18:17:37 +00:00
|
|
|
PRTL_SPLAY_LINKS
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2005-11-09 01:39:39 +00:00
|
|
|
RtlRealSuccessor(PRTL_SPLAY_LINKS Links)
|
2004-08-05 18:17:37 +00:00
|
|
|
{
|
2005-11-09 01:39:39 +00:00
|
|
|
PRTL_SPLAY_LINKS Child;
|
|
|
|
|
|
|
|
/* Get the right child */
|
|
|
|
Child = RtlRightChild(Links);
|
|
|
|
if (Child)
|
|
|
|
{
|
|
|
|
/* Get left-most child */
|
|
|
|
while (RtlLeftChild(Child)) Child = RtlLeftChild(Child);
|
|
|
|
return Child;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't have a right child, keep looping until we find our parent */
|
|
|
|
Child = Links;
|
|
|
|
while (RtlIsRightChild(Child)) Child = RtlParent(Child);
|
|
|
|
|
|
|
|
/* The parent should be a left child, return the real successor */
|
|
|
|
if (RtlIsLeftChild(Child)) return RtlParent(Child);
|
|
|
|
|
|
|
|
/* The parent isn't a right child, so no real successor for us */
|
|
|
|
return NULL;
|
2004-08-05 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-10-18 23:59:25 +00:00
|
|
|
* @implemented
|
|
|
|
*/
|
2004-08-05 18:17:37 +00:00
|
|
|
PRTL_SPLAY_LINKS
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2005-11-08 22:45:45 +00:00
|
|
|
RtlSplay(PRTL_SPLAY_LINKS Links)
|
2004-08-05 18:17:37 +00:00
|
|
|
{
|
2005-11-08 22:45:45 +00:00
|
|
|
/*
|
|
|
|
* Implementation Notes (http://en.wikipedia.org/wiki/Splay_tree):
|
|
|
|
*
|
|
|
|
* To do a splay, we carry out a sequence of rotations,
|
|
|
|
* each of which moves the target node N closer to the root.
|
|
|
|
*
|
|
|
|
* Each particular step depends on only two factors:
|
|
|
|
* - Whether N is the left or right child of its parent node, P,
|
|
|
|
* - Whether P is the left or right child of its parent, G (for grandparent node).
|
|
|
|
*
|
|
|
|
* Thus, there are four cases:
|
|
|
|
* - Case 1: N is the left child of P and P is the left child of G.
|
|
|
|
* In this case we perform a double right rotation, so that
|
|
|
|
* P becomes N's right child, and G becomes P's right child.
|
|
|
|
*
|
|
|
|
* - Case 2: N is the right child of P and P is the right child of G.
|
|
|
|
* In this case we perform a double left rotation, so that
|
|
|
|
* P becomes N's left child, and G becomes P's left child.
|
|
|
|
*
|
|
|
|
* - Case 3: N is the left child of P and P is the right child of G.
|
|
|
|
* In this case we perform a rotation so that
|
|
|
|
* G becomes N's left child, and P becomes N's right child.
|
|
|
|
*
|
|
|
|
* - Case 4: N is the right child of P and P is the left child of G.
|
|
|
|
* In this case we perform a rotation so that
|
|
|
|
* P becomes N's left child, and G becomes N's right child.
|
|
|
|
*
|
|
|
|
* Finally, if N doesn't have a grandparent node, we simply perform a
|
2007-10-19 23:21:45 +00:00
|
|
|
* left or right rotation to move it to the root.
|
2005-11-08 22:45:45 +00:00
|
|
|
*
|
|
|
|
* By performing a splay on the node of interest after every operation,
|
|
|
|
* we keep recently accessed nodes near the root and keep the tree
|
|
|
|
* roughly balanced, so that we achieve the desired amortized time bounds.
|
|
|
|
*/
|
2005-11-08 22:54:39 +00:00
|
|
|
PRTL_SPLAY_LINKS N, P, G;
|
|
|
|
|
|
|
|
/* N is the item we'll be playing with */
|
|
|
|
N = Links;
|
|
|
|
|
|
|
|
/* Let the algorithm run until N becomes the root entry */
|
|
|
|
while (!RtlIsRoot(N))
|
|
|
|
{
|
|
|
|
/* Now get the parent and grand-parent */
|
|
|
|
P = RtlParent(N);
|
|
|
|
G = RtlParent(P);
|
|
|
|
|
|
|
|
/* Case 1 & 3: N is left child of P */
|
|
|
|
if (RtlIsLeftChild(N))
|
|
|
|
{
|
|
|
|
/* Case 1: P is the left child of G */
|
|
|
|
if (RtlIsLeftChild(P))
|
|
|
|
{
|
2005-11-08 23:33:07 +00:00
|
|
|
/*
|
|
|
|
* N's right-child becomes P's left child and
|
|
|
|
* P's right-child becomes G's left child.
|
|
|
|
*/
|
|
|
|
RtlLeftChild(P) = RtlRightChild(N);
|
|
|
|
RtlLeftChild(G) = RtlRightChild(P);
|
|
|
|
|
|
|
|
/*
|
2007-10-19 23:21:45 +00:00
|
|
|
* If they exist, update their parent pointers too,
|
2005-11-08 23:40:37 +00:00
|
|
|
* since they've changed trees.
|
2005-11-08 23:33:07 +00:00
|
|
|
*/
|
|
|
|
if (RtlLeftChild(P)) RtlParent(RtlLeftChild(P)) = P;
|
|
|
|
if (RtlLeftChild(G)) RtlParent(RtlLeftChild(G)) = G;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-11-09 02:17:18 +00:00
|
|
|
RtlRightChild(RtlParent(G)) = N;
|
2005-11-08 23:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-08 23:40:37 +00:00
|
|
|
/* Now N is on top, so P has become its child. */
|
2005-11-08 23:33:07 +00:00
|
|
|
RtlRightChild(N) = P;
|
|
|
|
RtlParent(P) = N;
|
|
|
|
|
2005-11-08 23:40:37 +00:00
|
|
|
/* N is on top, P is its child, so G is grandchild. */
|
2005-11-08 23:33:07 +00:00
|
|
|
RtlRightChild(P) = G;
|
|
|
|
RtlParent(G) = P;
|
2005-11-08 22:54:39 +00:00
|
|
|
}
|
|
|
|
/* Case 3: P is the right child of G */
|
|
|
|
else if (RtlIsRightChild(P))
|
|
|
|
{
|
2005-11-08 23:40:37 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
/*
|
2007-10-19 23:21:45 +00:00
|
|
|
* If they exist, update their parent pointers too,
|
2005-11-08 23:40:37 +00:00
|
|
|
* 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);
|
2005-11-08 22:54:39 +00:00
|
|
|
|
2005-11-08 23:40:37 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-11-09 02:17:18 +00:00
|
|
|
RtlRightChild(RtlParent(G)) = N;
|
2005-11-08 23:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2005-11-08 22:54:39 +00:00
|
|
|
}
|
|
|
|
/* "Finally" case: N doesn't have a grandparent => P is root */
|
|
|
|
else
|
|
|
|
{
|
2005-11-08 23:51:46 +00:00
|
|
|
/* P's left-child becomes N's right child */
|
|
|
|
RtlLeftChild(P) = RtlRightChild(N);
|
|
|
|
|
|
|
|
/* If it exists, update its parent pointer too */
|
|
|
|
if (RtlLeftChild(P)) RtlParent(RtlLeftChild(P)) = P;
|
2005-11-08 22:54:39 +00:00
|
|
|
|
2005-11-08 23:51:46 +00:00
|
|
|
/* Now make N the root, no need to worry about references */
|
|
|
|
N->Parent = N;
|
|
|
|
|
|
|
|
/* And make P its right child */
|
|
|
|
N->RightChild = P;
|
|
|
|
P->Parent = N;
|
2005-11-08 22:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Case 2 & 4: N is right child of P */
|
|
|
|
else
|
|
|
|
{
|
2005-11-08 23:42:40 +00:00
|
|
|
/* Case 2: P is the right child of G */
|
|
|
|
if (RtlIsRightChild(P))
|
2005-11-08 22:54:39 +00:00
|
|
|
{
|
2005-11-08 23:47:25 +00:00
|
|
|
/*
|
|
|
|
* P's left-child becomes G's right child and
|
|
|
|
* N's left-child becomes P's right child.
|
|
|
|
*/
|
|
|
|
RtlRightChild(G) = RtlLeftChild(P);
|
|
|
|
RtlRightChild(P) = RtlLeftChild(N);
|
2005-11-08 22:54:39 +00:00
|
|
|
|
2005-11-08 23:47:25 +00:00
|
|
|
/*
|
2007-10-19 23:21:45 +00:00
|
|
|
* If they exist, update their parent pointers too,
|
2005-11-08 23:47:25 +00:00
|
|
|
* since they've changed trees.
|
|
|
|
*/
|
|
|
|
if (RtlRightChild(G)) RtlParent(RtlRightChild(G)) = G;
|
|
|
|
if (RtlRightChild(P)) RtlParent(RtlRightChild(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.
|
|
|
|
*/
|
2005-11-09 02:17:18 +00:00
|
|
|
RtlRightChild(RtlParent(G)) = N;
|
2005-11-08 23:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now N is on top, so P has become its child. */
|
|
|
|
RtlLeftChild(N) = P;
|
|
|
|
RtlParent(P) = N;
|
|
|
|
|
|
|
|
/* N is on top, P is its child, so G is grandchild. */
|
|
|
|
RtlLeftChild(P) = G;
|
|
|
|
RtlParent(G) = P;
|
2005-11-08 22:54:39 +00:00
|
|
|
}
|
2005-11-08 23:42:40 +00:00
|
|
|
/* Case 4: P is the left child of G */
|
|
|
|
else if (RtlIsLeftChild(P))
|
2005-11-08 22:54:39 +00:00
|
|
|
{
|
2005-11-08 23:47:25 +00:00
|
|
|
/*
|
|
|
|
* N's left-child becomes G's right child and
|
|
|
|
* N's right-child becomes P's left child.
|
|
|
|
*/
|
|
|
|
RtlRightChild(P) = RtlLeftChild(N);
|
|
|
|
RtlLeftChild(G) = RtlRightChild(N);
|
2005-11-08 22:54:39 +00:00
|
|
|
|
2005-11-08 23:47:25 +00:00
|
|
|
/*
|
2007-10-19 23:21:45 +00:00
|
|
|
* If they exist, update their parent pointers too,
|
2005-11-08 23:47:25 +00:00
|
|
|
* since they've changed trees.
|
|
|
|
*/
|
|
|
|
if (RtlRightChild(P)) RtlParent(RtlRightChild(P)) = P;
|
|
|
|
if (RtlLeftChild(G)) RtlParent(RtlLeftChild(G)) = G;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-11-09 02:17:18 +00:00
|
|
|
RtlRightChild(RtlParent(G)) = N;
|
2005-11-08 23:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now N is on top, so P has become its left child. */
|
|
|
|
RtlLeftChild(N) = P;
|
|
|
|
RtlParent(G) = N;
|
|
|
|
|
|
|
|
/* N is on top, P is its left child, so G is right child. */
|
|
|
|
RtlRightChild(N) = G;
|
|
|
|
RtlParent(P) = N;
|
2005-11-08 22:54:39 +00:00
|
|
|
}
|
|
|
|
/* "Finally" case: N doesn't have a grandparent => P is root */
|
|
|
|
else
|
|
|
|
{
|
2005-11-08 23:51:46 +00:00
|
|
|
/* P's right-child becomes N's left child */
|
|
|
|
RtlRightChild(P) = RtlLeftChild(N);
|
|
|
|
|
|
|
|
/* If it exists, update its parent pointer too */
|
|
|
|
if (RtlRightChild(P)) RtlParent(RtlRightChild(P)) = P;
|
|
|
|
|
|
|
|
/* Now make N the root, no need to worry about references */
|
|
|
|
N->Parent = N;
|
2005-11-08 22:54:39 +00:00
|
|
|
|
2005-11-08 23:51:46 +00:00
|
|
|
/* And make P its left child */
|
|
|
|
N->LeftChild = P;
|
|
|
|
P->Parent = N;
|
2005-11-08 22:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 12:20:37 +00:00
|
|
|
/* Return the root entry */
|
2011-08-01 03:23:53 +00:00
|
|
|
ASSERT(RtlIsRoot(N));
|
2009-01-23 12:20:37 +00:00
|
|
|
return N;
|
2004-08-05 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-10-18 23:59:25 +00:00
|
|
|
* @implemented
|
|
|
|
*/
|
2005-11-09 01:05:00 +00:00
|
|
|
PRTL_SPLAY_LINKS
|
|
|
|
NTAPI
|
|
|
|
RtlSubtreePredecessor(IN PRTL_SPLAY_LINKS Links)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2005-11-09 01:05:00 +00:00
|
|
|
PRTL_SPLAY_LINKS Child;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2005-11-09 01:05:00 +00:00
|
|
|
/* Get the left child */
|
|
|
|
Child = RtlLeftChild(Links);
|
|
|
|
if (!Child) return NULL;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2005-11-09 01:05:00 +00:00
|
|
|
/* Get right-most child */
|
|
|
|
while (RtlRightChild(Child)) Child = RtlRightChild(Child);
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2005-11-09 01:05:00 +00:00
|
|
|
/* Return it */
|
|
|
|
return Child;
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
2004-08-05 18:17:37 +00:00
|
|
|
/*
|
2014-10-18 23:59:25 +00:00
|
|
|
* @implemented
|
|
|
|
*/
|
2005-11-09 01:05:00 +00:00
|
|
|
PRTL_SPLAY_LINKS
|
|
|
|
NTAPI
|
|
|
|
RtlSubtreeSuccessor(IN PRTL_SPLAY_LINKS Links)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2005-11-09 01:05:00 +00:00
|
|
|
PRTL_SPLAY_LINKS Child;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2005-11-09 01:05:00 +00:00
|
|
|
/* Get the right child */
|
|
|
|
Child = RtlRightChild(Links);
|
|
|
|
if (!Child) return NULL;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2005-11-09 01:05:00 +00:00
|
|
|
/* Get left-most child */
|
|
|
|
while (RtlLeftChild(Child)) Child = RtlLeftChild(Child);
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2005-11-09 01:05:00 +00:00
|
|
|
/* Return it */
|
|
|
|
return Child;
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|