我的2-3树中的节点是否正确分裂?

时间:2012-08-04 22:05:17

标签: c++ data-structures 2-3-tree

我已经在我制作的树中输入了3个值,但我只能找到根节点的值不高或低。我的代码是否存在特定的错误,或者我应该重新开始,因为我完全错了?

提前感谢任何有时间提供帮助的人。

我测试了我的树:

TwoThreeTree MyTree;

MyTree.Add(20);
MyTree.Add(10);
MyTree.Add(60);

if ( MyTree.Search(20) ) return 0;
return 1;

我树的代码是:

部首:

enum LOCATION {
LOW,
HIGH,
MIDDLE
};

class TwoThreeTree {
private:
    class TwoThreeNode {
    private:
        // Currently not needed

    public:
        TwoThreeNode();
        ~TwoThreeNode();

        int     Data1, Data2;                       
        bool    TwoData;                            // Data2 is used or not?

        TwoThreeNode *Child1, *Child2, *Child3; 
        TwoThreeNode* Parent;                   

    };

    TwoThreeNode*   Root;               

public:
    TwoThreeTree();
    ~TwoThreeTree();

    void            Add(int item);              
    bool            Search(int item);           
    void            Split(TwoThreeNode* Leaf, int item, LOCATION location, TwoThreeNode* Lower, TwoThreeNode* Higher);  
    void            KillNodes(TwoThreeNode* Node); 

    int Highest(int a, int b, int c);   
    int Lowest(int a, int b, int c);    
    int Middle(int a, int b, int c);
};

来源:

#include "TwoThreeTree.h"

TwoThreeTree::TwoThreeTree() :
Root(0)
{
}

TwoThreeTree::~TwoThreeTree()
{
KillNodes( Root );
}

void TwoThreeTree::Split(TwoThreeNode* Leaf, int item, LOCATION location, TwoThreeNode* Lower, TwoThreeNode* Higher)
{
if ( !Leaf && Lower && Higher )     
{
    TwoThreeNode* NewRoot = new TwoThreeNode;

    NewRoot->Data1 = item;          
    NewRoot->Child1 = Lower;
    NewRoot->Child2 = Higher;
    Lower->Parent = NewRoot;
    Higher->Parent = NewRoot;

    Root = NewRoot;
}
else if ( Leaf && !Lower && !Higher )   
{
    TwoThreeNode* hChild = new TwoThreeNode; // higher child

    int Med = Middle( Leaf->Data1, Leaf->Data2, item );
    int High = Highest( Leaf->Data1, Leaf->Data2, item );
    int Low = Lowest( Leaf->Data1, Leaf->Data2, item );

    Leaf->Data2 = 0; Leaf->TwoData = false;     

    Leaf->Data1 = Low;  
    hChild->Data1 = High;

    Split ( Leaf->Parent, Med, location, Leaf, hChild ); 

}
else if ( Leaf && Lower && Higher )
{
    if ( Leaf->TwoData )    
    {
        if ( location == HIGH )
        {
            TwoThreeNode* hChild = new TwoThreeNode;    // higher child

            int pHigh = Highest( Leaf->Data1, Leaf->Data2, item );
            int pLow = Lowest( Leaf->Data1, Leaf->Data2, item );
            int pMed = Middle( Leaf->Data1, Leaf->Data2, item );

            hChild->Data1 = pHigh;
            hChild->Child1 = Lower;
            hChild->Child2 = Higher;    

            Lower->Parent = hChild;     
            Higher->Parent = hChild;

            Leaf->Data2 = 0; Leaf->TwoData = false;

            Leaf->Data1 = pLow;
            Leaf->Child3 = 0;

            Split( Leaf->Parent, pMed, location, Leaf, hChild ); 
        }
        else if ( location == LOW )
        {
            TwoThreeNode* lChild = new TwoThreeNode;    // lower child

            int pHigh = Highest( Leaf->Data1, Leaf->Data2, item );
            int pLow = Lowest( Leaf->Data1, Leaf->Data2, item );
            int pMed = Middle( Leaf->Data1, Leaf->Data2, item );

            lChild->Data1 = pLow;
            lChild->Child1 = Lower;
            lChild->Child2 = Higher;    

            Lower->Parent = lChild;
            Higher->Parent = lChild;

            Leaf->Data2 = 0; Leaf->TwoData = false;

            Leaf->Data1 = pHigh;
            Leaf->Child1 = Leaf->Child2;
            Leaf->Child2 = Leaf->Child3;
            Leaf->Child3 = 0;

            Split( Leaf->Parent, pMed, location, lChild, Leaf );
        }
        else if ( location == MIDDLE )
        {
            TwoThreeNode* hChild = new TwoThreeNode;

            int pHigh = Highest( Leaf->Data1, Leaf->Data2, item );
            int pLow = Lowest( Leaf->Data1, Leaf->Data2, item );
            int pMed = Middle( Leaf->Data1, Leaf->Data2, item );

            hChild->Data1 = pHigh;
            Leaf->Data1 = pLow;
            Leaf->TwoData = false;

            hChild->Child2 = Leaf->Child3;
            Leaf->Child3 = 0;
            hChild->Child1 = Higher;
            Leaf->Child2 = Lower;

            Higher->Parent = hChild;
            Lower->Parent = Leaf;

            Split( Leaf->Parent, pMed, location, Leaf, hChild );
        }
    }
    else // if parent isn't full
    {
        if ( location == HIGH )
        {
            Leaf->Child2 = Lower;
            Leaf->Child3 = Higher;
        }
        else if ( location == LOW )
        {
            Leaf->Child3 = Leaf->Child2;
            Leaf->Child1 = Lower;
            Leaf->Child2 = Higher;
        }
        Lower->Parent = Leaf;
        Higher->Parent = Leaf;
        if ( Leaf->Data1 > item )
        {
            Leaf->Data2 = Leaf->Data1;
            Leaf->Data1 = item;
        }
        else
            Leaf->Data2 = item;
    }
}
}

void TwoThreeTree::Add(int item)
{
if ( !Root ) {
    Root = new TwoThreeNode;    
    Root->Data1 = item;
}   
else if ( !Root->TwoData && ( !Root->Child1 && !Root->Child2 && !Root->Child3)) 
{
    if ( Root->Data1 > item )
    {
        Root->Data2 = Root->Data1;
        Root->Data1 = item;
    }
    else
        Root->Data2 = item;
}
else 
{
    LOCATION loc;
    TwoThreeNode* tNode;
    TwoThreeNode* Leaf = tNode = Root;
    while ( tNode )
    {
        Leaf = tNode;
        if ( tNode->TwoData )
        {
            if ( Highest( tNode->Data1, tNode->Data2, item ) == item ) {
                tNode = tNode->Child3;
                loc = HIGH;
            }
            else if ( Lowest( tNode->Data1, tNode->Data2, item ) == item ) {
                tNode = tNode->Child1;
                loc = LOW;
            }
            else {
                tNode = tNode->Child2;
                loc = MIDDLE;
            }
        }
        else
        {
            if ( item > tNode->Data1 ) {
                tNode = tNode->Child2;
                loc = HIGH;
            }
            else {
                tNode = tNode->Child1;
                loc = LOW;
            }
        }
    }
    if ( Leaf->TwoData ) {
        Split( Leaf, item, loc, 0, 0 );
    }
    else {
        if ( Leaf->Data1 > item ) {
            Leaf->Data2 = Leaf->Data1;
            Leaf->Data1 = item;
        }
        else
            Leaf->Data2 = item;
    }
}
}

bool TwoThreeTree::Search(int item)
{
TwoThreeNode* tNode = Root; 

while ( tNode )             
{
    if ( tNode->TwoData )
    {
        if ( item == tNode->Data1 || item == tNode->Data2 ) return true;    

        if ( Highest( tNode->Data1, tNode->Data2, item ) == item ) tNode = tNode->Child3;
        else if ( Lowest( tNode->Data1, tNode->Data2, item ) == item ) tNode = tNode->Child1;
        else tNode = tNode->Child2; 
    }
    else
    {
        if ( item == tNode->Data1 ) return true;

        if ( item > tNode->Data1 ) tNode = tNode->Child2;
        else tNode = tNode->Child1;     
    }
}
return false; 
}

int TwoThreeTree::Highest( int a, int b, int c )
{
int highest = ( a > b ) ? a : b;
return ( highest > c ) ? highest : c;
}

int TwoThreeTree::Lowest( int a, int b, int c )
{
int lowest = ( a < b ) ? a : b;
return ( lowest < c ) ? lowest : c;
}

int TwoThreeTree::Middle( int a, int b, int c )
{
int high = Highest(a, b, c);
int low = Lowest(a, b, c);

if ( a != high && a != low ) return a;
return ( b != high && b != low ) ? b : c;
}

void TwoThreeTree::KillNodes( TwoThreeNode* Node )
{
if ( Node )
{
    KillNodes( Node->Child1 );
    KillNodes( Node->Child2 );
    KillNodes( Node->Child3 );
    delete Node;
}
}


TwoThreeTree::TwoThreeNode::TwoThreeNode() : 
Child1(0), Child2(0), Child3(0),
Parent(0), TwoData(false)
{
}

TwoThreeTree::TwoThreeNode::~TwoThreeNode()
{
}

0 个答案:

没有答案