问题:设计一个中序线索遍历类。要求设计时要考虑未来可能对前序线索遍历类和后序遍历类的设计需要,并要求结点的数据域为char类型。
建一个inbitree工程(win 32 console application),并创建一个inbitree文件(c++ source file)。程序的代码如下:
// bitrthnode.h
#include<stdlib.h>
#include <iostream.h>
class threaditerator;
class inthreaditerator;
class bitrthnode
{
friend threaditerator;
friend inthreaditerator;
private:
int leftthread;
int rightthread;
bitrthnode *leftchild;
bitrthnode *rightchild;
char data;
public:
bitrthnode():
leftthread(0),rightthread(0),leftchild(NULL),rightchild(NULL){}
bitrthnode(const char &item,bitrthnode *left=NULL,bitrthnode *right=NULL):
data(item),leftchild(left),rightchild(right),leftthread(0),rightthread(0){}
};
// threaditerator.h
class threaditerator //线索遍历类
{
protected:
bitrthnode *root;
bitrthnode *current;
int nextcomplete;
public:
threaditerator(bitrthnode *tree);
int endofnext(void)const
{return nextcomplete;}
char data(void);
};
threaditerator::threaditerator(bitrthnode *tree)
{
root=tree;
current=root;
if(tree==NULL)
nextcomplete=1;
else nextcomplete=0;
}
char threaditerator::data(void)
{
if(current==NULL)
{
cout<<"二叉树空!"<<endl;
exit (1);
}
return current->data;
cout<<"data:"<<current->data<<endl;
}
//inthreaditerator.h
/*#include <threaditerator.h>*/
class inthreaditerator: public threaditerator //中序遍历类
{
private:
void inthread(bitrthnode* ¤t,bitrthnode* &pre);
void creatinthread();
public:
inthreaditerator(bitrthnode* tree):threaditerator(tree)
{creatinthread();}
void first(void);
void next(void);
};
void inthreaditerator::inthread(bitrthnode* ¤t,bitrthnode* &pre)
{
if(current!=NULL)
{
inthread(current->leftchild,pre);
if(current->leftchild==NULL)
{
current->leftchild=pre;
current->leftthread=1;
}
if(pre->rightchild==NULL)
{
pre->rightchild=current;
pre->rightthread=1;
}
pre=current;
inthread(current->rightchild,pre);
}
}
void inthreaditerator::creatinthread()
{
bitrthnode *t=root;
root=new bitrthnode();
root->leftthread=0;
root->rightthread=1;
if(t==NULL)
{
root->leftchild=root;
root->rightchild=root;
}
else
{
current=t;
root->leftchild=t;
root->leftthread=0;
bitrthnode* pre=root;
inthread(current,pre);
pre->rightchild=root;
pre->rightthread=1;
root->rightchild=pre;
root->rightthread=1;
}
}
void inthreaditerator::first(void)
{
current=root;
if(current==root)nextcomplete=1;
else nextcomplete=0;
}
void inthreaditerator::next(void)
{
if(nextcomplete==1)
{
cerr<<"已到二叉树尾!"<<endl;
exit(1);
}
bitrthnode* p=current->rightchild;
if(current->rightthread==0)
while(p->leftthread==0) p=p->leftchild;
current=p;
if(current==root)nextcomplete=1;
}
#include<iostream.h>
/*#include "inthreaditerator.h"
#include "bitrthnode.h"*/
bitrthnode *gettrthnode(char item,bitrthnode *left=NULL,bitrthnode *right=NULL,int leftthread=0,int rightthread=0);
void makecharthtree(bitrthnode* &root);
void printchar(char item);
void main()
{
bitrthnode *root2;
makecharthtree(root2);
inthreaditerator myinthreaditer(root2);
cout<<"二叉树的中序遍历序列为:";
for(myinthreaditer.first(); !myinthreaditer.endofnext();myinthreaditer.next())
cout<<myinthreaditer.data()<<" ";
}
bitrthnode *gettrthnode(char item,bitrthnode *left,bitrthnode *right,int leftthread,int rightthread)
{
bitrthnode *p;
p=new bitrthnode(item,left,right);
if(p==NULL)
{
cerr<<"内存分配失败!/n";
exit(1);
}
return p;
}
void makecharthtree(bitrthnode* &root)
{
bitrthnode *b,*c,*d,*f,*e,*g,*h;
g=gettrthnode('G',d,b,1,1);
d=gettrthnode('D',root,g,1,0);
b=gettrthnode('B',d,root,0,1);
h=gettrthnode('H',e,f,0,0);
e=gettrthnode('E',root,h,1,0);
f=gettrthnode('F',c,root,1,1);
c=gettrthnode('C',e,f,0,0);
root=gettrthnode('A',b,c,0,0);
}
void printchar(char item)
{
cout<<item<<" ";
}
编译后出现以下错误:
Compiling...
bitree1.cpp
E:/vc answer/bitree1/bitree1.cpp(167) : warning C4700: local variable 'b' used without having been initialized
E:/vc answer/bitree1/bitree1.cpp(167) : warning C4700: local variable 'd' used without having been initialized
E:/vc answer/bitree1/bitree1.cpp(170) : warning C4700: local variable 'f' used without having been initialized
E:/vc answer/bitree1/bitree1.cpp(170) : warning C4700: local variable 'e' used without having been initialized
E:/vc answer/bitree1/bitree1.cpp(172) : warning C4700: local variable 'c' used without having been initialized
为什么?希望大学给予扶助。谢谢....