//Tree.h
#include
#include
template
class Tree{
T*NA;
list*HL;
int root;
int sizeN,sizeC;
int maxN;
public:
Tree(int n = 100):root(-1), sizeN(0), sizeC(0), maxN(n){
NA = new T[n];
HL = new list[n];
}
~Tree(){ delete[]NA; delete[]HL; }
int Empty()const{ return size == 0; }
int Full()const{ return size == maxN; }
int SizeN()const{ return sizeN; }
int SizeC()const{ return sizeC; }
int FindNode(const T&node)const;
bool FindNode(const T&node, int pos)const;
bool InsertN(const T&node);
bool InsertC(const T&parent, const T&child);
void readTree(const char*filename);
void writeTree(const char*filename);
template friend ostream&operator<<(ostream& ostr, const Tree&t);
template friend istream&operator>>(istream& istr, Tree&t);
};
template
int Tree::FindNode(const T&node)const{
for (int i = 0; i < sizeN; i++)
if (NA[i] == node)
return(i);
return -1;
}
template
bool Tree::FindNode(const T&node, int pos)const{
if (pos < 0 || pos >= sizeN)
return 0;
node = NA[pos];
return 1;
}
template
bool Tree::InsertN(const T&node){
if (sizeN == maxN)
return 0;
NA[sizeN] = node;
sizeN++;
return 1;
}
template
bool Tree::InsertC(const T&parent, const T&child){
int pi = FindNode(parent), cj = FindNode(child);
if (pi == -1 || cj == -1 || pi == cj)
return 0;
HL[pi].insert(HL[pi].end(), cj);
sizeC++;
return 1;
}
template
istream& operator>>(istream& istr,Tree& t){
char str[50];
int n;
T parent,child;
istr >> str >> n;
istr >> str >> parent;
t.InsertN(parent);
t.root = t.sizeN - 1;
istr >> str;
for (int i = 1; i <= n - 1; ++i){
istr >> child;
t.InsertN(child);
}
istr >> str >> n;
for (int j = 1; j <= n; ++j){
istr >> parent >> child;
t.InsertC(parent, child);
}
return istr;
}
template
ostream&operator<<(ostream& ostr,const Tree& t){
for (int i = 0; i < t.sizeN; i++){
ostr << i << '-' << t.NA[i] << ':';
list::iterator first = t.HL[i].begin(), last = t.HL[i].end();
for (; first != last; ++first)
ostr << '(' << *first << ')' << ' ';
ostr << endl;
}
return (ostr);
}
//main函数
#include
#include
#include
#include"Tree.h"
int main(){
TreeT(20);
int c;
cin >> c;
switch (c){
case 1:{
cout << "从磁盘提取,输出至文件" << endl;
ifstream fin;
fin.open("treein.txt", ios::in);
if (!fin){
cout << "can not open treein.txt.\n";
exit(1);
}
fin >> T;
ofstream fout;
fout.open("Etreeout.txt", ios::out);
if (!fout){
cout << "cannot open treeout.txt.\n";
exit(1);
}
fout << T;
}break;
case 2:{
cout << "从文件提取,输出至终端:" << endl;
cin >> T;
cout << "结果:\n"<
}break;
}
system("pause");
return 0;
}
//输入文件信息
13
A
B C D E F G H I J K L M
12
A B
A C
A D
B E
B F
C G
D H
D I
D J
G K
G L
J M
//调试信息
d:\vc++6.0\microsoft visual studio\vc98\include\list.h(37) : error C2146: syntax error : missing ';' before identifier 'Length'
d:\vc++6.0\microsoft visual studio\vc98\include\list.h(37) : error C2501: 'DWORD' : missing storage-class or type specifiers
d:\vc++6.0\microsoft visual studio\vc98\include\list.h(37) : error C2501: 'Length' : missing storage-class or type specifiers
d:\vc++6.0\microsoft visual studio\vc98\include\list.h(53) : error C2146: syntax error : missing ';' before identifier 'GetPrevLink'
d:\vc++6.0\microsoft visual studio\vc98\include\list.h(53) : error C2433: 'WINAPI' : 'inline' not permitted on data declarations
d:\vc++6.0\microsoft visual studio\vc98\include\list.h(53) : fatal error C1004: unexpected end of file found