二元搜索树我的删除实现不起作用,无法找到错误

时间:2016-09-29 11:08:10

标签: c++ binary-search-tree

我只是通过使用这个特定的过程来实现具有递归结构的bs_delete的练习(不允许间接递归)。这是我到目前为止所提出的:

标题

    #pragma once
    #ifndef BS_H_
    #define BS_H_

    class element_bs {
    public:
        element_bs *p = nullptr, *l = nullptr, *r = nullptr;
        int key;

        element_bs() {}
        element_bs(int k) { key = k; }

    };

    class BS {
    public:
        element_bs *wurzel = nullptr;

        void bsAdd(element_bs *);
        void Inorder(element_bs *);
        element_bs *bsDelete_recursive(element_bs * ,int );
        void printSuche(int ); //Search
    };

    #endif

实施

#include "stdafx.h"
#include "BS.h"
#include <iostream>

void BS::Inorder(element_bs *ptr) {
    if (ptr != nullptr) {
        Inorder(ptr->l);
        std::cout << ptr->key << std::endl;
        Inorder(ptr->r);
    }
}

element_bs * BS::bsDelete_recursive(element_bs *x, int log)
{
    element_bs *k = nullptr;


    if (!log) {
        if (x->r != nullptr) 
            k = bsDelete_recursive(x->r, 1);

        else if (x->l != nullptr) 
            k = bsDelete_recursive(x->l, 2);

        else k = nullptr;

        if (k != nullptr) {
            k->p = x->p;

            if (k != x->r)
                k->r = x->r;

            if (k != x->l)
                k->l = x->l;
        }


        if (x->p != nullptr) {
            if (x == x->p->l)
                x->p->l = k;
            else x->p->r = k;
        }

    }
    else if (log == 1) {
        if (x->l != nullptr)
            k = bsDelete_recursive(x->l, 1);
        else {
            k = x;

            if(k != x->p->r)
                x->p->l = k->r;
        }
    }
    else {
        if (x->r != nullptr)
            k = bsDelete_recursive(x->r, 2);
        else {
            k = x;

            if(k != x->p->l)
                x->p->r = k->l;
        }
    }

    return k;
}

void BS::bsAdd(element_bs *ptr) {
    int i;

    if (wurzel == nullptr) {
        wurzel = ptr;
    }
    else {
        element_bs *w = wurzel;
        element_bs *n = w;
        bool active = true;
        bool check = false;


        while (active) {
            n = w;

            if (w->key >= ptr->key) {
                w = w->l;
                check = true;
            }
            else {
                w = w->r;
                check = false;
            }

            if (w == nullptr) {
                active = false;

                if (check)
                    n->l = ptr;
                else n->r = ptr;

                ptr->p = n;
            }
        }
    }
}

void BS::printSuche(int k) {
    element_bs *temp = wurzel;

    while (temp != nullptr) {
        std::cout << temp->key << std::endl;

        if (k == temp->key) {
            temp = nullptr;
        }
        else if (k <= temp->key)
            temp = temp->l;
        else
            temp = temp->r;
    }
 }

(试验)主

int main() {
    BS tree;
    element_bs liste[11]{ (8), (5), (7), (2), (4), (10), (11), (9), (3),     (3), (1) };

    for (int i = 0; i < 11; ++i)
        tree.bsAdd(&liste[i]);

    tree.Inorder(tree.wurzel);
    std::cout << " " << std::endl;
    tree.bsDelete_recursive(&liste[3], 0);

    tree.Inorder(tree.wurzel);

    //
    std::cout << " " << std::endl;
    std::cout << " " << std::endl;
    tree.bsDelete_recursive(&liste[4], 0);

    tree.Inorder(tree.wurzel);
    //
    std::cout << " " << std::endl;
    std::cout << " " << std::endl;
    tree.bsDelete_recursive(&liste[2], 0);

    tree.Inorder(tree.wurzel);
    //
    std::cout << " " << std::endl;
    std::cout << " " << std::endl;
    tree.bsDelete_recursive(&liste[5], 0);

    tree.Inorder(tree.wurzel);

    return 0;
}

第二次bs_delete调用无法正常运行。

非常感谢你!

0 个答案:

没有答案