二叉树的创建、先中后序遍历及判断是否为满二叉树(递归与非递归算法)

[color=blue][b]//(递归的)[/b][/color]

#include “stdafx.h”

#include<stdio.h>

#include<stdlib.h>

//#include<math.h>

typedef struct node *BiTree;

struct node{

char ch;

BiTree Lchild,Rchild;

};

BiTree root=NULL;

//创建二叉树

BiTree create(BiTree bt)

{

char ch;

scanf(“%c”,&ch);

if(ch==’$’)

bt=NULL;

else{

bt=(BiTree)malloc(sizeof(node));

bt->ch=ch;

bt->Lchild=create(bt->Lchild);

bt->Rchild=create(bt->Rchild);

}

return bt;

}

//先序遍历二叉树

void PreOrder(BiTree bt)

{

if(bt){

printf(“%c”,bt->ch);

PreOrder(bt->Lchild);

PreOrder(bt->Rchild);

}

}

//中序遍历二叉树

void InOrder(BiTree bt)

{

if(bt){

InOrder(bt->Lchild);

printf(“%c”,bt->ch);

InOrder(bt->Rchild);

}

}

//后序遍历二叉树

void PostOrder(BiTree bt)

{

if(bt){

PostOrder(bt->Lchild);

PostOrder(bt->Rchild);

printf(“%c”,bt->ch);

}

}

//求m的n次方

int pow(int m,int n){

int i,dr=1;

for(i=0;i<n;i++){

dr*=m;

}

return dr;

}

//求结点总数

int NodeCount(BiTree bt){

int BiTreeNodeCount=0,btl,btr;

if(bt!=NULL){

BiTreeNodeCount++;

btl=NodeCount(bt->Lchild);

btr=NodeCount(bt->Rchild);

BiTreeNodeCount=btl+btr+1;

}

return BiTreeNodeCount;

}

//求二叉树深度

int TreeDepth(BiTree bt){

int depth=0,dl,dr;

if(bt!=NULL){

dl=TreeDepth(bt->Lchild);

dr=TreeDepth(bt->Rchild);

depth=dl>dr ? dl:dr;

return (depth+1);

}else{

return depth;

}

}

//判断该二叉树是否为满二叉树?

void IsFullBinaryTree(BiTree bt){

if(NodeCount(bt)==(pow(2,TreeDepth(bt))-1)){

printf(“此二叉树是满二叉树!\n”);

}else{

printf(“此二叉树不是满二叉树!\n”);

}

}

void main()

{

printf(“请输入你要构建的二叉树序列:形如(ABC$$DE$G$$F$$$,其中’$’代表空格符)\n”);

root=create(root);

printf(“前序遍历二叉树:\n”);

PreOrder(root);

printf(“\n”);

printf(“中序遍历二叉树:\n”);

InOrder(root);

printf(“\n”);

printf(“后序遍历二叉树:\n”);

PostOrder(root);

printf(“\n”);

IsFullBinaryTree(root);

}

[color=blue][b]//(非递归的)[/b][/color]

#include “stdafx.h”

#include<stdio.h>

#include<stdlib.h>

#define MAX_STACK_SIZE 100

typedef struct node *BiTree;

struct node{

char ch;

BiTree Lchild,Rchild;

};

BiTree root=NULL;

BiTree stack[MAX_STACK_SIZE];

int top=-1;

//入栈

void push(int *top,BiTree item)

{

if(*top>=MAX_STACK_SIZE-1)

printf(“此栈已满!\n”);

else

stack[++*top]=item;

}

//出栈

BiTree pop(int *top)

{

if(*top==-1)

return NULL;

return stack[(*top)–];

}

//创建二叉树

BiTree create(BiTree bt)

{

char ch;

scanf(“%c”,&ch);

if(ch==’$’)

bt=NULL;

else{

bt=(BiTree)malloc(sizeof(node));

bt->ch=ch;

bt->Lchild=create(bt->Lchild);

bt->Rchild=create(bt->Rchild);

}

return bt;

}

//先序遍历二叉树

void iter_PreOrder(BiTree bt)

{

for(;;){

for(;bt;bt=bt->Lchild){

printf(“%c”,bt->ch);

push(&top,bt);

}

bt=pop(&top);

if(!bt) break;

bt=bt->Rchild;

}

}

//中序遍历二叉树

void iter_InOrder(BiTree bt)

{

for(;;){

for(;bt;bt=bt->Lchild)

push(&top,bt);

bt=pop(&top);

if(!bt) break;

printf(“%c”,bt->ch);

bt=bt->Rchild;

}

}

//后序遍历二叉树

void iter_PostOrder(BiTree bt)

{

int flag[MAX_STACK_SIZE];

for(;;){

for(;bt&&bt->ch;bt=bt->Lchild){

push(&top,bt);

flag[top]=1;

}

bt=pop(&top);

if(!bt) break;

if(flag[top+1]){

push(&top,bt);

flag[top]=0;

}

else{

printf(“%c”,bt->ch);

bt->ch=NULL;

}

bt=bt->Rchild;

}

}

//求m的n次方

int pow(int m,int n){

int i,dr=1;

for(i=0;i<n;i++){

dr*=m;

}

return dr;

}

//求结点总数

int NodeCount(BiTree bt){

int BiTreeNodeCount=0,btl,btr;

if(bt!=NULL){

BiTreeNodeCount++;

btl=NodeCount(bt->Lchild);

btr=NodeCount(bt->Rchild);

BiTreeNodeCount=btl+btr+1;

}

return BiTreeNodeCount;

}

//求二叉树深度

int TreeDepth(BiTree bt){

int depth=0,dl,dr;

if(bt!=NULL){

dl=TreeDepth(bt->Lchild);

dr=TreeDepth(bt->Rchild);

depth=dl>dr ? dl:dr;

return (depth+1);

}else{

return depth;

}

}

//判断该二叉树是否为满二叉树?

void IsFullBinaryTree(BiTree bt){

if(NodeCount(bt)==(pow(2,TreeDepth(bt))-1)){

printf(“此二叉树是满二叉树!\n”);

}else{

printf(“此二叉树不是满二叉树!\n”);

}

}

void main()

{

printf(“请输入你要构建的二叉树序列:形如(ABC$$DE$G$$F$$$,其中’$’代表空格符)\n”);

root=create(root);

printf(“迭代的前序遍历二叉树:\n”);

iter_PreOrder(root);

printf(“\n”);

printf(“迭代的中序遍历二叉树:\n”);

iter_InOrder(root);

printf(“\n”);

printf(“迭代的后序遍历二叉树:\n”);

iter_PostOrder(root);

printf(“\n”);

IsFullBinaryTree(root);

}

    原文作者:满二叉树
    原文地址: https://blog.csdn.net/iyllyl/article/details/83762847
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞