【问题标题】:Binary Search Tree recursion insertion二叉搜索树递归插入
【发布时间】:2011-12-15 13:51:46
【问题描述】:

我目前在使用递归将节点插入二叉树时遇到了困难。这几天我一直在纠结这个问题,觉得是时候来找答案了!

节点类(.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

实现二叉树数据结构(.h)的BSTree类:

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

我不明白为什么这个插入不起作用。代码看起来完美无瑕,让我摸不着头脑。我写了一个使用迭代的替代插入函数,但递归是必须的。

任何帮助都会很棒,谢谢。

【问题讨论】:

  • 从表面上看,您是在尝试创建按年级排序的学生二叉树吗?在那种情况下,为什么将 node_ptr 传递给 insert 方法?您应该考虑将您的递归插入方法设为私有,并添加一个仅获取新学生的成绩和姓名的公共插入方法。

标签: c++ recursion binary-search-tree


【解决方案1】:

问题出在这里:

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

【讨论】:

  • 我陷入了将指针/引用变量传递给函数的 C 思维状态。非常感谢,您为我省去了几个小时的麻烦!
猜你喜欢
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
相关资源
最近更新 更多