Binar de căutare de inserare copac recursivitate

voturi
0

Am în prezent dificultăți în inserarea unui nod într-un arbore binar folosind recursivitate. Am locuit pe această problemă pentru câteva zile acum și a crezut că a fost dată când am venit în căutarea pentru răspunsuri!

Clasa de Nod (.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 clasa pentru a pune în aplicare structura de date binare arbore (.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

Punerea în aplicare Funcția membru de inserție I sunt utilizați pentru a insera noduri în copac:

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

Nu înțeleg de ce această inserție nu funcționează. Codul arata impecabil si mi-a lăsat zgarieturi capul meu. Am scris o funcție de inserție alternativă, care utilizează iterație, dar recursivitatea este o necesitate.

Orice ajutor ar fi fantastic, vă mulțumesc.

Întrebat 26/10/2011 la 09:50
sursa de către utilizator
În alte limbi...                            


1 răspunsuri

voturi
2

Problema este aici:

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

node_ptreste o variabilă locală, pentru că tu treci prin valoare. Astfel, alocarea se pierde atunci când ieșiți din funcția.

Pentru a remedia problema - treci prin referință:

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

Acest lucru va necesita aceste schimbări, desigur:

        student* & left(void);
        student* & right(void);
Publicat 26/10/2011 la 09:56
sursa de către utilizator

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