//以下是头文件 TTree.h
//以下所有均与非线索的二叉树相同,唯一不同的是在创建二叉树的函数
//CreateTTree()中多了两句:
//p->ltag=p->rtag=1;s[top]->ltag=0;s[top]->rtag=0;
struct TTreeNode{
ElemType data;
int ltag,rtag;
TTreeNode* left;
TTreeNode* right;
};
void InitTTree(TTreeNode* &TT )
{
TT=NULL;
}
void CreateTTree(TTreeNode* &TT ,char* a)
{
TTreeNode* s[10];
int top=-1;
TT=NULL;
TTreeNode *p;
int k;
istrstream ins(a);
char ch;
ins >>ch;
while(ch!='@')
{
switch(ch)
{
case '(':
top++;s[top]=p;k=1;
break;
case')':
top--;
break;
case',':
k=2;
break;
default:
p=new TTreeNode;
p->data=ch;p->left=p->right=NULL;p->ltag=p->rtag=1;
if(TT==NULL)
TT=p;
else{
switch(k)
{
case 1:
s[top]->left=p;s[top]->ltag=0;
break;
case 2:
s[top]->right=p;s[top]->rtag=0;
}
}
}
ins>>ch;
}
}
int TTreeEmpty(TTreeNode* TT)
{
return TT==NULL;
}
void Preorder(TTreeNode* TT)//前序遍历
{
if(TT!=NULL){
cout < <TT- > data < <' ';
Preorder (TT- > left);
Preorder(TT->right);
}
}
void Inorder(TTreeNode* TT)//中序遍历
{
if(TT!=NULL){
Inorder(TT->left);
cout < <TT- > data < <' ';
Inorder (TT- > right);
}
}
void Postorder(TTreeNode* TT)//后序遍历
{
if(TT!=NULL){
Postorder(TT->left);
Postorder(TT->right);
cout < <TT- > data < <' ';
}
}
void Levelorder(TTreeNode* TT)
{
TTreeNode* q[30];
int front =0,rear=0;
TTreeNode* p;
if(TT! =NULL){
rear =(rear+1)%30;
q[rear] =TT;
}
while(front! =rear)
{
front =(front+1)%30;
p =q[front];
cout<<p- > data < <' ';
if (p- > left!=NULL){
rear=(rear+1)%30;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%30;
q[rear]=p->right;
}
}
}
TTreeDepth(TTreeNode* TT)
{
if(TT==NULL)
return 0;
else
{
int dep1=TTreeDepth(TT->left);
int dep2=TTreeDepth(TT->right);
if(dep1>dep2)
return dep1+1;
else
return dep2+1;
}
}
void PrintTTree(TTreeNode* TT)
{
if(TT!=NULL)
{
cout < <TT- > data;
if(TT->left!=NULL||TT->right!=NULL)
{
cout < <'(';
PrintTTree (TT- > left);
if(TT->right!=NULL)
cout < <',';
PrintTTree (TT- > right);
cout < <')';
}
}
}
void DeleteTTree(TTreeNode* TT)
{
if(TT! =NULL)
{
DeleteTTree(TT- > left);
DeleteTTree(TT->right);
delete TT;
}
}
void ClearTTree(TTreeNode* &TT )
{
DeleteTTree(TT);
TT=NULL;
}
void InThread(TTreeNode* TT)//对以树根指针为HT的二叉树加中序线索化
{
static TTreeNode* pre=NULL;
if(TT!=NULL)
{
if(TT->ltag==0)InThread(TT->left);
if(pre!=NULL& &pre- >rtag==1)
pre->right=TT;
if(TT->left==NULL)
{
TT->ltag=1;
TT->left=pre;
}
if(TT->right==NULL)
TT->rtag=1;
pre=TT;
if(TT->rtag==0)InThread(TT->right);
}
}
TTreeNode* InorderNext(TTreeNode* p)//在中序线索二叉树上求结点P的中序后继算法
{
if(p->rtag==1)
return p->right;
else{
p=p->right;
while(p->ltag==0)
p=p->left;
return p;
}
}
void ThInorder(TTreeNode* TT)
{
if(TT!=NULL){
while(TT->ltag==0)
TT=TT->left;
do{
cout < <TT- > data < <' ';
TT =InorderNext(TT);
}while(TT! =NULL);
}
}
//以下是源文件 TTree.cpp
#include<iostream.h >
#include < conio .h >
#include < stdlib .h >
#include < strstrea .h >
typedef char ElemType;
#include"TTree.h"
void main()
{
TTreeNode* bt;
InitTTree(bt);
char b[50];
cout < <"输入以'@'字符作为结束符的二叉树广义表表示: "<<endl;
cin.getline(b,sizeof(b));
CreateTTree(bt,b);
PrintTTree(bt);cout<<endl;
InThread(bt);
ThInorder(bt);cout<<endl;
getch();
}
/*
http://f2.9612.org//vcpp/webinfo/WebInfoBata1.asp
QQ群:
34409541 讨论网页
34409326 讨论JAVA 已满
34408784 讨论VC++
34409699 讨论VC++
9143041 讨论MFC编程
10614204 讨论C#
10613030 讨论Win32编程
10613067 讨论游戏开发
18779860 讨论JAVA
*/
//以下所有均与非线索的二叉树相同,唯一不同的是在创建二叉树的函数
//CreateTTree()中多了两句:
//p->ltag=p->rtag=1;s[top]->ltag=0;s[top]->rtag=0;
struct TTreeNode{
ElemType data;
int ltag,rtag;
TTreeNode* left;
TTreeNode* right;
};
void InitTTree(TTreeNode* &TT )
{
TT=NULL;
}
void CreateTTree(TTreeNode* &TT ,char* a)
{
TTreeNode* s[10];
int top=-1;
TT=NULL;
TTreeNode *p;
int k;
istrstream ins(a);
char ch;
ins >>ch;
while(ch!='@')
{
switch(ch)
{
case '(':
top++;s[top]=p;k=1;
break;
case')':
top--;
break;
case',':
k=2;
break;
default:
p=new TTreeNode;
p->data=ch;p->left=p->right=NULL;p->ltag=p->rtag=1;
if(TT==NULL)
TT=p;
else{
switch(k)
{
case 1:
s[top]->left=p;s[top]->ltag=0;
break;
case 2:
s[top]->right=p;s[top]->rtag=0;
}
}
}
ins>>ch;
}
}
int TTreeEmpty(TTreeNode* TT)
{
return TT==NULL;
}
void Preorder(TTreeNode* TT)//前序遍历
{
if(TT!=NULL){
cout < <TT- > data < <' ';
Preorder (TT- > left);
Preorder(TT->right);
}
}
void Inorder(TTreeNode* TT)//中序遍历
{
if(TT!=NULL){
Inorder(TT->left);
cout < <TT- > data < <' ';
Inorder (TT- > right);
}
}
void Postorder(TTreeNode* TT)//后序遍历
{
if(TT!=NULL){
Postorder(TT->left);
Postorder(TT->right);
cout < <TT- > data < <' ';
}
}
void Levelorder(TTreeNode* TT)
{
TTreeNode* q[30];
int front =0,rear=0;
TTreeNode* p;
if(TT! =NULL){
rear =(rear+1)%30;
q[rear] =TT;
}
while(front! =rear)
{
front =(front+1)%30;
p =q[front];
cout<<p- > data < <' ';
if (p- > left!=NULL){
rear=(rear+1)%30;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%30;
q[rear]=p->right;
}
}
}
TTreeDepth(TTreeNode* TT)
{
if(TT==NULL)
return 0;
else
{
int dep1=TTreeDepth(TT->left);
int dep2=TTreeDepth(TT->right);
if(dep1>dep2)
return dep1+1;
else
return dep2+1;
}
}
void PrintTTree(TTreeNode* TT)
{
if(TT!=NULL)
{
cout < <TT- > data;
if(TT->left!=NULL||TT->right!=NULL)
{
cout < <'(';
PrintTTree (TT- > left);
if(TT->right!=NULL)
cout < <',';
PrintTTree (TT- > right);
cout < <')';
}
}
}
void DeleteTTree(TTreeNode* TT)
{
if(TT! =NULL)
{
DeleteTTree(TT- > left);
DeleteTTree(TT->right);
delete TT;
}
}
void ClearTTree(TTreeNode* &TT )
{
DeleteTTree(TT);
TT=NULL;
}
void InThread(TTreeNode* TT)//对以树根指针为HT的二叉树加中序线索化
{
static TTreeNode* pre=NULL;
if(TT!=NULL)
{
if(TT->ltag==0)InThread(TT->left);
if(pre!=NULL& &pre- >rtag==1)
pre->right=TT;
if(TT->left==NULL)
{
TT->ltag=1;
TT->left=pre;
}
if(TT->right==NULL)
TT->rtag=1;
pre=TT;
if(TT->rtag==0)InThread(TT->right);
}
}
TTreeNode* InorderNext(TTreeNode* p)//在中序线索二叉树上求结点P的中序后继算法
{
if(p->rtag==1)
return p->right;
else{
p=p->right;
while(p->ltag==0)
p=p->left;
return p;
}
}
void ThInorder(TTreeNode* TT)
{
if(TT!=NULL){
while(TT->ltag==0)
TT=TT->left;
do{
cout < <TT- > data < <' ';
TT =InorderNext(TT);
}while(TT! =NULL);
}
}
//以下是源文件 TTree.cpp
#include<iostream.h >
#include < conio .h >
#include < stdlib .h >
#include < strstrea .h >
typedef char ElemType;
#include"TTree.h"
void main()
{
TTreeNode* bt;
InitTTree(bt);
char b[50];
cout < <"输入以'@'字符作为结束符的二叉树广义表表示: "<<endl;
cin.getline(b,sizeof(b));
CreateTTree(bt,b);
PrintTTree(bt);cout<<endl;
InThread(bt);
ThInorder(bt);cout<<endl;
getch();
}
/*
http://f2.9612.org//vcpp/webinfo/WebInfoBata1.asp
QQ群:
34409541 讨论网页
34409326 讨论JAVA 已满
34408784 讨论VC++
34409699 讨论VC++
9143041 讨论MFC编程
10614204 讨论C#
10613030 讨论Win32编程
10613067 讨论游戏开发
18779860 讨论JAVA
*/