⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.cpp

📁 约瑟夫环源代码,前中后序递归遍历二叉树
💻 CPP
字号:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define null 0
typedef struct node
{ char data;
  struct node *lchild;
  struct node *rchild;
}* bitree;

int setup(bitree &t)
{char ch=' ';
 scanf("%c",&ch);
 if(ch==' ')t=null;
 else{
	 if(!(t=(node *)malloc(sizeof(node))))return 0;
	 t->data=ch;          //建立根节点
	 printf("%c",t->data);
	 setup(t->lchild);//建立左子树
	 setup(t->rchild);//建立右子树
 }
 return 1;
}

int travl1(bitree t)//先序遍历的递归调用
{if(t)
 {printf("%c",t->data);//访问根节点
  if(t->lchild)travl1(t->lchild);//访问左子树
  if(t->rchild)travl1(t->rchild);//访问右子树
  return 1;
 }
else return 0;
return 1;
}


int travl2(bitree t)//中序遍历的递归调用
{if(t)
 {if(t->lchild!=null)travl2(t->lchild);//访问左子树
  printf("%c",t->data);//访问根节点
  if(t->rchild!=null)travl2(t->rchild);//访问右子树
  return 1;
 }
else return 0;
return 1;
}

int travl3(bitree t)//后序遍历的递归调用
{if(t)
 {if(t->lchild!=null)travl3(t->lchild);//访问左子树
  if(t->rchild!=null)travl3(t->rchild);//访问右子树
  printf("%c",t->data);//访问根节点
  return 1;
 }
else return 0;
return 1;
}


void main()
{bitree t=null;
 printf("输入数值:");
 printf("\n建树顺序为");
 setup(t);
 printf("\n先序遍历里结果为");
 travl1(t);
 printf("\n中序遍历结果为");
 travl2(t);
 printf("\n后序遍历结果为");
 travl3(t);
 printf("\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -