#include "StdAfx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct TreeNode
{
char data;
struct TreeNode *left;
struct TreeNode *right;
};
typedef TreeNode *BinTree;
struct Node
{
BinTree data;
struct Node *next;
};
typedef struct Node *PNode;
struct Queue
{
PNode f;
PNode r;
};
typedef struct Queue *LinkQueue;
LinkQueue SetNullQueue()
{
LinkQueue lqueue;
lqueue=(LinkQueue)malloc(sizeof(struct Queue));
if(lqueue!=NULL)
{
lqueue->f=NULL;
lqueue->r=NULL;
}
else
printf("alloc failure!");
return lqueue;
}
int IsNullQueue(LinkQueue lqueue)
{
return(lqueue->f==NULL);
}
void InQueue(LinkQueue lqueue,BinTree x)
{
PNode p;
p=(PNode)malloc(sizeof(struct Node));
if(p!=NULL)
{
p->data=x;
p->next=NULL;
if(IsNullQueue(lqueue))
{
lqueue->f=p;
lqueue->r=p;
}
else
{
lqueue->r->next=p;
lqueue->r=p;
}
}
else
printf("alloc failure!");
}
void OutQueue(LinkQueue lqueue)
{
PNode p;
if(IsNullQueue(lqueue))
printf("empty!");
else
{
p=lqueue->f;
lqueue->f=lqueue->f->next;
free(p);
}
}
BinTree FrontQueue(LinkQueue lqueue)
{
if(lqueue->f==NULL)
{
printf("empty!");
return 0;
}
else
return (lqueue->f->data);
}
typedef int DataType;
typedef struct Node *top;
typedef struct Node *LinkStack;
LinkStack SetNullStack()
{
LinkStack top=(LinkStack)malloc(sizeof(struct Node));
if(top!=NULL)
top->next =NULL;
else
printf("alloc failure");
return top;
}
int IsNullStack(LinkStack top)
{
return(top->next == NULL);
}
void Push(LinkStack top,BinTree x)
{
PNode p=(PNode)malloc(sizeof(struct Node));
if(p!=NULL)
{
p->data =x;
p->next =top->next ;
top->next =p;
}
else
printf("alloc failure!\n");
}
void Pop(LinkStack top)
{
PNode p;
if(IsNullStack(top))
printf("empty!\n");
else
{
p=top->next ;
top->next =p->next ;
free(p);
}
}
BinTree top_seq(LinkStack top)
{
if(IsNullStack(top))
printf("empty!\n");
else
return top->next ->data ;
}
BinTree create1()
{
BinTree bt;
char ch;
scanf(" %c",&ch);
if(ch=='@')
bt=NULL;
else
{
bt=(BinTree)malloc(sizeof(struct TreeNode));
bt->data=ch;
bt->left=create1();
bt->right=create1();
}
return bt;
}
BinTree create2()
{
LinkQueue queue=SetNullQueue();
BinTree s,p,bt;
int count=-1;
char ch;
scanf(" %c",&ch);
bt=NULL;
while(ch!='#')
{
if(ch=='@')
{
s=NULL;
count++;
}
else
{
s=(BinTree)malloc(sizeof(struct TreeNode));
s->data=ch;
s->left=NULL;
s->right=NULL;
count++;
}
InQueue(queue,s);
if(count==0)
bt=s;
else
{
p=FrontQueue(queue);
if(s!=NULL && p!=NULL)
if(count%2==1)
p->left=s;
else
p->right=s;
if(count%2==0)
OutQueue(queue);
}
scanf(" %c",&ch);
}
return bt;
}
void preOrder1(BinTree bt)
{
if(bt)
{
printf("%c",bt->data);
preOrder1(bt->left);
preOrder1(bt->right);
}
}
void preOrder2(BinTree bt)
{
LinkStack lstack=SetNullStack();
BinTree p=bt;
if(bt)
Push(lstack,bt);
while(!IsNullStack(lstack))
{
p=top_seq(lstack);
Pop(lstack);
while(p)
{
printf("%c",p->data);
if(p->right)
Push(lstack,p->right);
p=p->left;
}
}
}
void inOrder1(BinTree bt)
{
if(bt)
{
inOrder1(bt->left);
printf("%c",bt->data);
inOrder1(bt->right);
}
}
void inOrder2(BinTree bt)
{
LinkStack lstack=SetNullStack();
BinTree p=bt;
if(bt)
Push(lstack,bt);
p=p->left;
while(p||!IsNullStack(lstack))
{
while(p)
{
Push(lstack,p);
p=p->left;
}
p=top_seq(lstack);
Pop(lstack);
printf("%c",p->data);
p=p->right;
}
}
void postOrder1(BinTree bt)
{
if(bt)
{
postOrder1(bt->left);
postOrder1(bt->right);
printf("%c",bt->data);
}
}
void postOrder2(BinTree bt)
{
LinkStack lstack=SetNullStack();
BinTree p=bt;
while(p||!IsNullStack(lstack))
{
while(p)
{
Push(lstack,p);
p=p->left?p->left:p->right;
}
p=top_seq(lstack);
Pop(lstack);
printf("%c",p->data);
if(!IsNullStack(lstack)&&(top_seq(lstack)->left==p))
p=top_seq(lstack)->right;
else
p=NULL;
}
}
void levelOrder(BinTree bt)
{
BinTree p;
LinkQueue queue=SetNullQueue();
if(bt)
{
p=bt;
InQueue(queue,bt);
while(!IsNullQueue(queue))
{
p=FrontQueue(queue);
OutQueue(queue);
printf("%c",p->data);
if(p->left!=NULL)
InQueue(queue,p->left);
if(p->right!=NULL)
InQueue(queue,p->right);
}
}
}
int LeafNode(BinTree bt)
{
if(bt==NULL)
return 0;
else
{
if(bt->left==NULL && bt->right==NULL)
return 1;
else
return( LeafNode(bt->left) + LeafNode(bt->right) );
}
}
int deep(BinTree bt)
{
if(bt==NULL)
return 0;
else
{
int i=deep(bt->left);
int j=deep(bt->right);
int max=i>j?i:j;
return max+1;
}
}
BinTree copy(BinTree bt1)
{
BinTree bt2;
if(bt1)
{
bt2=(BinTree)malloc(sizeof(struct TreeNode));
if(bt2)
{
bt2->left=copy(bt1->left);
bt2->right=copy(bt1->right);
bt2->data=bt1->data;
return bt2;
}
else
printf("alloc failure");
}
else
return 0;
}
int main()
{
BinTree bt1=create1();
preOrder1(bt1);
printf("\n");
preOrder2(bt1);
printf("\n");
inOrder1(bt1);
printf("\n");
inOrder2(bt1);
printf("\n");
postOrder1(bt1);
printf("\n");
postOrder2(bt1);
printf("\n");
levelOrder(bt1);
printf("\n");
int i=LeafNode(bt1);
printf(" %d",i);
printf("\n");
int j=deep(bt1);
printf(" %d",j);
printf("\n");
BinTree bt2=copy(bt1);
preOrder1(bt2);
printf("\n");
BinTree bt3=create2();
preOrder1(bt3);
printf("\n");
system("pause");
}