C语言二叉树非递归遍历问题,判断二叉树是否为完全二叉树
分类:微服架构

#include<stdio.h>
typedef char Datatype;
typedef struct BiTNode {
    Datatype data;
    struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;

#include<stdio.h>
#include<stdlib.h>

C语言二叉树非递归遍历问题
#include"stdio.h"
#include"stdlib.h"

c语言 判断二叉树是否为完全二叉树 50C
新手渣渣 请教各位大神 问题出在哪 谢谢
#include
#include
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0

void CreateBiTree(BiTree *T)
   {
    char ch;
    scanf("%c",&ch);
    if(ch=='#')
        (*T)=NULL;
    else
       {
        (*T)=(BiTree)malloc(sizeof(BiTNode));
        if(!(*T))
            exit(0);
        (*T)->data=ch;
        CreateBiTree(&(*T)->lchild);
        CreateBiTree(&(*T)->rchild);
    }
}

int leafcount=0;

#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

typedef char TElemType;
typedef int Status;
typedef struct BiTNode{
TElemType data2;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

  void Print_bitree(BiTree *T,int Layer)
    {/**//*按竖向树状打印的二叉树*/
     int i;
     if(*T==NULL)
         return;
    Print_bitree(&(*T)->rchild,Layer+1);
     for(i=0;i<Layer;i++)    
         printf("  ");
     printf("%cn",(*T)->data);    /*按逆中序输出结点,用层深决定结点的左右位置*/
    Print_bitree(&(*T)->lchild,Layer+1);
}

typedef struct BiTNode{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

typedef int Status;
typedef struct LNode{
BiTree data;
struct LNode *next;
}LNode;
typedef struct{
LNode *top;
}LStack;

typedef struct QNode
{
BiTree data1;
struct QNode *next;
}QNode,*QueuePtr;

void main()
{
     int Layer;
      
     BiTNode* S;
       Layer=0;
  
     printf("Enter the char:n");
    CreateBiTree(&S);
     printf("Output the bitree:n");
     Print_bitree(&S,Layer);
}

int CreateBiTree(BiTree *T)/*根据先序序列建立二叉树的二叉链表*/
{ char ch;
    scanf("%c",&ch);
        if(ch=='-')
   {
   *T=NULL;
   }
        else{
            if(!(*T=(BiTree)malloc(sizeof(BiTNode)))) return 0;
            (*T)->data=ch;
            CreateBiTree(&((*T)->lchild));
            CreateBiTree(&((*T)->rchild));
        }
   return 1;
}

int main(){
Status CreateBiTree(BiTree &T);
Status Pop(LStack &S);
Status Init_Stack(LStack &S);
Status Push(LStack &S,BiTree T);
Status StackEmpty;
Status PreOrderTraverse;
void visit(TElemType data);

typedef struct
{
QueuePtr front; //队头指针
QueuePtr rear; //队尾指针
}LinkQueue;

int preOrder(BiTree T)/*先序遍历的递归算法*/

BiTree T;printf("创建树中...");if(CreateBiTree printf;PreOrderTraverse;return 0;

Status InitQueue(LinkQueue *Q); //关于队列的函数声明
Status EnQueue(LinkQueue *Q,BiTree e);
Status DeQueue(LinkQueue *Q,BiTree *e);
Status QueueEmpty(LinkQueue Q);

{
    if(T){
        printf("%c ",T->data);
   if (!T->lchild&&!T->rchild) leafcount+=1;
        preOrder(T->lchild);
        preOrder(T->rchild);
    }
    return 1;
}

}

Status InitBiTree(BiTree *T); //关于二叉树的函数声明
Status CreateBiTree(BiTree *T); //啥意思啊 那个defination
Status InOrderTraverse;
int Check;

int inOrder(BiTree T)/*中序遍历的递归算法*/
{
    if(T){
        inOrder(T->lchild);
        printf("%c ",T->data);
        inOrder(T->rchild);
    }
    return 1;
}

Status CreateBiTree(BiTree &T){
TElemType ch;
scanf("%c",&ch);
if
T=NULL;
else{
T=(BiTNode *)malloc(sizeof;
if
exit;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}

Status InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=malloc(sizeof;
if(!Q->front)
exit;
Q->front->next=NULL;
return OK;
}

int oldOrder(BiTree T)/*后序遍历的递归算法*/
{
    if(T){
        oldOrder(T->lchild);
        oldOrder(T->rchild);
        printf("%c ",T->data);
    }
    return 1;
}

Status Init_Stack(LStack &S){
LNode *p;
p=malloc(sizeof;
if
exit;
p->next=NULL;
S.top=p;
return OK;
}

Status EnQueue(LinkQueue *Q,BiTree e)
{
QNode *p;
p=malloc(sizeof;
if
exit;
p->data1->data2=e->data2;
p->data1->lchild=e->lchild;
p->data1->rchild=e->rchild;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}

void main()

Status Push(LStack &S,BiTree T){
LNode *p;
p=malloc(sizeof;
if
exit;
S.top->data = T;
p->next = S.top;
S.top = p;
return OK;
}

Status DeQueue(LinkQueue *Q,BiTree *e) {
if(Q->front==Q->rear)
return ERROR;
QNode *p;
p=Q->front->next;
->data2=p->data1->data2;
->lchild=p->data1->lchild;
->rchild=p->data1->rchild;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free;
return OK;
}

{   BiTree T;
    leafcount=0;
    printf("please enter abc--de-g--f---n");

Status StackEmpty{
if(S.top==NULL)
return 1;
else
return 0;
}

Status QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
return TRUE;
else
return FALSE;
}

    CreateBiTree(&T);

void visit(TElemType data){
printf("%cn",data);
}

Status InitBiTree(BiTree *T)
{
*T=NULL;
return OK;
}

    printf("xian xu bian li:");
    preOrder(T);

BiTree Pop(LStack &S){
BiTree tran;
LNode *t;
tran=S.top->data;
t=S.top;
S.top=S.top->next;
free;
return tran;
}

Status CreateBiTree(BiTree *T)
{
char ch;
scanf("%c",&ch);
if
{
*T=NULL;
}
else
{
*T=malloc(sizeof;
->data2=ch;
CreateBiTree->lchild);
CreateBiTree->rchild);
}
return OK;
}

    printf("nzhong xu bian li:");
    inOrder(T);

Status PreOrderTraverse{
LStack S;
Init_Stack;
BiTree p;
p=T;
while(p||!(StackEmpty{
if{
Push;
p=p->lchild;
}
else{
p=Pop;
visit(p->data);
p=p->rchild;
}
}
return OK;
}

Status InOrderTraverse
{
if
return 0;
else
{
InOrderTraverse(T->lchild);
printf("%c",T->data2);
InOrderTraverse(T->rchild);
}
return OK;
}

    printf("nhou xu bian li:");
    oldOrder(T);

//源代码如上,程序运行,我输入ABC DE G F

int Check
{
BiTree S;
LinkQueue Q;
int flag=0;
if
{
InitQueue;
EnQueue;
while(!(QueueEmpty
{
DeQueue(&Q,&S);
if
{
flag=1;
}

    printf("nye zi jie dian:%d n",leafcount);
    system("pause");

建立二叉树那一段可以运行,到了二叉树遍历的时候程序无法运行自动关闭,麻烦各位了!

else
{
if
return 0;
else
{
EnQueue(&Q,(S->lchild));
EnQueue(&Q,(S->rchild));
}
}
}
}
printf;
return 1;

}

}

int main()
{

BiTree T;
int n;
InitBiTree;
CreateBiTree;
printf("n中序遍历二叉树:");
InOrderTraverse;
n=Check;
printf("判断是否为完全二叉树 1-是 0-否:%dn",n);
return 0;
}

本文由10bet手机官网发布于微服架构,转载请注明出处:C语言二叉树非递归遍历问题,判断二叉树是否为完全二叉树

上一篇:快乐其实很简单,时尚简约风格够味儿 下一篇:没有了
猜你喜欢
热门排行
精彩图文