hzh834215917 2020-06-18 17:46 采纳率: 0%
浏览 123

【C++】二叉树删除根节点出错!!求帮助解惑!!

此代码可以正常删除叶子节点,但删除根节点时就出错。

出错截图如下

图片说明

代码如下

#pragma once
#ifndef BT2
#define BT2
#include <iostream>
#include <queue>

template<class T>
class BT;

template<class T>
class Element
{
public:
    Element()
    {
        key = 0;
    }
    T key;
};

template<class T>
class BTNode
{
    friend class BT<T>;
private:
    BTNode()
    {
        data.key = 0;
        left = right = NULL;
    }
    Element<T> data;
    BTNode<T> *left;
    BTNode<T> *right;
    void display(int i)
    {
        std::cout << "Position " << i << ": data.key= " << data.key << "\n";
        if (left) left->display(2 * i);
        if (right) right->display(2 * i + 1);
    }
};
//二叉树类
template<class T>
class BT
{
public:
    BT(BTNode<T> *init = 0) :root(init) {};
    ~BT()
    {
        delete root;
    }
    void Insert(const Element<T> &x);
    void Search(const Element<T> &x);
    void InOrder();
    void PreOrder();
    void PostOrder();
    void LevelOrder();
    void Delete(const Element<T> &x);
    void visit(BTNode<T> *);
    void display()
    {
        std::cout << "\n";
        if (root)
        {
            std::cout << "根为:" << root->data.key << "\n";
            root->display(1);
        }
        else
        {
            std::cout << "This is a empty tree!" << "\n";
        }
    }
    BTNode<T> *miniMum(BTNode<T> *);
    BTNode<T> *RemoveMin(BTNode<T> *);
private:
    BTNode<T> *root;
    void InOrder(BTNode<T> *a);
    void PreOrder(BTNode<T> *a);
    void PostOrder(BTNode<T> *a);
    BTNode<T> *Delete(BTNode<T> *, const Element<T> &);
};

//二叉树结点类
template<class T>
BTNode<T> *BT<T>::miniMum(BTNode<T> *a)
{
    if (a->left == NULL)
    {
        return a;
    }
    a->left = miniMum(a->left);
    return a;
}

template<class T>
BTNode<T> *BT<T>::RemoveMin(BTNode<T> *a)
{
    if (a->left == NULL)
    {
        BTNode<T> *leftnode1 = a->right;
        delete a;
        return leftnode1;
    }
    a->left = RemoveMin(a->left);
    return a;
}


template<class T>
void BT<T>::visit(BTNode<T> *a)
{
    std::cout << a->data.key << " ";
}

//插入节点
template<class T>
void BT<T>::Insert(const Element<T> &x)
{
    BTNode<T> *p = root;
    BTNode<T> *q = 0;
    while (p)
    {
        q = p;
        if (x.key == p->data.key) return;
        else if (x.key < p->data.key)
        {
            p = p->left;
        }
        else
        {
            p = p->right;
        }

    }
    p = new BTNode<T>;
    p->data.key = x.key;
    if (!root) root = p;
    else if (x.key < q->data.key) q->left = p;
    else q->right = p;


}

//查找节点
template<class T>
void BT<T>::Search(const Element<T> &x)
{
    BTNode<T> *current = root;
    bool a = false;
    while (current)
    {
        if (x.key == current->data.key)
        {
            a = true;
            std::cout << "已找到!" << "\n";
            break;
        }
        else if (x.key < current->data.key)
        {
            current = current->left;
        }
        else
        {
            current = current->right;
        }
    }
    if (!a)
    {
        if (current == root)
        {
            std::cout << "树为空!" << "\n";
        }
        else
        {
            std::cout << "未找到!" << "\n";
        }

    }
}

//中序遍历
template<class T>
void BT<T>::InOrder()
{
    InOrder(root);
}
template<class T>
void BT<T>::InOrder(BTNode<T> *a)
{
    if (a->left) InOrder(a->left);
    visit(a);
    if(a->right) InOrder(a->right);

}

//前序遍历
template<class T>
void BT<T>::PreOrder()
{
    PreOrder(root);
}
template<class T>
void BT<T>::PreOrder(BTNode<T> *a)
{
    visit(a);
    if (a->left) PreOrder(a->left);
    if (a->right) PreOrder(a->right);
}

//后序遍历
template<class T>
void BT<T>::PostOrder()
{
    PostOrder(root);
}
template<class T>
void BT<T>::PostOrder(BTNode<T> *a)
{
    visit(a);
    if (a->left) PostOrder(a->left);
    if (a->right) PostOrder(a->right);
}

//层序遍历
template<class T>
void BT<T>::LevelOrder()
{
    std::queue<BTNode<T> *> b;
    BTNode<T> *a = root;
    while (a)
    {
        visit(a);
        if (a->left) b.push(a->left);
        if (a->right) b.push(a->right);
        if (b.empty()) return;
        a = b.front();
        b.pop();
    }
}

//删除节点
template<class T>
void BT<T>::Delete(const Element<T> &x)
{
    if (root)
    {
        root = Delete(root, x);
    }
}
template<class T>
BTNode<T> *BT<T>::Delete(BTNode<T> *a, const Element<T> &x)
{
    if (a == NULL) return NULL;
    if (x.key < a->data.key)
    {
        a->left = Delete(a->left, x);
        return a;
    }
    else if (x.key > a->data.key)
    {
        a->right = Delete(a->right, x);
        return a;
    }
    else
    {
        if (a->left == NULL)
        {
            BTNode<T> *rightnode = a->right;
            delete a;
            return rightnode;
        }
        if (a->right == NULL)
        {
            BTNode<T> *leftnode = a->left;
            delete a;
            return leftnode;
        }
    }
    BTNode<T> *dela = a;
    BTNode<T> *successor = miniMum(a->right);
    successor->left = a->left;
    successor->right = RemoveMin(a->right);
    delete dela;
    return successor;

}
#endif // !BT2

#include "树2.h"

int main()
{
    Element<int> a, b, c, d, e, f;
    a.key = 5;
    b.key = 8;
    c.key = 12;
    d.key = 1;
    e.key = 3;
    f.key = 24;
    BT<int> m;
    m.Insert(a);
    m.Insert(b);
    m.Insert(c);
    m.Insert(d);
    m.Insert(e);
    m.Delete(a);**//删除b节点时正常运行,删除根节点(a)时出错!**
    m.display();


    return 0;
}
  • 写回答

1条回答 默认 最新

  • LionGod 2020-06-18 19:12
    关注

    二叉树的根节点不能删,只能更新值,我博客里有类似于完全二叉树的排序算法

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮