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语言版)》严蔚敏 | 第五章 建立二叉树,并完成三/四种遍历算法的更多相关文章

  1. 数据结构(C语言版)严蔚敏->排序

    @ 目录 1. 插入排序 1.1 直接插入排序 1.2 折半插入排序 1.3 希尔排序(Shell Sort) 2.交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择排序 3.1 简单选择排序 ...

  2. 数据结构1:数据结构与算法C语言版分析概述

    本节开始将带领大家系统地学习数据结构,作为一门计算机专业大二学生的必修课程,该课程面对的目标人群为初步具备基本编程能力和编程思想的程序员(大一接触了 C 语言或者 C++).通过系统地学习数据结构,可 ...

  3. php 冒泡 快速 选择 插入算法 四种基本算法

    php四种基础算法:冒泡,选择,插入和快速排序法 来源:PHP100中文网 | 时间:2013-10-29 15:24:57 | 阅读数:120854 [导读] 许多人都说 算法是程序的核心,一个程序 ...

  4. PHP四种基础算法详解

    许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一个初级phper,虽然很少接触到算法方面的东西 .但是对于冒泡排序,插入排序,选择排序,快速排序四种基本算法,我想还是要 ...

  5. php四种基础算法:冒泡,选择,插入和快速排序法

    转自:http://www.php100.com/html/php/rumen/2013/1029/6333.html 许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一 ...

  6. 【C++】四种排序算法的时间比较

    四种排序算法的时间比较 [注]clock函数对输入(用户输入)元素N排序的计时 #include<iostream> #include<time.h> using namesp ...

  7. 创建B树,动态添加节点,并使用三种遍历算法对树进行遍历

    ks17:algorithm apple$ cat btree_test.c ///********************************************************** ...

  8. php四种基础算法:冒泡,选择,插入和快速排序法PHP基础教程

    许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一个初级phper,虽然很少接触到算法方面的东西.但是对于冒泡排序,插入排序,选择排序,快速排序四种基本算法,我想还是要掌 ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. 分页查询 pagecount recordcount pagesize

    pagecount=(recordcount+pagesize-1)/pagesize

  2. Mac基本配置

    相关操作 配置文件 java 下载jdk-12.0.1_osx-x64_bin.dmg 配置环境变量 #配置java JAVA_HOME=/Library/Java/JavaVirtualMachin ...

  3. 【openstf】自己的云测平台——mac安装openstf

    openstf的github地址:https://github.com/openstf/stf 上图可以清晰看出openstf的使用场景和效果   openstf是一个web应用程序,用于远程调试智能 ...

  4. Bootstrap 学习笔记 项目实战 响应式轮播图

    左右两个箭头可以随浏览器缩放进行移动 保持在图片中间 Html代码: <!DOCTYPE html> <html lang="zh-cn"> <hea ...

  5. 事件 on emit off 封装

    /* on 绑定 emit 触发 off 解绑 //存放事件 eventList = { key:val handle:[] } 1对多 on(eventName,callback); handle: ...

  6. 20190908 On Java8 第十九章 类型信息

    第十九章 类型信息 RTTI(RunTime Type Information,运行时类型信息)能够在程序运行时发现和使用类型信息. Java 主要有两种方式在运行时识别对象和类信息: "传 ...

  7. spring-security问题记录

    一.错误信息 Could not decode JSON for additional information: BaseClientDetails 2019-12-03 22:18:37.239 W ...

  8. Python 爬虫 校花网

    爬虫:是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. 福利来了  校花网 ,首先说为什么要爬这个网站呢,第一这个网站简单爬起来容易,不会受到打击,第二呢 你懂得.... 1.第一步,需要下 ...

  9. KNN算法项目实战——改进约会网站的配对效果

    KNN项目实战——改进约会网站的配对效果 1.项目背景: 海伦女士一直使用在线约会网站寻找适合自己的约会对象.尽管约会网站会推荐不同的人选,但她并不是喜欢每一个人.经过一番总结,她发现自己交往过的人可 ...

  10. 部署Tomcat服务器

    部署Tomcat服务器,具体内容如下: 1.安装部署JDK基础环境; 2.安装部署Tomcat服务器; 3.创建JSP测试页面,文件名为test.jsp,显示服务器当前时间. 然后客户机访问Web服务 ...