树的各种遍历方法
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
const int maxn = 1e5 + 10;
int sum=0;
typedef struct tree *tramp;
struct tree{
char data;
struct tree *left;
struct tree *right;
};
typedef struct SNode* Stack;
struct SNode {
tramp data;
struct SNode* next;
};
typedef struct
{
tramp data[maxn];
int rear;
int front;
}Queue;
tramp createTree()
{
tramp tree;
char str;
str = getchar();
if(str == '#'){
tree = NULL;
}else{
tree = (tramp)malloc(sizeof(tree));
tree->data = str;
tree->left = createTree();
tree->right = createTree();
}
return tree;
}
Stack MakeStack()
{
Stack ptrl;
ptrl=(Stack)malloc(sizeof(struct SNode));
ptrl->next=NULL;
return ptrl;
}
Queue* MakeQueue()
{
Queue* Q=(Queue*)malloc(sizeof(Queue));
Q->front=0;
Q->rear=0;
return Q;
}
int IsEmpty(Stack s)
{
if(s->next==NULL)
return (1);
else
return(0);
}
void push(Stack p, tramp s)
{
Stack ptrl;
ptrl = (Stack)malloc(sizeof(struct SNode));
ptrl->data = s;
ptrl->next = p->next;
p->next = ptrl;
}
void pop(Stack p,tramp &s)
{
Stack ptrl;
int item;
if (IsEmpty(p)) {
printf("堆栈空");
}
else {
ptrl = p->next;
s=ptrl->data;
p->next = ptrl->next;
free(ptrl);
}
}
void AddQ(Queue *PtrlQ,tramp item)
{
if(PtrlQ->rear+1==maxn){
printf("the Queue is full");
return ;
}
PtrlQ->rear=PtrlQ->rear+1;
PtrlQ->data[PtrlQ->rear]=item;
}
tramp DeleteQ ( Queue *PtrQ )
{
if ( PtrQ->front == PtrQ->rear ) {
printf("队列空");
return NULL;
} else {
PtrQ->front = PtrQ->front+1;
return PtrQ->data[PtrQ->front];
}
}
int isEmpty( Queue *PtrlQ )
{
if(PtrlQ->front == PtrlQ->rear) return 0;
else return 1;
}
int isFull( Queue *PtrlQ )
{
if(PtrlQ->rear+1==maxn) return 0;
else return 1;
}
void perOrder(tramp T)
{
if(T!=NULL){
printf("%c",T->data);
perOrder(T->left);
perOrder(T->right);
}
}
void inOrder(tramp T)
{
if(T!=NULL){
inOrder(T->left);
printf("%c",T->data);
inOrder(T->right);
}
}
void postOrder(tramp T)
{
if(T!=NULL){
postOrder(T->left);
postOrder(T->right);
printf("%c",T->data);
}
}
void iterInorder( tramp BT )
{
tramp T=BT;
Stack S = MakeStack();
while( T || !IsEmpty(S) ){
while(T){
push(S,T);
T = T->left;
}
if(!IsEmpty(S)){
pop(S,T);
printf("%c", T->data);
T = T->right;
}
}
}
void LevelOrder( tramp BT )
{
Queue* Q=MakeQueue();
tramp T;
if ( !BT ) return;
AddQ( Q, BT );
while ( isEmpty(Q)==1 ) {
T = DeleteQ( Q );
printf("%c", T->data);
if ( T->left ) AddQ( Q, T->left );
if ( T->right ) AddQ( Q, T->right );
}
}
void Leaves(tramp BT )
{
if( BT ) {
if ( !BT->left && !BT->right )
sum++;
Leaves ( BT->left );
Leaves ( BT->right );
}
}
int height(tramp tree)
{
int h1,h2;
if(tree == NULL){
return 0;
}else{
h1 = height(tree->left);
h2 = height(tree->right);
if(h1>h2){
return h1+1;
}else{
return h2+1;
}
}
}
int main(){
tramp tree;
tree = createTree();
int i=0;
int l=1,treeHeight,treeCount;
treeHeight = height(tree);
printf("\ntreeHeight: %d\n",treeHeight);
Leaves(tree);
printf("leaf: %d\n",sum);
printf("perOrder: ");
perOrder(tree);
printf("\n");
printf("inOrder: ");
inOrder(tree);
printf("\n");
printf("postOrder: ");
postOrder(tree);
printf("\n");
printf("iterInorder: ");
iterInorder(tree);
printf("\n");
printf("LevelOrder: ");
LevelOrder(tree);
printf("\n");
system("pause");
return 0;
}