c++ - 从二叉树错误中删除

标签 c++ binary-tree

我写了一个小代码来检测二叉树中的最大数字,它工作得很好,很简单,它一直向下到最后一个正确的节点(万一是叶子),然后我就计算出<<它,现在我想删除它,我看了一些类似的问题,但我只需要删除我从搜索中得到的号码,但我的程序在我运行它后崩溃列出树获取号码,删除并尝试再列出来。

这是我的搜索:

    T Remove( Node* theRoot)
        {
        if ( root == NULL )
        {
             cout<<"There is no tree";
             return -1;
        }

        if (theRoot->rChildptr != NULL)
            return Largest(theRoot->rChildptr);
        else
             delete theRoot;
             return theRoot->data;

    } 

完整代码如下:

#include <iostream>
#include <string>
#include <cstdlib> 


using namespace std;


template<class T>
class BinaryTree
{

struct Node
    {
        T data;
        Node* lChildptr;
        Node* rChildptr;

        Node(T dataNew)
        {
            data = dataNew;
            lChildptr = NULL;
            rChildptr = NULL;

        }
    };
private:
    Node* root; 

        void Insert(T newData, Node* &theRoot)
        {
            if(theRoot == NULL) 
            {
                theRoot = new Node(newData);
                return;
            }

            if(newData < theRoot->data)  
                Insert(newData, theRoot->lChildptr);
            else
                Insert(newData, theRoot->rChildptr);
        }

        void PrintTree(Node* theRoot)
        {
            if(theRoot != NULL)
            {
                PrintTree(theRoot->lChildptr);
                cout<< theRoot->data<<" \n";
                PrintTree(theRoot->rChildptr);
            }
        }
        T Largest( Node* theRoot)
            {
            if ( root == NULL )
            {
                 cout<<"There is no tree";
                 return -1;
            }

            if (theRoot->rChildptr != NULL)
                return Largest(theRoot->rChildptr);
            else
                 delete theRoot;
                 return theRoot->data;

        }
        T Remove(Node* theRoot)
        {
            if ( root == NULL )
            {
                 cout<<"There is no tree";
                 return -1;
            }

            if (theRoot->rChildptr != NULL)
                return Largest(theRoot->rChildptr);
            else
                delete theRoot;
                 return ;
        };

    public:
        BinaryTree()
        {
            root = NULL;
        }

        void AddItem(T newData)
        {
            Insert(newData, root);
        }

        void PrintTree()
        {
            PrintTree(root);
        }
        T Largest()
        {
            return Largest(root);
        }
        //void Remove()
        //{
        //  Remove(root);
        //}

    };

    int main()
    {
        BinaryTree<int> *myBT = new BinaryTree<int>();
        myBT->AddItem(2);
        myBT->AddItem(20);
        myBT->AddItem(5);
        myBT->AddItem(1);
        myBT->AddItem(10);
        myBT->AddItem(15);
            //for(int i = 0; i < 10; i++)           //randommal tolti fel
                //myBT->AddItem(rand() % 100);
        cout << "BinaryTree:" << endl;              //kilistazaa a fat
        myBT->PrintTree();
        cout << "Largest element: " << myBT->Largest() << endl;  //visszaadja a legnagyobb elemet
        //myBT->Remove();
        myBT->PrintTree();
  }

实际的删除功能在//注释中,因此我可以运行程序。

最佳答案

您正在删除 theRoot 然后尝试取消引用它。如果你想返回存储在节点中的值,你需要先像这样制作一个本地拷贝:

T value = theRoot->data;
delete theRoot;
return value;

return thetRoot->data; 行是否打算成为 else 语句的一部分?如果是这样,您需要像这样在其周围添加方括号:

if (theRoot->rChildptr != NULL)
{
    return Largest(theRoot->rChildptr);
}
else
{
    T value = theRoot->data;
    delete theRoot;
    return value;
}

或者简单地完全删除 else 情况(因为如果子指针为空,您总是返回):

if (theRoot->rChildptr != NULL)
{
    return Largest(theRoot->rChildptr);
}

T value = theRoot->data;
delete theRoot;
return value;

您还需要确保父节点不会仍然指向已删除的子节点(很难确切地看到发生了什么,因为您没有发布太多代码)。

关于c++ - 从二叉树错误中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193218/

相关文章:

c++ - 如何防止从字符串派生的 2 个类之间的交叉分配

c++ - 二叉树 BFS 的队列

algorithm - 给定多棵二叉树,更本地化、更高效的最低公共(public)祖先算法?

c - 二叉搜索树 C

c++ - 没有 GL_COLOR_INDEX 的 glBitmap()

c++ - 为什么要添加地址?

c - 二叉树,IsMaxTree

将二叉树转换为对应的无向图

c++ - 使 typedef 不兼容

c++ - 使用 copy-and-swap 惯用语,复制对象的析构函数如何不释放指向内存?