?? exam5.cpp
字號:
#include<iostream>
#include"exam3.h"
#include<fstream>
#include<string>
#include<vector>
using namespace std;
template<class T>
struct Tnode{ //森林的結點結構
T data;
Tnode<T> *child,*brother;
};
//****************************************************************
template<class T>
struct lnode
{
int tag;
union
{
T data;
lnode<T> *sublist;
}val;
lnode<T> *link;
};
//****************************************************************
template<class T>
class GL{
public:
void disGL(lnode<T> *p)
{
if(p!=NULL)
{
if(p->tag==1)
{
cout<<"(";
if(p->val.sublist==NULL)
cout<<" ";
else disGL(p->val.sublist);
}
else cout<<p->val.data;
}
}
};
//****************************************************************
template<class T>
class Tree{
public:
Tree();
~Tree();
void filecreat(Tnode<T> * &root); //文件讀取構造二叉樹
void fcreatTree(ifstream& f,Tnode<T> *&p); //文件讀取構造二叉樹的遞歸
void trans(Tnode<T>* p,BTnode<T>* root);
int height(Tnode<T>* p);
int Nodecount(Tnode<T>* p);
void display1(Tnode<T>* p);
void display2(Tnode<T>* p);
void show(Tnode<T>* p);
BTnode<T> *root;
};
template<class T>
Tree<T>::Tree() //構造函數
{
root=NULL; //初始化根結點
}
//*********************************************************************
/*template <class T>
void TBT<T>::fcreatTree(ifstream& f,TBTnode<T> *&p)//讀取文件的遞歸程序
{
char ch1,ch2;
p=new BTnode<T>;
f>>p->data;
f>>ch1;f>>ch2; //以文件中每一行的三個字符為單位讀取
if(ch1=='0')
fcreatTree(f,p->lchild);
else p->lchild=NULL;
if(ch2=='0')
fcreatTree(f,p->rchild);
else p->rchild=NULL;
}
template <class T>
void TBT<T>::filecreat(TBTnode<T> * &root) //從文件中讀取構造二叉樹
{
char ch[40];char a;string s;
cout<<"請輸入文件的絕對路徑:"<<endl;
cin>>ch;
ifstream input(ch);
if(!input)
{
cout<<"打開文件失敗!"<<endl;
return;
}
getline(input,s);input>>a;
if(a=='1')
{
root=NULL;
return;
}
else fcreatTree(input,root);
input.close();
}*/
//****************************************************************
template<class T>
void Tree<T>::trans(Tnode<T>* p,BTnode<T>* root) //將樹(森林)的孩子兄弟鏈表形式轉換成二叉樹形式
{
if(p!=NULL)
{
root=new BTnode<T>;
root->data=p->data;
trans(p->brother,root->rchild);
trans(p->child,root->lchild);
}
}
//****************************************************************
template<class T>
int Tree<T>::height(Tnode<T>* p) //求森林的高度
{
if(p==NULL)return 0;
int h1,h2;
h1=height(p->brother);
h2=height(p->child);
return (h1>h2)?h1:(h2+1);
}
//****************************************************************
template<class T>
int Tree<T>::Nodecount(Tnode<T>* p)
{
if(p==NULL)return 0;
else return Nodecount(p->brother)+Nodecount(p->child)+1;
}
//****************************************************************
template<class T>
void Tree<T>::display1(Tnode<T>* p) //層次遍歷森林
{
}
//****************************************************************
template<class T>
void Tree<T>::display2(Tnode<T>* p) //輸出一個森林中每個結點的值及其對應的層次數
{
}
//****************************************************************
template<class T>
void Tree<T>::show(Tnode<T>* p) //輸出一個森林的廣義表形式
{
if(p!=NULL)
{
cout<<"("<<p->data;
show(p->child);
if(p->brother!=NULL)
{
cout<<"),";
show(p->brother);
}
else cout<<
}
}
void main()
{
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -