Binarne drzewo poszukiwań C ++ (Rodzice)

głosy
0

Muszę tylko trochę więcej pomocy na moim BST. To właśnie mój BST wygląda podczas wstawiania:

P, L, J, G

                      R   --Root at Index 0
                     / \
     L @ Index1     L   NULL
                   / \
     J @ Index3   J   NULL
                 / \
     G @ Index7 G  NULL

Oto kod, który sprawia, że ​​tak się stało.

void BST::insert(const data &aData)
{   
    if ( items[Parent].empty ) 
    {
        items[Parent].theData = aData; // insert at leaf.
        items[Parent].empty = false;
        size++;

        return;
    }           
    for ( int i = 0; i <= size; i++ )
    {
        if ( aData < items[Parent].theData )
        {
            if ( items[2*i+1].empty )
            {
            items[2*i+1].theData = aData;
            items[2*i+1].empty = false;
            }
            else 
                           {
            // we must already have a left child to some root.
                                Parent++;  So make the previous data the root???
            if ( items[Parent].empty )
            {
                items[Parent].theData = items[2*i+1].theData;
                items[Parent].empty   = false;
                Parent = (i-1)/2;
            }
                           } 
        }
        else
        { ...// do the same for data greater than but with items[2*i+2] }

Moje pytanie jest to, że kiedy będę musiał zrobić nowy pierwiastek? Kiedy muszę zrobić nowy pierwiastek? Dla recomparison?

Jest to podejście prawidłowe? Dziękuję tym, którzy jeszcze zarówno spojrzeć na moje posty :)

// Konstruktor Klasa BST i jego prywatny odcinek.

BST::BST(int capacity) : items(new item[capacity]), size(0), Parent(0), 
leftChild(0), rightChild(0)
{
    items->empty = true;
    maxSize = capacity;
}
private:
    int size;  // size of the ever growing/expanding tree :)
    int Parent;
    int maxSize;    
    int leftChild;
    int rightChild;
    struct item
    {
        bool empty;
        data theData;
    };
    item *items;    // The tree array
Utwórz 17/11/2009 o 21:59
źródło użytkownik
W innych językach...                            


2 odpowiedzi

głosy
1

Twoja logika (raczej rozmyte muszę powiedzieć) wydaje się być źle: Jaki rodzaj „czy” sekwencja jest?

if ( items[2*i+1].empty )
{
}
else if (!items[2*i+1].empty)
{
   if ( items[2*i+1].empty )
   {
        // any code here is unreachable
   }
}
Odpowiedział 17/11/2009 o 22:12
źródło użytkownik

głosy
1

Proponuję reimplement to działać rekurencyjnie. Coś takiego:

void BST::insert(const data& aData, int pos) {
    if (items[pos].empty) {
        // insert it here
    }
    else (aData < items[pos].theData) {
        // go to left child
        insert(aData, 2*pos + 1);
    }
    else {
        // go to right child
        insert(aData, 2*pos + 2);
    }
}

To naprawdę nie jest jasne, co nadrzędna, leftChild i rightChild robią w swojej klasie, ale to osobna kwestia.

Odpowiedział 17/11/2009 o 22:46
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more