Ricerca binaria inserimento albero di ricorsione

voti
0

Attualmente sto avendo difficoltà di inserire un nodo in un albero binario utilizzando la ricorsione. Ho soffermarsi su questo problema per un paio di giorni ormai e ho pensato che fosse volta che sono venuto in cerca di risposte!

classe Node (h):

#ifndef STUDENT_MACROGUARD
#define STUDENT_MACROGUARD

#include <cstdlib>
#include <string>

namespace student_class
{
    class student
    {
        public:
            // Constructors / Destructors;

            student(const float entry = 0, std::string name = , 
                        student* left_ptr = NULL, student* right_ptr = NULL);
            ~student(void){};

            // Mutating member functions;

            void set_grade(const float entry);
            void set_name(std::string entry);

            void set_left(student* node_ptr);
            void set_right(student* node_ptr);

            // Non mutating member functions;

            float grade(void);
            std::string name(void);

            student* left(void);
            student* right(void);

            // Const member functions;

            const student* left(void) const;
            const student* right(void) const;

    private:
            std::string student_name;
            float grade_field;
            student* left_ptr;
            student* right_ptr;
    };
}

#endif

BSTree classe per implementare la struttura di dati albero binario (h):

#ifndef BTTree_MACROGUARD
#define BTTree_MACROGUARD

#include <cstdlib>
#include <string>
#include <iostream>
#include student.h

using namespace student_class;

namespace BTTree_class
{
class BTTree
{
    public:
            // Constructors / Destructors; 

            BTTree(student* node_ptr = NULL);
            ~BTTree(void);

            // Mutating member functions;

            void insert(student* node_ptr = NULL, const float grade = 0, std::string name = );
            void remove(student* node_ptr);

            // Non mutating member functions;

            student* grade_search(const float entry);
            student* name_search(const std::string entry);
            student* root(void);
            void print_tree(student* node_ptr);

            // Const member functions;

            const student* grade_search(const float entry) const;
            const student* name_search(const float entry) const;
            const student* root(void) const;

    private:
            student* root_ptr;
    };
}

#endif

L'implementazione funzione membro inserto che sto usando per inserire nodi contro l'albero:

    void BTTree::insert(student* node_ptr, const float grade, std::string name)
{
    if(root_ptr == NULL)
    {
        root_ptr = new student(grade, name);
        return;
    }

    if(node_ptr == NULL)
    {
        node_ptr = new student(grade, name);
        return;
    }
    else if(node_ptr->grade() > grade)
    {
        insert(node_ptr->left(), grade, name);
    }
    else
    {
        insert(node_ptr->right(), grade, name);
    }
}

Non capisco il motivo per cui questo inserimento non sta funzionando. Il codice è impeccabile e mi ha lasciato graffiare la mia testa. Ho scritto una funzione di inserimento alternativo che usa iterazione, ma ricorsione è un must.

Qualsiasi aiuto sarebbe fantastico, grazie.

È pubblicato 26/10/2011 alle 09:50
fonte dall'utente
In altre lingue...                            


1 risposte

voti
2

Il problema è qui:

if(node_ptr == NULL)
{
    node_ptr = new student(grade, name);
    return;
}

node_ptrè una variabile locale, perché si passa per valore. Così, l'assegnazione si perde quando si esce dalla funzione.

Per risolvere il problema - pass per riferimento:

void BTTree::insert(student* &node_ptr, const float grade, std::string name)

Ciò richiederà questi cambiamenti, naturalmente:

        student* & left(void);
        student* & right(void);
Risposto il 26/10/2011 a 09:56
fonte dall'utente

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