C++我创建了一个类描述二叉树,但是析构时内存报错,能帮我看看怎么回事吗?
#ifndef BTREE_H_
#define BTREE_H_
#include<iostream>
using namespace std;
struct bintree
{
char ch;
bintree * ltree;
bintree * rtree;
};
class bt
{
private:
bintree *root;
public:
bt(char c = 'A');
bintree *rroot();
void insert(bintree * b);
void presearch(bintree * b);
void insearch(bintree * b);
void nexsearch(bintree * b);
void return_ad(bintree *b);
~bt();
};
#endif
#include"btree.h"
bt::bt(char c)
{
root = new bintree;
root->ch = c;
}
bintree * bt::rroot(){return root;}
void bt::insert(bintree * b)
{
cout<< "The node "<< b->ch<<" has a left node ? "<<endl;
char choice;
cin>>choice;
if(choice == 'Y')
{
bintree * i = new bintree;
cout<<"Please enter the content of left node : "<< endl;
cin>>choice;
b->ltree = i;
i->ch =choice;
insert(i);
}
else
b->ltree = NULL;
cout<< "The node "<< b->ch<<" has a right node ?"<<endl;
cin>>choice;
if(choice == 'Y')
{
bintree * j= new bintree;
cout<<"Please enter the content of right node : "<< endl;
cin>>choice;
b->rtree = j;
j->ch =choice;
insert(j);
}
else
b->rtree = NULL;
}
void bt::presearch(bintree * tree)
{
if(tree)
{
cout<<" "<<tree->ch<<" ";
presearch(tree->ltree);
presearch(tree->rtree);
}
}
void bt::insearch(bintree * tree)
{
if(tree)
{
insearch(tree->ltree);
cout<<" "<<tree->ch<<" ";
insearch(tree->rtree);
}
}
void bt::nexsearch(bintree * tree)
{
if(tree)
{
nexsearch(tree->ltree);
nexsearch(tree->rtree);
cout<<" "<<tree->ch<<" ";
}
}
void bt::delete_tree(bintree * tree)
{
if(tree)
{
delete_tree(tree->ltree);
delete_tree(tree->ltree);
delete tree;
}
}
bt::~bt()
{
delete_tree(root);
}