从二叉树,c ++打印和删除数据

时间:2017-03-17 21:24:58

标签: c++11 binary-tree

晚上好, 我正在努力打印我的二叉树

#ifndef TREE_H_INCLUDED
#define TREE_H_INCLUDED

#include "qt.h"
#include <iostream>
using namespace std;

template<typename T>
class Tree{
    Node<T> *root;
    void insertIntoTree(T &d, Node<T> *&r);
    void printTree(Node<T> *r);
    void deleteTree(Node<T> *&r);
    Node<T>* findInTree(T &d, Node<T> *r, Node<T> *&parent);
    void deleteLeaf(Node<T> *p, Node<T> *q);
    void deleteInBranch(Node<T> *p, Node<T> *g);
public:
    Tree() : root(nullptr){}
    ~Tree(){
        clean();
    }
    bool find(T d){
        Node<T> *dummy=nullptr;
        return findInTree(d, root, dummy);
    };
    void clean(){
        deleteTree(root);}
    void insert(T d){
         insertIntoTree(d, root);}
    void print(){
        printTree(root);
    }
    bool deleteNode(T d);
};

template<typename T>
void Tree<T>::insertIntoTree(T &d, Node<T> *&r){
    if(!r){
        r = new Node<T>(d);
        return;
    }
    if(r->getData()<d){
        insertIntoTree(d, r->right);
    }
    else{
        insertIntoTree(d, r->left);
    }
}

template<typename T>
void Tree<T>::printTree(Node<T> *r){
    if(r){
        printTree(r->left);
        r->print();
        printTree(r->right);
    }
}

template<typename T>
void Tree<T>::deleteTree(Node<T> *&r){
    if(r){
        deleteTree(r->left);
        deleteTree(r->right);
        delete r;
        r = nullptr;
    }
}

template<typename T>
Node<T>* Tree<T>::findInTree(T &d, Node<T> *r, Node<T> *&parent){
    if(!r){
        return nullptr;
    }
    if(r->getData()==d){
        return r;
    }
    parent = r;
    if(r->getData()<d){
        return findInTree(d, r->right, parent);
     }
    else{
        return findInTree(d, r->right, parent);
    }
}

template<typename T>
void Tree<T>::deleteLeaf(Node<T> *p, Node<T> *q){
    if(p == root){
        root = nullptr;
    }
    else{
        if(p->right == q)
            q->right = nullptr;
        else
            q->left = nullptr;
    }
    delete p;
}

template<typename T>
void Tree<T>::deleteInBranch(Node<T> *p, Node<T> *q){
    Node<T> *s = (p->left ? p->left : p->right);
    if(p == root){
        root = s;
    }else{
        if(q->left == p){
            q->left = s;
        }
        else{
            q->right = s;
        }
    }
    delete p;
}

template<typename T>
bool Tree<T>::deleteNode(T d){
   Node<T> *p, *q = nullptr;
   p = findInTree(d, root, q);
   if(!p){
       return false;
   }
   if(!p->left && !p->right){
        deleteLeaf(p, q);
   } else if(!p->left || !p->right){
       deleteInBranch(p, q);
   } else{
       Node<T> *r = p->left; 
       q = p;
       while(r->right){
           q = r;
           r = r->right;
       }
       p->setData(r->getData());
       if(r->left){
           deleteInBranch(r, q);
       } else{
           deleteLeaf(r, q);
       }
   }
   return true;
}

#endif // TREE_H_INCLUDED

qt.h

#ifndef QT_H_INCLUDED
#define QT_H_INCLUDED

#include<iostream>

template<typename T>
class Node{
    T data;
    Node<T> *left;
    Node<T> *right;
public:
    Node(T d) : data(d), left(nullptr), right(nullptr){}
    void print(){
        std::cout << data << std::endl;}
    T getData()const {
        return data;
        }
    void setData(const T &value){
        data = value;
        }
    template<typename X> friend class Tree;
};

#endif // QT_H_INCLUDED

,不幸的是我也要发布main.cpp

#include <iostream>
#include "qt.h"
#include "tree.h"

using namespace std;

int main()
{
    Tree<int> t;
    t.insert(42);
    t.insert(26);
    t.insert(10);
    t.insert(5);
    t.insert(53);
    t.insert(9);
    t.insert(38);
    if(t.find(42)){
        cout << "nalezeno" << endl;
    }
    else
        cout << "nenalezeno" << endl;
    if(t.deleteNode(42)){
        cout << "smazano" << endl;
    }
    t.print();
     return 0;
}

问题是,当我运行它时,它可以工作,当我只添加6个数字时,当我添加第七个(38)时,程序会下降,没有错误,没有警告。我真的找不到问题,我想,它可能在删除(42),而不是打印,但我找不到原因。 谢谢你的帮助

0 个答案:

没有答案