一、给出按照 先序遍历序列 创建二叉树的例子 且 使用顺序栈和循环队列
1.代码如下©:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100005
typedef char DataType;
typedef struct Node {
DataType data;
struct Node * LChild;
struct Node * RChild;
}BiTNode,*BiTree;
typedef struct stackElem {
BiTNode node;
int flag;
}ElemType;
typedef struct stack{
ElemType elem[MAXSIZE];
int top;
}SeqStack;
typedef BiTree QElemType;
typedef struct queue{
QElemType elem[MAXSIZE];
int front;
int rear;
}SeqQueue;
BiTNode* CreateBiTree(BiTNode* root);
void PreOrder(BiTree root);
void InOrder(BiTree root);
void PostOrder(BiTree root);
void PostOrder1(BiTree root);
void PreOrder1(BiTree root);
void InOrder1(BiTree root);
void LevelOrder(BiTree root);
int NonLeafNode(BiTree root);
int BiTreeDeepth(BiTree root);
SeqStack* InitStack(void);
int Empty(SeqStack* s);
int Push(SeqStack* s,ElemType x);
int Pop(SeqStack *s);
ElemType* GetTop(SeqStack* s);
SeqQueue* InitQueue(void);
int IsQueueEmpty(SeqQueue* q);
int IsQueueFull(SeqQueue* q);
int EnterQueue(SeqQueue* q,QElemType x);
int DelQueue(SeqQueue* q,QElemType* x);
int main()
{
BiTNode * root = CreateBiTree(root);
printf("递归遍历:\n");
PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
printf("非递归遍历:\n");
PreOrder1(root);
printf("\n");
InOrder1(root);
printf("\n");
PostOrder1(root);
printf("\n");
printf("%d\n",NonLeafNode(root));
printf("%d\n",BiTreeDeepth(root));
printf("层次遍历:\n");
LevelOrder(root);
printf("\n");
return 0;
}
BiTNode* CreateBiTree(BiTNode* root){
char ch;
ch = getchar();
if(ch == '#'){
return NULL;
}else{
root = (BiTNode*)malloc(sizeof(BiTNode));
root->data = ch;
root->LChild = CreateBiTree(root->LChild);
root->RChild = CreateBiTree(root->RChild);
}
return root;
}
SeqStack* InitStack(void){
SeqStack* s;
s= (SeqStack*)malloc(sizeof(SeqStack));
s->top = -1;
return s;
}
int Empty(SeqStack* s){
if(s->top == -1){
return 1;
}else{
return 0;
}
}
int Push(SeqStack* s,ElemType x){
if(s->top == MAXSIZE - 1){
return 0;
}else{
s->top++;
s->elem[s->top] = x;
return 1;
}
}
int Pop(SeqStack* s){
if(Empty(s)){
return 0;
}else{
s->top--;
return 1;
}
}
ElemType* GetTop(SeqStack* s){
if(Empty(s)){
return NULL;
}else{
return &(s->elem[s->top]);
}
}
void PreOrder(BiTree root){
if(root){
printf("%c",root->data);
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
void InOrder(BiTree root){
if(root){
InOrder(root->LChild);
printf("%c",root->data);
InOrder(root->RChild);
}
}
void PostOrder(BiTree root){
if(root){
PostOrder(root->LChild);
PostOrder(root->RChild);
printf("%c",root->data);
}
}
void PreOrder1(BiTree root){
SeqStack* s = InitStack();
while(root || !Empty(s)){
while(root){
ElemType temp;
printf("%c",root->data);
temp.node = *root;
Push(s,temp);
root = root->LChild;
}
if(!Empty(s)){
root=GetTop(s)->node.RChild;
Pop(s);
}
}
}
void InOrder1(BiTree root){
SeqStack* s = InitStack();
while(root || !Empty(s)){
while(root){
ElemType temp;
temp.node = * root;
Push(s,temp);
root = root->LChild;
}
if(!Empty(s)){
printf("%c",GetTop(s)->node.data);
root = GetTop(s)->node.RChild;
Pop(s);
}
}
}
void PostOrder1(BiTree root){
SeqStack* s = InitStack();
while(root || !Empty(s)){
while(root){
ElemType temp;
temp.node = * root;
temp.flag = 0;
Push(s,temp);
root = root->LChild;
}
while(!Empty(s)&& GetTop(s)->flag==1){
printf("%c",GetTop(s)->node.data);
Pop(s);
}
if(!Empty(s)){
root = GetTop(s)->node.RChild;
GetTop(s)->flag = 1;
}
}
}
int NonLeafNode(BiTree root){
if(!root){
return 0;
}else if(!root->LChild && !root->RChild){
return 0;
}else{
return NonLeafNode(root->LChild) + NonLeafNode(root->RChild) + 1;
}
}
int BiTreeDeepth(BiTree root){
if(!root){
return 0;
}else{
if(BiTreeDeepth(root->LChild) > BiTreeDeepth(root->RChild)){
return BiTreeDeepth(root->LChild) +1 ;
}else{
return BiTreeDeepth(root->RChild) +1 ;
}
}
}
SeqQueue* InitQueue(void){
SeqQueue* q;
q= (SeqQueue*)malloc(sizeof(SeqQueue));
q->front = 0;
q->rear = 0;
return q;
}
int IsQueueEmpty(SeqQueue* q){
if(q->front == q->rear){
return 1;
}else{
return 0;
}
}
int IsQueueFull(SeqQueue* q){
if(q->rear - q->front - MAXSIZE){
return 0;
}else{
return 1;
}
}
int EnterQueue(SeqQueue* q,QElemType x){
if(IsQueueFull(q)){
return 0;
}
q->rear = (q->rear + 1)% MAXSIZE;
q->elem[q->rear] = x;
return 1;
}
int DelQueue(SeqQueue* q,QElemType* x){
if(IsQueueEmpty(q)){
return 0;
}else{
q->front=(q->front+1)%MAXSIZE;
*x = q->elem[q->front];
return 1;
}
}
void LevelOrder(BiTree root){
SeqQueue* q;
q = InitQueue();
QElemType p;
EnterQueue(q,root);
while(!IsQueueEmpty(q)){
DelQueue(q,&p);
printf("%c",p->data);
if(p->LChild){
EnterQueue(q,p->LChild);
}
if(p->RChild){
EnterQueue(q,p->RChild);
}
}
}