《数据结构与算法(C语言版)》严蔚敏 | 第五章 建立二叉树,并完成三/四种遍历算法
PS:所有的代码示例使用的都是这个图

2019-10-29
利用p126的算法5.3建立二叉树,并完成三种遍历算法 中序 后序 先序
#include<iostream>
#include<stack>
#define TElemType char
using namespace std;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild; }BiTNode,*BiTree;
//先序遍历的顺序建立二叉链表
void xCreateBiTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new BiTNode;
T->data=ch;
xCreateBiTree(T->lchild);
xCreateBiTree(T->rchild);
}
}
//先序遍历输出
void xPrintTree(BiTree T)
{ if(T)
{
cout<<T->data;
xPrintTree(T->lchild);
xPrintTree(T->rchild); }
else
{
return;
}
}
//中序遍历的顺序建立二叉链表
void zCreateBiTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new BiTNode;
zCreateBiTree(T->lchild);
T->data=ch;
zCreateBiTree(T->rchild);
}
}
//中序遍历输出
void zPrintTree(BiTree T)
{ if(T)
{
zPrintTree(T->lchild);
cout<<T->data;
zPrintTree(T->rchild);
}
else
{
return;
}
}
//后序遍历的顺序建立二叉链表
void hCreateBiTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new BiTNode;
hCreateBiTree(T->lchild);
hCreateBiTree(T->rchild);
T->data=ch;
}
}
//后序遍历输出
void hPrintTree(BiTree T)
{ if(T)
{
hPrintTree(T->lchild);
hPrintTree(T->rchild);
cout<<T->data;
}
else
{
return;
}
}
int main()
{
BiTree root;
// xCreateBiTree(root);
// xPrintTree(root);
/*
输入:ABC##DE#G##F###
输出:ABCDEGF
*/ // zCreateBiTree(root);
// zPrintTree(root);
/*
输入:ABC##DE#G##F###
输出:CBEGDFA
*/
hCreateBiTree(root);
hPrintTree(root);
/*
输入:ABC##DE#G##F###
输出:CGEFDBA
*/ return 0;
}
2019-11-04
利用p126的算法5.3建立二叉树,并完成四种遍历算法 中序 先序 后序 层次
输入:ABC##DE#G##F###
#include<iostream>
#include<stack>
#include<queue>
#define TElemType char
#define status int
#define OK 1
#define OVERFLOW 0
using namespace std;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//先序遍历输出
void xPrintTree(BiTree T)
{ if(T)
{
cout<<T->data;
xPrintTree(T->lchild);
xPrintTree(T->rchild); }
else
{
return;
}
}
//中序遍历输出
void zPrintTree(BiTree T)
{ if(T)
{
zPrintTree(T->lchild);
cout<<T->data;
zPrintTree(T->rchild);
}
else
{
return;
}
}
//后序遍历输出
void hPrintTree(BiTree T)
{ if(T)
{
hPrintTree(T->lchild);
hPrintTree(T->rchild);
cout<<T->data;
}
else
{
return;
}
}
//层次遍历输出
void LPrintTree(BiTree T)
{
queue<BiTree>q;
q.push(T);
while(!q.empty())
{
BiTree head=q.front();
q.pop();
cout<<head->data;
if(head->lchild)
{
q.push(head->lchild);
}
if(head->rchild)
{
q.push(head->rchild);
}
}
}
void CreateBiTree(BiTree &T)
{
TElemType ch;
cin>>ch;
if(ch=='#')
{
T=NULL;
}
else
{
T=new BiTNode;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int main()
{
BiTree root;
CreateBiTree(root);
cout<<"\n先序遍历输出"<<endl;
xPrintTree(root);
cout<<"\n中序遍历输出"<<endl;
zPrintTree(root);
cout<<"\n后序遍历输出"<<endl;
hPrintTree(root);
cout<<"\n层次遍历输出"<<endl;
LPrintTree(root);
return ;
}
设计四个算法,分别实现:
(1)统计二叉树中结点的个数。
(2)统计二叉树中叶子结点的个数
(3)统计二叉树中度为2的结点的个数
(4)统计二叉树中度为1的结点的个数
/**
#图P121(b)
#建立二叉树,并且用三种遍历法遍历他
#2019/11/4
输入:
ABC##DE#G##F###
输出:
这棵树的节点数:7
这棵树的叶子节点数:3
这棵树的度为2的结点的数:2
这棵树的度为1的结点的数:2
*/
#include<iostream>
#include<stack>
#define TElemType char
#define status int
#define OK 1
#define OVERFLOW 0
using namespace std;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild; }BiTNode,*BiTree;
//统计二叉树中结点的个数
int CountTreeNode(BiTree T,int &count)
{ if(T)
{
count++;
CountTreeNode(T->lchild,count);
CountTreeNode(T->rchild,count); }
else
{
return count;
}
}
//统计二叉树中叶子结点的个数
int CountTreeLeaf(BiTree T,int &count)
{ if(T)
{
if(!T->lchild&&!T->rchild)
count++;
CountTreeLeaf(T->lchild,count);
CountTreeLeaf(T->rchild,count);
}
else
{
return count;
}
}
//统计二叉树中度为2的结点的个数
int CountTreeDegreeTwo(BiTree T,int &count)
{ if(T)
{
if(T->lchild&&T->rchild)
count++;
CountTreeDegreeTwo(T->lchild,count);
CountTreeDegreeTwo(T->rchild,count);
}
else
{
return count;
}
}
//统计二叉树中度为1的结点的个数
int CountTreeDegreeOne(BiTree T,int &count)
{ if(T)
{
if((T->lchild&&!T->rchild)||(!T->lchild&&T->rchild))
count++;
CountTreeDegreeOne(T->lchild,count);
CountTreeDegreeOne(T->rchild,count);
}
else
{
return count;
}
}
void CreateBiTree(BiTree &T)
{
TElemType ch;
cin>>ch;
if(ch=='#')
{
T=NULL;
}
else
{
T=new BiTNode;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int main()
{
BiTree root;
CreateBiTree(root);
int count=0;
cout<<"这棵树的节点数:"<<CountTreeNode(root,count)<<endl;
count=0;
cout<<"这棵树的叶子节点数:"<<CountTreeLeaf(root,count)<<endl;
count=0;
cout<<"这棵树的度为2的结点的数:"<<CountTreeDegreeTwo(root,count)<<endl;
count=0;
cout<<"这棵树的度为1的结点的数:"<<CountTreeDegreeOne(root,count)<<endl; return 0;
}
《数据结构与算法(C语言版)》严蔚敏 | 第五章 建立二叉树,并完成三/四种遍历算法的更多相关文章
- 数据结构(C语言版)严蔚敏->排序
@ 目录 1. 插入排序 1.1 直接插入排序 1.2 折半插入排序 1.3 希尔排序(Shell Sort) 2.交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择排序 3.1 简单选择排序 ...
- 数据结构1:数据结构与算法C语言版分析概述
本节开始将带领大家系统地学习数据结构,作为一门计算机专业大二学生的必修课程,该课程面对的目标人群为初步具备基本编程能力和编程思想的程序员(大一接触了 C 语言或者 C++).通过系统地学习数据结构,可 ...
- php 冒泡 快速 选择 插入算法 四种基本算法
php四种基础算法:冒泡,选择,插入和快速排序法 来源:PHP100中文网 | 时间:2013-10-29 15:24:57 | 阅读数:120854 [导读] 许多人都说 算法是程序的核心,一个程序 ...
- PHP四种基础算法详解
许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一个初级phper,虽然很少接触到算法方面的东西 .但是对于冒泡排序,插入排序,选择排序,快速排序四种基本算法,我想还是要 ...
- php四种基础算法:冒泡,选择,插入和快速排序法
转自:http://www.php100.com/html/php/rumen/2013/1029/6333.html 许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一 ...
- 【C++】四种排序算法的时间比较
四种排序算法的时间比较 [注]clock函数对输入(用户输入)元素N排序的计时 #include<iostream> #include<time.h> using namesp ...
- 创建B树,动态添加节点,并使用三种遍历算法对树进行遍历
ks17:algorithm apple$ cat btree_test.c ///********************************************************** ...
- php四种基础算法:冒泡,选择,插入和快速排序法PHP基础教程
许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一个初级phper,虽然很少接触到算法方面的东西.但是对于冒泡排序,插入排序,选择排序,快速排序四种基本算法,我想还是要掌 ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
随机推荐
- Delphi中堆栈区别
http://blog.csdn.net/zang141588761/article/details/52838728 Delphi中堆栈区别 2016-10-17 14:49 277人阅读 评论( ...
- 使用 Spring HATEOAS 开发 REST 服务
使用 Spring HATEOAS 开发 REST 服务 学习博客:https://www.ibm.com/developerworks/cn/java/j-lo-SpringHATEOAS/ htt ...
- python基础-5.2装饰器
1.了解装饰器前准备 #### 第一波 #### def foo(): print 'foo' foo #表示是函数,仅指向了函数的地址,为执行 foo() #表示执行foo函数 #### 第二波 # ...
- CentOS7 修复MBR引导
为了达到实验目的,首先破坏MBR引导bootloader 重启系统发现系统进不去了,这正是我们想要的 重启进入系统救援模式,输入以下命令重建MBR引导bootloader 重启,可以正常引导进入系统
- 《剑指offer》面试题16 反转链表 Java版
(输入链表的头节点,反转链表) 书中方法:对于一个链表,我们只能从头往后遍历,如果要反转,我们需要更改当前节点的next域指向前一个节点,此时链表断开,为了能继续修改下一个节点的next域,我们还要维 ...
- Kafka DockerFile
FROM php:5.6.38-fpm COPY . /alidata/workerspace WORKDIR /alidata/workerspace RUN set -x && a ...
- MVC框架与MTC框架
3.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...
- Spark Streaming Transformations
map(func):对DStream中的所有的元素进行func转换生成新的DStream flatMap(func):和map方法类似,先对DStream中的元素进行func运算,然后压平,就是说,如 ...
- spark复习笔记(4):spark脚本分析
1.[start-all.sh] #!/usr/bin/env bash # # Licensed to the Apache Software Foundation (ASF) under one ...
- Int、bigint、smallint、tinyint的区别
Bigint:从-2^63-2^63的整型数据(所有数字).存储大小为8个字节.Bigint已经有长度了,在mysql建表中的length,只是用于显示的位数. Int:从-2^31-2^31的整型数 ...