Binary Search vložení Tree rekurze

hlasů
0

Jsem v současné době potíže s vložením uzlu do binárního stromu s použitím rekurze. Byl jsem obydlí na tomto problému na několik dní a hned myslel, že to bylo, když jsem přišel hledat odpovědi!

třída uzlu (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 třída implementovat strukturu binárního stromu dat (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

Zavádění člen funkce insert Já používám vložit uzlů do stromu:

    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);
    }
}

Nechápu, proč se to vkládání nefunguje. Kód vypadá bezchybně a to nechal mě poškrábání hlavu. Napsal jsem alternativní funkci vložení, který využívá iteraci, ale rekurze je nutností.

Pomoci by bylo fantastické, děkuji.

Položena 26/10/2011 v 09:50
zdroj uživatelem
V jiných jazycích...                            


1 odpovědí

hlasů
2

Problémem je zde:

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

node_ptrje lokální proměnná, protože si ji ujít hodnotě. To znamená, že přiřazení je ztraceno při opuštění funkce.

Chcete-li to napravit - projít odkazem:

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

Že bude požadovat, aby tyto změny, samozřejmě:

        student* & left(void);
        student* & right(void);
Odpovězeno 26/10/2011 v 09:56
zdroj uživatelem

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