c++ - 通过引用传递指针(插入二叉搜索树)

标签 c++

我的程序正在调用一个成员函数,该函数使用通过引用传递的指针执行向二叉搜索树的插入(以便更改指针的值)。但是,当我尝试将第二个节点插入树中时,程序崩溃了。我假设这与错误的指针有关。

此分配中唯一可以更改的部分是成员函数的实现,而不是声明。

如有任何帮助,我们将不胜感激。

 //Header file
using namespace std;

struct PersonRec
{
    char name[20];
    int bribe;
    PersonRec* leftLink;
    PersonRec* rightLink;
};


class CTree
{

private:
    PersonRec *tree;
    bool IsEmpty();
    void AddItem( PersonRec*&, PersonRec*);
    void DisplayTree(PersonRec*);

public:
    CTree();
    //~CTree();
    void Add();
    void View();

};

//Implementation file

#include <iostream>
#include <string>

using namespace std;

#include "ctree.h"

CTree::CTree()
{
    tree = NULL;
}

//PersonList::~MyTree()
//{
//
//}


bool CTree::IsEmpty()
{
    if(tree == NULL) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

void CTree::Add()
{
    PersonRec* newPerson = new PersonRec();
    cout << "Enter the person's name: ";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cin.getline(newPerson->name, 20);
    cout << "Enter the person's contribution: ";
    cin >> newPerson->bribe;


    newPerson->leftLink = NULL;
    newPerson->rightLink = NULL;

    AddItem(tree, newPerson);
}

void CTree::View()
{
    if (IsEmpty())
    {
        cout<<"The list is empy";
    }
    else
    {
        DisplayTree(tree);

    }

};

void CTree::AddItem( PersonRec*& ptr, PersonRec* newPer )
{
        if (tree == NULL)
        {
            ptr = newPer;
        }
        else if ( newPer->bribe < ptr->bribe)
            AddItem(ptr->leftLink, newPer); 
        else
            AddItem(ptr->rightLink, newPer); 
}
void CTree::DisplayTree(PersonRec* ptr)
{
    if (ptr == NULL)
                    return;
    DisplayTree(ptr->rightLink);
    cout<<ptr->name<<" "<<"$"<<ptr->bribe <<endl;
    DisplayTree(ptr->leftLink); 
}


//Driver file
#include <iostream>

using namespace std;
#include <cstdlib>
#include "ctree.h"

int displayMenu (void);
void processChoice(int, CTree&);

int main (void)
{
int num;
CTree ct;
do 
{
num = displayMenu();
if (num != 3)
processChoice(num, ct);
} while (num != 3);
return 0;
}

int displayMenu (void)
{
int choice;
cout << "\nMenu\n";
cout << "==============================\n\n";
cout << "1. Add student to waiting list\n";
cout << "2. View waiting list\n";
cout << "3. Exit program\n\n";
cout << "Please enter choice: ";
cin >> choice;
return choice;
}

void processChoice(int choice, CTree& myTree)
{
   switch (choice)
   {
      case 1: myTree.Add (); break;
      case 2: myTree.View (); break;
   } 
}

最佳答案

CTree::AddItem中,您的条件是错误的:

    if (tree == NULL)
    {
        ptr = newPer;
    }

应该是

    if (ptr == NULL)
    {
        ptr = newPer;
    }

当您调用 AddItem(ptr->rightLink, newPer); 时,newPer 是新节点,但当前根的任一后代都可以是 NULL,因此您需要检查 NULL 并替换它,而不是新节点。

关于c++ - 通过引用传递指针(插入二叉搜索树),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10659191/

相关文章:

c++ - 函数没有返回正确的结果

c++ - 错误 LNK2038 : mismatch detected for '_MSC_VER' : value '1600' doesn't match value '1700' in CppFile1. 对象

c++ - 使用 libxml2 从 C++ 中的 XML <tag> 获取值

c++ - 打印一个 unicode 拉丁字母 (utf8)

c++ - 了解返回时的右值引用

c++ - 为什么 lambda 对象中的局部变量是 const?

c++ - ACE 互斥体如何工作以及为什么 ACE 线程互斥体的工作方式不同?

c++ - 在另一个模板基类中实现抽象基类函数

c++ - endl 和 cout 后的行间距?

c++ - 从函数返回一个包含另一个对象的对象