1. 程式人生 > >樹的創建與遍歷

樹的創建與遍歷

this using open out write template pos oid \n

#include<iostream>
#include<list>
#include<fstream>
#include<queue>
using namespace std;

template<class T>
class Tree //樹的建立
{
T *NA;
list<int> *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<int>[n];
}
~Tree(){delete []NA; delete []HL;}
int Empty()const{return size==0;}
int Full()const{return sizeN==maxN;}
int SizeN()const{return sizeN;}
int SizeC()const{return sizeC;}
int FindNode(const T& v)const;
bool FindNode(T& v,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);
friend ostream& operator<<(ostream& ostr,const Tree<T>& t)
{
for(int i=0;i<t.sizeN;i++)
{
ostr<<i<<‘-‘<<t.NA[i]<<‘:‘;
list<int>::iterator first=t.HL[i].begin(),last=t.HL[i].end();
//cout<<*first<<endl;
//cout<<*last<<endl;
for(;first!=last;++first)
{
ostr<<‘(‘<<*first<<‘)‘<<‘ ‘;
}
ostr<<endl;
}
return ostr;
}
friend istream& operator>>(istream& istr,Tree<T>& t)
{
char str[50];
int n,i;
T parent,child;
istr>>str>>n;
istr>>str>>parent;
t.InsertN(parent);
t.root=t.sizeN-1;
istr>>str;
for(i=1;i<=n-1;++i)
{
istr>>child;
t.InsertN(child);
}
istr>>str>>n;
for(i=1;i<=n;++i)
{
istr>>parent>>child;
t.InsertC(parent,child);
}
return istr;
}
void BFS();
void DFS(int r);
};

template <class T>
int Tree<T>::FindNode(const T& node)const //找到node結點並返回其下標
{
for(int i=0;i<sizeN;i++)
{
if(NA[i]==node)
return i;
}
return -1;
}

template<class T>
bool Tree<T>::FindNode(T& node,int pos)const //將pos處的結點賦值給node
{
if(pos<0||pos>=sizeN)
{
return 0;
}
node=NA[pos];
return 1;
}

template<class T> //插入結點
bool Tree<T>::InsertN(const T& node)
{
if(sizeN==maxN)
{
return 0;
}
NA[sizeN]=node;
sizeN++;
return 1;
}

template <class T> //插入子節點
bool Tree<T>::InsertC(const T& parent,const T& child)
{
int pi=FindNode(parent),cj=FindNode(child);
//cout<<pi<<‘ ‘<<cj<<endl;
if(pi==-1||cj==-1||pi==cj)
{
return 0;
}
HL[pi].insert(HL[pi].end(),cj);
sizeC++;
return 1;
}

template <class T>
void Tree<T>::BFS() //BFS
{
queue<int> q;
q.push(root);
while(!q.empty())
{
int temp=q.front();
q.pop();
cout<<NA[temp]<<‘ ‘;
for(list<int>::iterator it=HL[temp].begin();it!=HL[temp].end();it++)
{
q.push(*it);
}
}
}

template <class T>
void Tree<T>::DFS(int r) //DFS
{
cout<<NA[r]<<‘ ‘;
for(list<int>::iterator it=HL[r].begin();it!=HL[r].end();it++)
{
DFS(*it);
}
}

int main()
{
/*
Tree<char> T(20);
cin>>T;
cout<<T;*/
Tree<char> T(20);
ifstream fin;
fin.open("treein.txt",ios::in);
if(!fin)
{
cout<<"cannot open this file\n";
exit(1);
}
fin>>T;
cout<<T;
cout<<"BFS遍歷序列:"<<endl;
T.BFS();
cout<<endl;
cout<<"DFS遍歷序列:"<<endl;
T.DFS(0);
return 0;
}

本篇文章中的輸入采用的是C++的文件輸入格式為

<Node> 13

<Root> A

<Others>

B C D E F G H I J K L M

<Childs> 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

其中 第一行是結點數,第二行是根節點,第三,四行是其余的結點,第五行是其余結點的數量,後12行的前一個字符是父節點,後一個字符是父節點的子節點

因為在本篇文章中重載了Tree的>>和<<因此也可以直接采用cin>>T 和cout<<T的輸入輸出方式。

樹的創建與遍歷