7075
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct TNode{
char data;
struct TNode *lchild, *rchild;
}BiNode, *BiTree;
BiTree T;
void createTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='@') T=NULL;
else
{
T=new TNode;
T->data=ch;
createTree(T->lchild);
createTree(T->rchild);
}
}
void printTree_back(BiTree &T)
{
if(T)
{
printTree_back(T->lchild);
printTree_back(T->rchild);
cout<<T->data;
}
}
int main()
{
createTree(T);
printTree_back(T);
return 0;
}
7078(线索二叉树,中序遍历)
#include <iostream>
#include<cstdio>
using namespace std;
typedef struct BThrNode{
char data;
struct BThrNode *lchild, *rchild;
int ltag, rtag;
}BThrNode, *BThrTree;
BThrTree T,p;
void createBTree(BThrTree &T)
{
char ch;
cin>>ch;
if(ch=='@') T=NULL;
else
{
T=new BThrNode;
T->data=ch;
createBTree(T->lchild);
createBTree(T->rchild);
}
}
BThrTree pre=NULL;
void InThread(BThrTree p)
{
if(p!=NULL)
{
InThread(p->lchild);
if(p->lchild==NULL){p->ltag=1; p->lchild=pre;}
else p->ltag=0;
if(p->rchild==NULL){p->rtag=1; pre->rchild=p;}
else pre->rtag=0;
pre=p;
InThread(p->rchild);
}
}
void TraveBTree(BThrTree &T)
{
if(T)
{
TraveBTree(T->lchild);
cout<<T->data;
TraveBTree(T->rchild);
}
}
int main()
{
createBTree(T);
InThread(p);
TraveBTree(T);
return 0;
}
7079(中序遍历)
#include <iostream>
#include<cstdio>
using namespace std;
typedef struct BTNode{
char data;
struct BTNode *lchild, *rchild;
//int ltag, rtag;
}BTNode, *BTree;
BTree T;
void createBTree(BTree &T)
{
char ch;
cin>>ch;
if(ch=='@') T=NULL;
else
{
T=new BTNode;
T->data=ch;
createBTree(T->lchild);
createBTree(T->rchild);
}
}
//void InThread(BThrTree p)
//{
// if(p!=NULL)
// {
// InThread(p->lchild);
// if(p->lchild==NULL){p->ltag=1; p->lchild=pre;}
// else p->ltag=0;
// if(p->rchild==NULL){p->rtag=1; pre->rchild=p;}
// else pre->rtag=0;
// pre=p;
// InThread(p->rchild);
// }
//}
void TraveBTree(BTree &T)
{
if(T)
{
TraveBTree(T->lchild);
cout<<T->data;
TraveBTree(T->rchild);
}
}
int main()
{
createBTree(T);
TraveBTree(T);
return 0;
}
7077(层次遍历)
#include <iostream>
#include<cstdio>
#include <queue>
using namespace std;
const int MAX_SIZE=1010;
typedef struct BTNode{
char data;
struct BTNode *lchild, *rchild;
//int ltag, rtag;
}BTNode, *BTree;
typedef struct {
BTNode data[MAX_SIZE];
int front,rear;
}SQ;
BTree T;
void createBTree(BTree &T)
{
char ch;
cin>>ch;
if(ch=='@') T=NULL;
else
{
T=new BTNode;
T->data=ch;
createBTree(T->lchild);
createBTree(T->rchild);
}
}
//void InThread(BThrTree p)
//{
// if(p!=NULL)
// {
// InThread(p->lchild);
// if(p->lchild==NULL){p->ltag=1; p->lchild=pre;}
// else p->ltag=0;
// if(p->rchild==NULL){p->rtag=1; pre->rchild=p;}
// else pre->rtag=0;
// pre=p;
// InThread(p->rchild);
// }
//}
void TraveBTree(BTree &T)
{
BTree p;
queue<BTree> Q;
if(T)
{
Q.push(T);
while (!Q.empty())
{
p=Q.front();
cout<<p->data;
Q.pop();
if(p->lchild) Q.push(p->lchild);
if(p->rchild) Q.push(p->rchild);
}
}
}
int main()
{
createBTree(T);
TraveBTree(T);
return 0;
}
7076
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct BTNode{
char data;
struct BTNode *lchild, *rchild;
}BTNode, *BTree;
BTree T;
void createBTree(BTree &T)
{
char ch;
cin>>ch;
if(ch=='@') T=NULL;
else
{
T=new BTNode;
T->data=ch;
createBTree(T->lchild);
createBTree(T->rchild);
}
}
int Trave(BTree T)
{
//int num=0;
if(!T) return 0;
else
{
if(T->lchild==NULL && T->rchild==NULL) return 1;
else return Trave(T->lchild)+ Trave(T->rchild);
}
return 0;
}
int main()
{
createBTree(T);
int ans= Trave(T);
cout<<ans<<endl;
return 0;
}
7074
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct BTNode{
char data;
struct BTNode *lchild, *rchild;
}BTNode, *BTree;
BTree T;
void createBTree(BTree &T)
{
char ch;
cin>>ch;
if(ch=='@') T=NULL;
else
{
T=new BTNode;
T->data=ch;
createBTree(T->lchild);
createBTree(T->rchild);
}
}
int Trave(BTree T)
{
//int num=0;
if(!T) return 0;
else
{
if(T->lchild==NULL && T->rchild==NULL) return 1;
else return Trave(T->lchild)+ Trave(T->rchild);
}
return 0;
}
int Depth(BTree &T)
{
int m,n;
if(T==NULL) return 0;
else
{
m= Depth(T->lchild);
n= Depth(T->rchild);
if(m>n) return m+1;
else return n+1;
}
}
int main()
{
createBTree(T);
int ans= Depth(T);
cout<<ans<<endl;
return 0;
}
7080(父亲孩子表示法)
#include<iostream>
#include<cstdio>
using namespace std;
typedef struct BTNode{
char data;
struct BTNode *lchild, *rchild;
}BTNode, *BTree;
BTree T;
void createBTree(BTree &T)
{
char ch;
cin>>ch;
if(ch=='@') T=NULL;
else
{
T=new BTNode;
T->data=ch;
createBTree(T->lchild);
createBTree(T->rchild);
}
}
int Trave(BTree T)
{
//int num=0;
if(!T) return 0;
else
{
if(T->lchild==NULL && T->rchild==NULL) return 1;
else return Trave(T->lchild)+ Trave(T->rchild);
}
return 0;
}
int Depth(BTree &T)
{
int m,n;
if(T==NULL) return 0;
else
{
m= Depth(T->lchild);
n= Depth(T->rchild);
if(m>n) return m+1;
else return n+1;
}
}
int CountLeaf(BTree &T)
{
int ans=0;
if(T==NULL) return 0;
if(T->lchild==NULL) return CountLeaf(T->rchild)+1;
else
return CountLeaf(T->lchild)+ CountLeaf(T->rchild);
}
int main()
{
createBTree(T);
//int ans= Depth(T);
cout<<CountLeaf(T)<<endl;
return 0;
}
未完待续