二叉树(作业)
2009-11-23 14:23
127 查看
1、掌握二叉树的存储方法;
2、掌握二叉树的基本算法。
二:实验内容和要求:
1、编写函数,创建一棵二叉树;
2、编写函数,用递归算法分别求二叉树的各种遍历序列;
三:源代码
一、实验目的:
2、掌握二叉树的基本算法。
二:实验内容和要求:
1、编写函数,创建一棵二叉树;
2、编写函数,用递归算法分别求二叉树的各种遍历序列;
三:源代码
]#include <stdio.h> #include <malloc.h> typedef struct BiTree { char data; struct BiTree *LChild; struct BiTree *RChild; }BiTree; BiTree * CreateBiTree(); //创建二叉树 void PrintTree(BiTree* bt,int nLayer) ; //输出二叉树 void PreOrder(BiTree *root); //先序遍历 void InOrder(BiTree *root) ; //中序遍历 void PostOrder(BiTree *root); //后序遍历 BiTree * CreateBiTree() //创建二叉树 { BiTree *bt; char ch; scanf("%c",&ch); if(ch=='.') { bt=NULL; } else { bt=(BiTree*)malloc(sizeof(BiTree)); bt->data=ch; bt->LChild=CreateBiTree(); bt->RChild=CreateBiTree(); } return bt; } void PrintTree(BiTree* bt,int nLayer) //输出二叉树 { if(bt == NULL) return; PrintTree(bt->RChild,nLayer+1); for(int i=0;i<nLayer;i++) printf(" "); printf("%c\n",bt->data); PrintTree(bt->LChild,nLayer+1); } void PreOrder(BiTree *root) //先序遍历 { if (root!=NULL) { printf("%3c",root ->data); PreOrder(root ->LChild); PreOrder(root ->RChild); } } void InOrder(BiTree *root) //中序遍历 { if (root!=NULL) { InOrder(root ->LChild); printf("%3c",root ->data); InOrder(root ->RChild); } } void PostOrder(BiTree *root) //后序遍历 { if(root!=NULL) { PostOrder(root ->LChild); PostOrder(root ->RChild); printf("%3c",root ->data); } } void main() { BiTree *bt; bt=CreateBiTree(); PrintTree(bt,1); PreOrder(bt); printf("\n"); InOrder(bt); printf("\n"); PostOrder(bt); printf("\n"); }
一、实验目的:
相关文章推荐
- 二叉树(上机作业版)
- 数据结构第四次作业(二叉树的基本操作实现)
- 软基作业——先序遍历法生成二叉树
- C++作业:将左右孩子互换 / 复制一棵二叉树
- 二叉树——数据结构课堂作业
- 数据结构作业——brothers(二叉树)
- 数据结构OJ作业 二叉树
- 数据结构作业——brothers(二叉树)
- 软基作业:二叉树的创建与遍历
- THU数据结构编程作业一:真二叉树重构(Proper Rebuild)
- 【二叉树系列】二叉树课程大作业
- 华南理工数据结构大作业第二题 二叉树各种操作深度结点个数后序前序中序层次求祖先
- 定时作业-二叉树的建立与遍历
- 【二叉树系列】二叉树课程大作业
- 【数据结构作业四】以二叉链表作存储结构,建立一棵二叉树,并输出该二叉树的先序、中序、后序遍历序列、高度和其叶子结点数。
- Java面试作业,给一个整型数组,要求算出最多相同value的sum,类似二叉树搜索;
- 数据结构作业:利用中序遍历和后序遍历构建二叉树(RMQ转LCA)
- 上机作业之树:递归算法在二叉树中的简单应用
- 习题课第一次作业:2、重建二叉树
- 【二叉树系列】二叉树课程大作业