二叉树和链表相似,只是后节点变成了左右节点,重要的是递归思想的理解和返回时候的层级结构

1.满二叉树的穿件及前中后序遍历

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

typedef struct node{
int date;
struct node * lchild;
struct node * rchild;
}btreenode;

btreenode * btree_create(int a,int b){
btreenode * t = NULL;
t = (btreenode*)malloc(sizeof(btreenode));
t->date = b;
if(2*b<=a){
t->lchild = btree_create(a,2*b);
}
else{
t->lchild = NULL;
}
if(2*b+1<=a){
t->rchild = btree_create(a,2*b+1);
}
else{
t->rchild = NULL;
}
return t;
}

void first_show(btreenode* root){

printf("%d ",root->date);
if(root->lchild != NULL){
first_show(root->lchild);
}
if(root->rchild != NULL){
first_show(root->rchild);
}
}

void middle_show(btreenode* root){

if(root->lchild != NULL){
middle_show(root->lchild);
}
printf("%d ",root->date);
if(root->rchild != NULL){
middle_show(root->rchild);
}
}

void last_show(btreenode* root){

if(root->lchild != NULL){
last_show(root->lchild);
}
if(root->rchild != NULL){
last_show(root->rchild);
}
printf("%d ",root->date);
}

int main(int argc, const char *argv[])
{
btreenode * root = btree_create(10,1);
first_show(root);
printf("\n");
middle_show(root);
printf("\n");
last_show(root);
printf("\n");
return 0;
}

2.不完全二叉树的创建(sancf和getchar方法均会产生垃圾字符,输入时候需要区分)

typedef struct node{
int date;
struct node * lchild;
struct node * rchild;
}btreenode;

btreenode * btree_create(){
btreenode * t = NULL;
t = (btreenode*)malloc(sizeof(btreenode));
char i = getchar();
getchar();
t->date = i;

if(i == '#'){
return NULL;
}
t->lchild = btree_create();
t->rchild = btree_create();
return t;
}

void first_show(btreenode* root){

printf("%c ",root->date);
if(root->lchild != NULL){
first_show(root->lchild);
}
if(root->rchild != NULL){
first_show(root->rchild);
}
}

void middle_show(btreenode* root){

if(root->lchild != NULL){
middle_show(root->lchild);
}
printf("%c ",root->date);
if(root->rchild != NULL){
middle_show(root->rchild);
}
}

void last_show(btreenode* root){

if(root->lchild != NULL){
last_show(root->lchild);
}
if(root->rchild != NULL){
last_show(root->rchild);
}
printf("%c ",root->date);
}

int main(int argc, const char *argv[])
{
btreenode * root = btree_create();
first_show(root);
printf("\n");
middle_show(root);
printf("\n");
last_show(root);
printf("\n");
return 0;
}

3、二叉树的层级遍历(使用进队出队思想,最好是进队即出队,别想着全部进队后出队,对比下前者还是节省内存的)

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

typedef struct node{
int date;
struct node * lchild;
struct node * rchild;
}btreenode;

typedef struct linklist{
btreenode * node1;
struct linklist * next;
}linklist;

linklist * linklist_create(){
linklist* ln =NULL;
ln = (linklist*)malloc(sizeof(linklist));
ln->next = NULL;
return ln;
}

void linklist_show(linklist * ln){
while(ln->next != NULL){
printf("%d ",ln->next->node1->date);
ln = ln->next;
}
printf("\n");
}
int linklist_in(linklist* ln,btreenode * btree){
if(btree == NULL) {return -1;}
linklist* t = ln;
linklist* temp = linklist_create();
temp->node1 = btree;
while(t->next != NULL){t = t->next;}
t->next = temp;
temp->next = NULL;
return 0;
}
int linklist_out(linklist * ln){
if(ln->next == NULL){return -1;}

linklist *temp = ln->next;
ln->next = temp->next;

int value = temp->node1->date;
// printf("%d ",temp->node1->date);
free(temp);
temp =NULL;
return value;
}

btreenode * btree_create(int a,int b){
btreenode * t = NULL;
t = (btreenode*)malloc(sizeof(btreenode));
t->date = b;
if(2*b<=a){
t->lchild = btree_create(a,2*b);
}
else{
t->lchild = NULL;
}
if(2*b+1<=a){
t->rchild = btree_create(a,2*b+1);
}
else{
t->rchild = NULL;
}
return t;
}

void btree_tier_show(linklist* ln,btreenode* root){

// linklist* t = ln;
linklist_in(ln,root);
// printf("%d ",linklist_out(ln));
while(ln->next != NULL){
if(ln->next->node1->lchild != NULL)
linklist_in(ln,ln->next->node1->lchild);
if(ln->next->node1->lchild != NULL)
linklist_in(ln,ln->next->node1->rchild);
printf("%d ",linklist_out(ln));
}
printf("\n");
}

void first_show(btreenode* root){

printf("%d ",root->date);
if(root->lchild != NULL){
first_show(root->lchild);
}
if(root->rchild != NULL){
first_show(root->rchild);
}
}

int main(int argc, const char *argv[])
{
btreenode * root = btree_create(20,1);
first_show(root);
printf("\n---------------------\n");
linklist *ln = linklist_create();
btree_tier_show(ln,root);
return 0;
}

转载于:https://www.cnblogs.com/huiji12321/p/11246752.html

step3 . day6数据结构之非线性表 满二叉树和不完全二叉树相关推荐

  1. 数据结构总结---------非线性表(多叉平衡树)

    数据结构总结---------非线性表(多叉平衡树) 1.为什么会出现多叉平衡树? 2.常见的多叉平衡树 2.1 B树 1.图解 2.简介 2.2 B+树 1.图解 2.简介 2.3 B*树 1.图解 ...

  2. step3 . day2 数据结构之线性表链表

    今天继续学习数据结构的线性表部分,从基础的顺序表到链表,类比写了一些调用函数,完成了表的增删改查排序等问题. 尤其是链表的排序,费了很大的心思终于捋顺写出来了,小有成就感,而且代码一次通过率越来越高, ...

  3. step3 . day1 数据结构之线性表顺序表

    大学没有选修数据结构,只是在C语言书最后提到过几种数据的 组织形式,也算眼熟,今天学的顺序表感觉还是很容易理解,写了一个有史以来代码最长.调试时间最短的代码,甚是感觉提高了不少,贴上Mark一下,写注 ...

  4. step3 . day5 数据结构之线性表 栈和队的应用-球钟问题

    球钟问题: 球钟是一个利用球的移动来记录时间的简单装置.它有三个可以容纳若干个球的指示器:分钟指示器,五分钟指示器,小时指示器.若分钟指示器中有2个球,5分钟指示器中有6个球,小时指示器中有5个球,则 ...

  5. step3 . day4 数据结构之线性表 栈和队

    补充一下:循环链表初学可能不好理解,除了多画图以外,把循环链表想象成无限的单向(或者双向)链表,每一个元素都是中间元素,就更好理解了. 1.栈和队是线性表的两种特殊管理逻辑,两者都是线性表 2.栈的原 ...

  6. step3 . day3 数据结构之线性表 单项循环链表和双向循环链表

    1. 使用单项循环链表解决约瑟夫问题: #include <stdio.h> #include <stdlib.h> typedef struct looplist{ int ...

  7. 数据结构与算法(八)-二叉树(斜二叉树、满二叉树、完全二叉树、线索二叉树)...

    前言:前面了解了树的概念和基本的存储结构类型及树的分类,而在树中应用最广泛的种类是二叉树 一.简介 在树型结构中,如果每个父节点只有两个子节点,那么这样的树被称为二叉树(Binary tree).其中 ...

  8. 数据结构:满二叉树,完全二叉树,非完全二叉树 的区别

    数据结构:满二叉树,完全二叉树,非完全二叉树 的区别 前言 一.满二叉树 二.完全二叉树 三.非完全二叉树 总结 版权声明 前言 记录下满二叉树,完全二叉树,非完全二叉树的区别 一.满二叉树 如上图所 ...

  9. 【数据结构】树 二叉树 满二叉树 完全二叉树初步理解

    文章目录 树的相关基本术语 树的表示形式 树结构典型应用场景 二叉树基本概念 满二叉树概念及其性质 完全二叉树的概念和性质 一道完全二叉树的面试题 树的相关基本术语 节点的度:一个节点含有的子树的个数 ...

最新文章

  1. linux使进程不依赖终端,Linux nohup命令应用简介--让Linux的进程不受终端影响
  2. 破坏计算机信息系统功能罪,破坏计算机信息系统罪
  3. fork()函数_UNIX环境高级编程(APUE)系列学习第8章-2 exit系列函数与wait系列函数...
  4. 必 备 习 题 集 (五)
  5. OpenCV2 图像处理与计算机视觉(一)—— 去除一幅二值化图像中的椒盐噪声
  6. 网上五花八门的单片机教程,到底应该怎么整理学习过程
  7. java实现文字跑马灯_跑马灯的问题
  8. Struts2学习笔记(4)-ActionSupport类及Action接口详解
  9. 腾讯手机管家android版,腾讯手机管家上线Android8.11.0版本
  10. python的方向_Python有哪些就业方向
  11. 傲腾optane介绍
  12. cocos2d-x初步
  13. 带你从头到尾梳理大图片加载OOM处理问题
  14. MATLAB-APP编程
  15. 转载的 C#.NET面试题
  16. 戴尔R710服务器修改风扇转速,dell 服务器手动降低风扇转速,解决dell风扇转速高,噪音大问题...
  17. n*n数组某位置元素关于主对角线对称点的坐标
  18. electron的使用
  19. 计算机被限制压缩包打不开怎么办,压缩文件打不开,教您压缩文件打不开怎么办解决...
  20. 基于Qt的海康威视网络摄像头SDK的二次开发——摄像头登录和预览

热门文章

  1. mysql 12142_php连接mysql的类mysql.class.php
  2. canoco5冗余分析步骤_Python怎么学习才能效果最好?网友:学习步骤在这了
  3. Cluster模式潜在问题及解决方案、Web服务综合解决方案
  4. Spark(1)——spark基本原理与启动
  5. linux libvpx编译安装,linux编译安装时常见错误解决办法
  6. 《Linux内核分析》课程总结
  7. 最小覆盖子串_滑动窗口
  8. 二叉树的前中后序遍历之迭代法(非统一风格迭代方式)
  9. 记录之pytorch中文文档
  10. C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解