繁体   English   中英

二进制搜索树递归插入

[英]Binary Search Tree recursion insertion

我目前在使用递归将节点插入二叉树时遇到困难。 我已经在这个问题上呆了几天了,以为是时候我来寻找答案了!

节点类(.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类实现二叉树数据结构(.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

我用来将节点插入树中的插入成员函数实现:

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

我不明白为什么此插入无法正常工作。 代码看起来完美无缺,这让我抓狂了。 我已经编写了一个使用迭代的备用插入函数,但是必须进行递归。

任何帮助都将非常棒,谢谢。

问题在这里:

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

node_ptr是局部变量,因为您按值传递了它。 因此,当您退出该功能时,分配将丢失。

要解决它-通过引用传递:

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

当然,这需要进行以下更改:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM