//thrbitree.h
#ifndef THRBITREE_H
#define THRBITREE_H
#include <iostream>
using namespace std;
enum flag {child,Thread};
template <class T>
struct ThrNode
{
T data;
ThrNode<T> *lchild,*rchild;
flag ltag,rtag;
};
template <class T>
class InThrBiTree
{
public:
InThrBiTree();
~InThrBiTree(){};
ThrNode<T>*Next(ThrNode<T>*p);
void InOrder();
private:
ThrNode<T> *root;
ThrNode<T> *Creat(ThrNode<T> *bt);
void ThrBiTree(ThrNode<T> *bt,ThrNode<T> *pre);
};
template <class T>
ThrNode<T>* InThrBiTree<T>::Creat(ThrNode<T> *bt)
{
char ch;
cin>>ch;
if(ch=='#') bt=NULL;
else {
bt = new ThrNode<T>;
bt->data = ch;
bt->ltag = 0;
bt->rtag = 0;
bt->lchild = Creat(bt->lchild);
bt->rchild = Creat(bt->rchild);
}
return bt;
}
template <class T>
void InThrBiTree<T>::ThrBiTree(ThrNode<T> *bt,ThrNode<T> *pre)
{
if(bt == NULL) return;
ThrBiTree(bt->lchild,pre);
if(bt->lchild == NULL) {
bt->ltag = 1;
bt->lchild = pre;
}
if(bt->rchild == NULL) bt->rtag = 1;
if(pre->rtag == 1) pre->rchild = bt;
pre = bt;
ThrBiTree(bt->rchild,pre);
}
template <class T>
InThrBiTree<T>::InThrBiTree()
{
root = Creat(root);
ThrNode<T> *pre = NULL;
ThrBiTree(root,pre);
}
template <class T>
ThrNode<T>*InThrBiTree<T>::Next(ThrNode<T>*p)
{
ThrNode<T>*q = NULL;
if(p->rtag == 1) q = p->rchild;
else {
q = p->rchild;
while(q -> ltag ==1)
q = q->lchild;
}
return q;
}
template <class T>
void InThrBiTree<T>::InOrder()
{
ThrNode<T>*p = NULL;
if(root == NULL) return;
p = root;
while(p->ltag == 0)
p = p->lchild;
cout<<p->data;
while(p->rchild != NULL)
{
p = Next(p);
cout<<p->data;
}
}
#endif
源文件
#include "thrbitree.h"
int main()
{
cout<<"请输入结点元素:"<<endl;
InThrBiTree<char> in;
in.InOrder();
return 0;
}
--------------------Configuration: 1123——1 - Win32 Debug--------------------
Compiling...
thread.cpp
d:\microsoft visual studio\myprojects\1123——1\thrbitree.h(35) : error C2440: '=' : cannot convert from 'const int' to 'enum flag'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
d:\microsoft visual studio\vc98\include\xlocale(467) : while compiling class-template member function 'struct ThrNode<char> *__thiscall InThrBiTree<char>::Creat(struct ThrNode<char> *)'
d:\microsoft visual studio\myprojects\1123——1\thrbitree.h(36) : error C2440: '=' : cannot convert from 'const int' to 'enum flag'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
d:\microsoft visual studio\vc98\include\xlocale(467) : while compiling class-template member function 'struct ThrNode<char> *__thiscall InThrBiTree<char>::Creat(struct ThrNode<char> *)'
d:\microsoft visual studio\myprojects\1123——1\thrbitree.h(48) : error C2440: '=' : cannot convert from 'const int' to 'enum flag'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
d:\microsoft visual studio\vc98\include\xlocale(467) : while compiling class-template member function 'void __thiscall InThrBiTree<char>::ThrBiTree(struct ThrNode<char> *,struct ThrNode<char> *)'
d:\microsoft visual studio\myprojects\1123——1\thrbitree.h(51) : error C2440: '=' : cannot convert from 'const int' to 'enum flag'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
d:\microsoft visual studio\vc98\include\xlocale(467) : while compiling class-template member function 'void __thiscall InThrBiTree<char>::ThrBiTree(struct ThrNode<char> *,struct ThrNode<char> *)'
执行 cl.exe 时出错.
1123——1.exe - 1 error(s), 0 warning(s)