簡體   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