全て!
Binary Tree を Pre-Thread にしてから出力する関数を開発しようとしていますが、いくつか問題が発生しています。コードはコンパイルされていますが、プログラムを実行するとクラッシュします。以下に私のコードがあります。
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
#define MaxNodeNum 50
class ThreadTree;
istream& operator>>(istream& in,ThreadTree& Tree);
struct ThreadNode{`int ltag,rtag;
ThreadNode *leftChild,*rightChild;
char data;
ThreadNode():data(NULL),leftChild(NULL),
rightChild(NULL),ltag(0),rtag(0){}
ThreadNode(const char item):data(item),leftChild(NULL),
rightChild(NULL),ltag(0),rtag(0){}
};
class ThreadTree{`protected:
ThreadNode *root;
char *element;
int CurNodeNum;
void CreateBinTree(ThreadNode*& subTree);
friend istream& operator>>(istream& in,ThreadTree& Tree);
void createPreThread(ThreadNode *current,ThreadNode *& pre);
void destroy(ThreadNode *&subTree);
public:
ThreadTree():root(NULL){}
void createPreThread();
ThreadNode *Root()
{ return root; }
void ConstructTree(char A[],int n,int i,ThreadNode *& ptr);
ThreadNode *First(ThreadNode *current);
ThreadNode *Next(ThreadNode *current);
void PreOrder();
~ThreadTree(){ destroy(root); }
};
void ThreadTree::CreateBinTree(ThreadNode*& subTree){
char s[MaxNodeNum];
cout<<"Input the nodes,'#'represents null nodes,the number of nodes<="<<MaxNodeNum<<endl;
cin>>s;
CurNodeNum=strlen(s);
element=new char[CurNodeNum];
for(int i=0;i<CurNodeNum;i++)
element[i]=s[i];
}
void ThreadTree::ConstructTree(char A[],int n,int i,ThreadNode *& ptr){
if(i>=n) ptr=NULL;
else{
if(A[i]!='#'){
ptr=new ThreadNode(A[i]);
ConstructTree(A,n,2*i+1,ptr->leftChild);
ConstructTree(A,n,2*i+2,ptr->rightChild);
}
else i++;
}
}
istream& operator>>(istream& in,ThreadTree& Tree){
Tree.CreateBinTree(Tree.root);
Tree.ConstructTree(Tree.element,Tree.CurNodeNum,0,Tree.root);
return in;
}
void ThreadTree::createPreThread(){
ThreadNode *pre=NULL;
if(root!=NULL){
createPreThread(root,pre);
pre->rightChild=NULL; pre->rtag=1;
}
}
void ThreadTree::createPreThread(ThreadNode *current,ThreadNode *& pre){
if(current==NULL) return;
if(current->leftChild==NULL){
current->leftChild=pre;
current->ltag=1;
}
if(pre!=NULL && pre->rightChild==NULL){
pre->rightChild=current;
pre->rtag=1;
}
pre=current;
createPreThread(current->leftChild,pre);
createPreThread(current->rightChild,pre);
}
ThreadNode* ThreadTree::First(ThreadNode *current){
return current;
}
ThreadNode * ThreadTree::Next(ThreadNode *current){
if(current->ltag==1){
if(current->rightChild==NULL)
return NULL;
else
return current->rightChild;
}else
return current->leftChild;
}
void ThreadTree::PreOrder(){
ThreadNode *q;
for(q=First(root);q!=NULL;q=Next(q))
cout<<q->data<<" ";
}
void ThreadTree::destroy(ThreadNode *&subTree){
if(subTree!=NULL){
destroy(subTree->leftChild);
destroy(subTree->rightChild);
delete subTree;
}
}
int main(){
ThreadTree tree;
cin>>tree;
tree.createPreThread();
cout<<tree.Next(tree.Root())->data;
cout<<endl;
tree.PreOrder();
return 0;
}
これについて私を助けてください。前もって感謝します!