2010-10-14 01:27:31 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2000-09-18 11:07:07 -05:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-11 08:43:26 -05:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2010-02-12 08:01:35 -06:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2008-04-11 08:43:26 -05:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2008-04-11 08:43:26 -05:00
|
|
|
* This file is part of OpenOffice.org.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2008-04-11 08:43:26 -05:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2008-04-11 08:43:26 -05:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
2008-04-11 08:43:26 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2000-09-18 11:07:07 -05:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2006-09-17 10:09:08 -05:00
|
|
|
|
2000-09-18 11:07:07 -05:00
|
|
|
|
|
|
|
#include "stgavl.hxx"
|
|
|
|
|
|
|
|
StgAvlNode::StgAvlNode()
|
|
|
|
{
|
|
|
|
pLeft = pRight = NULL;
|
|
|
|
nBalance = nId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
StgAvlNode::~StgAvlNode()
|
|
|
|
{
|
|
|
|
delete pLeft;
|
|
|
|
delete pRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlNode::Find( StgAvlNode* pFind )
|
|
|
|
{
|
|
|
|
StgAvlNode* p = this;
|
|
|
|
while( p )
|
|
|
|
{
|
|
|
|
short nRes = p->Compare( pFind );
|
|
|
|
if( !nRes )
|
|
|
|
return p;
|
|
|
|
else p = ( nRes < 0 ) ? p->pLeft : p->pRight;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find point to add node to AVL tree and returns
|
|
|
|
// +/0/- for >/=/< previous
|
|
|
|
|
|
|
|
short StgAvlNode::Locate
|
|
|
|
( StgAvlNode* pFind,
|
|
|
|
StgAvlNode** pPivot, StgAvlNode **pParent, StgAvlNode** pPrev )
|
|
|
|
{
|
2004-02-04 05:30:58 -06:00
|
|
|
short nRes = 0;
|
2000-09-18 11:07:07 -05:00
|
|
|
StgAvlNode* pCur = this;
|
|
|
|
*pParent = *pPrev = NULL;
|
|
|
|
*pPivot = this;
|
|
|
|
|
|
|
|
// search tree for insertion point
|
|
|
|
|
|
|
|
while( pCur != NULL )
|
|
|
|
{
|
|
|
|
// check for pPivot
|
|
|
|
if( pCur->nBalance != 0 )
|
|
|
|
*pPivot = pCur, *pParent = *pPrev;
|
|
|
|
// save pPrev location and see what direction to go
|
|
|
|
*pPrev = pCur;
|
|
|
|
nRes = pCur->Compare( pFind );
|
|
|
|
if( nRes == 0 )
|
|
|
|
break;
|
|
|
|
else pCur = ( nRes < 0 ) ? pCur->pLeft : pCur->pRight;
|
|
|
|
}
|
|
|
|
return( nRes );
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust balance factors in AVL tree from pivot down.
|
|
|
|
// Returns delta balance.
|
|
|
|
|
|
|
|
short StgAvlNode::Adjust( StgAvlNode** pHeavy, StgAvlNode* pNew )
|
|
|
|
{
|
|
|
|
StgAvlNode* pCur = this;
|
|
|
|
short nDelta;
|
|
|
|
// no traversing
|
|
|
|
if( pCur == pNew )
|
|
|
|
return nBalance;
|
|
|
|
short nRes = Compare( pNew );
|
|
|
|
if( nRes > 0 )
|
|
|
|
{
|
|
|
|
*pHeavy = pCur = pRight;
|
|
|
|
nDelta = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pHeavy = pCur = pLeft;
|
|
|
|
nDelta = 1;
|
|
|
|
}
|
|
|
|
nBalance = 0;
|
|
|
|
while( pCur != pNew )
|
|
|
|
{
|
|
|
|
nRes = pCur->Compare( pNew );
|
|
|
|
if( nRes > 0 )
|
|
|
|
{
|
|
|
|
// height of right increases by 1
|
|
|
|
pCur->nBalance = -1;
|
|
|
|
pCur = pCur->pRight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// height of left increases by 1
|
|
|
|
pCur->nBalance = 1;
|
|
|
|
pCur = pCur->pLeft;
|
|
|
|
}
|
|
|
|
}
|
2006-06-19 23:53:53 -05:00
|
|
|
nBalance = nBalance + nDelta;
|
2000-09-18 11:07:07 -05:00
|
|
|
return nDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform LL rotation and return new root
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlNode::RotLL()
|
|
|
|
{
|
|
|
|
StgAvlNode *pHeavy = pLeft;
|
|
|
|
pLeft = pHeavy->pRight;
|
|
|
|
pHeavy->pRight = this;
|
|
|
|
pHeavy->nBalance = nBalance = 0;
|
|
|
|
return pHeavy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform LR rotation and return new root
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlNode::RotLR()
|
|
|
|
{
|
|
|
|
|
|
|
|
StgAvlNode* pHeavy = pLeft;
|
|
|
|
StgAvlNode* pNewRoot = pHeavy->pRight;
|
|
|
|
|
|
|
|
pHeavy->pRight = pNewRoot->pLeft;
|
|
|
|
pLeft = pNewRoot->pRight;
|
|
|
|
pNewRoot->pLeft = pHeavy;
|
|
|
|
pNewRoot->pRight = this;
|
|
|
|
|
|
|
|
switch( pNewRoot->nBalance )
|
|
|
|
{
|
|
|
|
case 1: // LR( b )
|
|
|
|
nBalance = -1;
|
|
|
|
pHeavy->nBalance = 0;
|
|
|
|
break;
|
|
|
|
case -1: // LR( c )
|
|
|
|
pHeavy->nBalance = 1;
|
|
|
|
nBalance = 0;
|
|
|
|
break;
|
|
|
|
case 0: // LR( a )
|
|
|
|
nBalance = 0;
|
|
|
|
pHeavy->nBalance = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pNewRoot->nBalance = 0;
|
|
|
|
return pNewRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform RR rotation and return new root
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlNode::RotRR()
|
|
|
|
{
|
|
|
|
StgAvlNode* pHeavy = pRight;
|
|
|
|
pRight = pHeavy->pLeft;
|
|
|
|
pHeavy->pLeft = this;
|
|
|
|
nBalance = pHeavy->nBalance = 0;
|
|
|
|
return pHeavy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform the RL rotation and return the new root
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlNode::RotRL()
|
|
|
|
{
|
|
|
|
StgAvlNode* pHeavy = pRight;
|
|
|
|
StgAvlNode* pNewRoot = pHeavy->pLeft;
|
|
|
|
pHeavy->pLeft = pNewRoot->pRight;
|
|
|
|
pRight = pNewRoot->pLeft;
|
|
|
|
pNewRoot->pRight = pHeavy;
|
|
|
|
pNewRoot->pLeft = this;
|
|
|
|
switch( pNewRoot->nBalance )
|
|
|
|
{
|
|
|
|
case -1: // RL( b )
|
|
|
|
nBalance = 1;
|
|
|
|
pHeavy->nBalance = 0;
|
|
|
|
break;
|
|
|
|
case 1: // RL( c )
|
|
|
|
pHeavy->nBalance = -1;
|
|
|
|
nBalance = 0;
|
|
|
|
break;
|
|
|
|
case 0: // RL( a )
|
|
|
|
nBalance = 0;
|
|
|
|
pHeavy->nBalance = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pNewRoot->nBalance = 0;
|
|
|
|
return pNewRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a tree element. Return the removed element or NULL.
|
|
|
|
|
2010-07-28 21:56:19 -05:00
|
|
|
StgAvlNode* StgAvlNode::Rem( StgAvlNode** p, StgAvlNode* pDel, sal_Bool bPtrs )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
|
|
|
if( *p )
|
|
|
|
{
|
|
|
|
StgAvlNode* pCur = *p;
|
2006-06-19 23:53:53 -05:00
|
|
|
short nRes = bPtrs ? short( pCur == pDel ) : short(pCur->Compare( pDel ));
|
2000-09-18 11:07:07 -05:00
|
|
|
if( !nRes )
|
|
|
|
{
|
|
|
|
// Element found: remove
|
|
|
|
if( !pCur->pRight )
|
|
|
|
{
|
|
|
|
*p = pCur->pLeft; pCur->pLeft = NULL;
|
|
|
|
}
|
|
|
|
else if( !pCur->pLeft )
|
|
|
|
{
|
|
|
|
*p = pCur->pRight; pCur->pRight = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The damn element has two leaves. Get the
|
|
|
|
// rightmost element of the left subtree (which
|
|
|
|
// is lexically before this element) and replace
|
|
|
|
// this element with the element found.
|
|
|
|
StgAvlNode* last = pCur;
|
|
|
|
StgAvlNode* l;
|
|
|
|
for( l = pCur->pLeft;
|
|
|
|
l->pRight; last = l, l = l->pRight ) {}
|
|
|
|
// remove the element from chain
|
|
|
|
if( l == last->pRight )
|
|
|
|
last->pRight = l->pLeft;
|
|
|
|
else
|
|
|
|
last->pLeft = l->pLeft;
|
|
|
|
// perform the replacement
|
|
|
|
l->pLeft = pCur->pLeft;
|
|
|
|
l->pRight = pCur->pRight;
|
|
|
|
*p = l;
|
|
|
|
// delete the element
|
|
|
|
pCur->pLeft = pCur->pRight = NULL;
|
|
|
|
}
|
|
|
|
return pCur;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( nRes < 0 )
|
|
|
|
return Rem( &pCur->pLeft, pDel, bPtrs );
|
|
|
|
else
|
|
|
|
return Rem( &pCur->pRight, pDel, bPtrs );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enumerate the tree for later iteration
|
|
|
|
|
2006-06-19 23:53:53 -05:00
|
|
|
void StgAvlNode::StgEnum( short& n )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
|
|
|
if( this )
|
|
|
|
{
|
|
|
|
if( pLeft )
|
2006-06-19 23:53:53 -05:00
|
|
|
pLeft->StgEnum( n );
|
2000-09-18 11:07:07 -05:00
|
|
|
nId = n++;
|
|
|
|
if( pRight )
|
2006-06-19 23:53:53 -05:00
|
|
|
pRight->StgEnum( n );
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add node to AVL tree.
|
2010-07-28 21:56:19 -05:00
|
|
|
// Return sal_False if the element already exists.
|
2000-09-18 11:07:07 -05:00
|
|
|
|
2010-07-28 21:56:19 -05:00
|
|
|
sal_Bool StgAvlNode::Insert( StgAvlNode** pRoot, StgAvlNode* pIns )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
|
|
|
StgAvlNode* pPivot, *pHeavy, *pNewRoot, *pParent, *pPrev;
|
|
|
|
// special case - empty tree
|
|
|
|
if( *pRoot == NULL )
|
|
|
|
{
|
|
|
|
*pRoot = pIns;
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_True;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
// find insertion point and return if already present
|
|
|
|
short nRes = (*pRoot)->Locate( pIns, &pPivot, &pParent, &pPrev );
|
|
|
|
if( !nRes )
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_False;
|
2000-09-18 11:07:07 -05:00
|
|
|
// add new node
|
|
|
|
if( nRes < 0 )
|
|
|
|
pPrev->pLeft = pIns;
|
|
|
|
else
|
|
|
|
pPrev->pRight = pIns;
|
|
|
|
// rebalance tree
|
|
|
|
short nDelta = pPivot->Adjust( &pHeavy, pIns );
|
|
|
|
if( pPivot->nBalance >= 2 || pPivot->nBalance <= -2 )
|
|
|
|
{
|
|
|
|
pHeavy = ( nDelta < 0 ) ? pPivot->pRight : pPivot->pLeft;
|
|
|
|
// left imbalance
|
|
|
|
if( nDelta > 0 )
|
|
|
|
if( pHeavy->nBalance == 1 )
|
|
|
|
pNewRoot = pPivot->RotLL();
|
|
|
|
else
|
|
|
|
pNewRoot = pPivot->RotLR();
|
|
|
|
// right imbalance
|
|
|
|
else if( pHeavy->nBalance == -1 )
|
|
|
|
pNewRoot = pPivot->RotRR();
|
|
|
|
else
|
|
|
|
pNewRoot = pPivot->RotRL();
|
|
|
|
// relink balanced subtree
|
|
|
|
if( pParent == NULL )
|
|
|
|
*pRoot = pNewRoot;
|
|
|
|
else if( pPivot == pParent->pLeft )
|
|
|
|
pParent->pLeft = pNewRoot;
|
|
|
|
else if( pPivot == pParent->pRight )
|
|
|
|
pParent->pRight = pNewRoot;
|
|
|
|
}
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_True;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
|
2010-07-28 21:56:19 -05:00
|
|
|
// Remove node from tree. Returns sal_True is found and removed.
|
2000-09-18 11:07:07 -05:00
|
|
|
// Actually delete if bDel
|
|
|
|
|
2010-07-28 21:56:19 -05:00
|
|
|
sal_Bool StgAvlNode::Remove( StgAvlNode** pRoot, StgAvlNode* pDel, sal_Bool bDel )
|
2000-09-18 11:07:07 -05:00
|
|
|
{
|
|
|
|
// special case - empty tree
|
|
|
|
if( *pRoot == NULL )
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_False;
|
2000-09-18 11:07:07 -05:00
|
|
|
// delete the element
|
2010-07-28 21:56:19 -05:00
|
|
|
pDel = Rem( pRoot, pDel, sal_False );
|
2000-09-18 11:07:07 -05:00
|
|
|
if( pDel )
|
|
|
|
{
|
|
|
|
if( bDel )
|
|
|
|
delete pDel;
|
|
|
|
// Rebalance the tree the hard way
|
|
|
|
// OS 22.09.95: Auf MD's Wunsch auskommentiert wg. Absturz
|
|
|
|
/* StgAvlNode* pNew = NULL;
|
|
|
|
while( *pRoot )
|
|
|
|
{
|
2010-07-28 21:56:19 -05:00
|
|
|
StgAvlNode* p = Rem( pRoot, *pRoot, sal_False );
|
2000-09-18 11:07:07 -05:00
|
|
|
Insert( &pNew, p );
|
|
|
|
}
|
|
|
|
*pRoot = pNew;*/
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_True;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
else
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_False;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
|
2010-07-28 21:56:19 -05:00
|
|
|
// Move node to a different tree. Returns sal_True is found and moved. This routine
|
2000-09-18 11:07:07 -05:00
|
|
|
// may be called when the key has changed.
|
|
|
|
|
2010-07-28 21:56:19 -05:00
|
|
|
sal_Bool StgAvlNode::Move
|
2000-09-18 11:07:07 -05:00
|
|
|
( StgAvlNode** pRoot1, StgAvlNode** pRoot2, StgAvlNode* pMove )
|
|
|
|
{
|
|
|
|
// special case - empty tree
|
|
|
|
if( *pRoot1 == NULL )
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_False;
|
|
|
|
pMove = Rem( pRoot1, pMove, sal_False );
|
2000-09-18 11:07:07 -05:00
|
|
|
if( pMove )
|
|
|
|
return Insert( pRoot2, pMove );
|
|
|
|
else
|
2010-07-28 21:56:19 -05:00
|
|
|
return sal_False;
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////// class AvlIterator /////////////////////////
|
|
|
|
|
|
|
|
// The iterator walks through a tree one entry by one.
|
|
|
|
|
|
|
|
StgAvlIterator::StgAvlIterator( StgAvlNode* p )
|
|
|
|
{
|
|
|
|
pRoot = p;
|
|
|
|
nCount = 0;
|
2011-01-31 16:07:30 -06:00
|
|
|
nCur = 0;
|
2000-09-18 11:07:07 -05:00
|
|
|
if( p )
|
2006-06-19 23:53:53 -05:00
|
|
|
p->StgEnum( nCount );
|
2000-09-18 11:07:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlIterator::Find( short n )
|
|
|
|
{
|
|
|
|
StgAvlNode* p = pRoot;
|
|
|
|
while( p )
|
|
|
|
{
|
|
|
|
if( n == p->nId )
|
|
|
|
break;
|
|
|
|
else p = ( n < p->nId ) ? p->pLeft : p->pRight;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlIterator::First()
|
|
|
|
{
|
|
|
|
nCur = -1;
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlIterator::Last()
|
|
|
|
{
|
|
|
|
nCur = nCount;
|
|
|
|
return Prev();
|
|
|
|
}
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlIterator::Next()
|
|
|
|
{
|
|
|
|
return Find( ++nCur );
|
|
|
|
}
|
|
|
|
|
|
|
|
StgAvlNode* StgAvlIterator::Prev()
|
|
|
|
{
|
|
|
|
return Find( --nCur );
|
|
|
|
}
|
|
|
|
|
2010-10-14 01:27:31 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|