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. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...

  2. django基于odm,简单的post和get封装

  3. Arduino的小灯亮起来~~~

    呵呵呵~~~昨天宝宝,就守着板子,跟说明书,心里默念,怎么特么还不亮?这个 东西怎么还不出!?~ 我特么没插线,可不不出么... 然后找那跟儿蓝色(我这个是蓝色的,不知道 有没有别的颜色的)的带USB ...

  4. 19c的 rac在oracle linux7.4上搭建总结

    准备: 1,ASM磁盘空间最低要求OCR的磁盘占用需求有了明显增长.为了方便操作,设置如下:External: 1个卷x40GNormal: 3个卷x30GHight: 5个卷x25GFlex: 3个 ...

  5. 宝塔 windows下apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件

    首先我们来看两段对上传目录设置无权限的列子,配置如下: //在宝塔下如用/upload这个路径应用无效,一定要C:/wwwroot/upload才有效果 <Directory "要去掉 ...

  6. 配置OSPF认证

      按照上图拓扑配置路由器的IP 配置完后测试直连网段连通性 搭建OSPF网络 注意是多区域的配置,R2是ABR 连着area0和area1 并且每个路由器的环回接口IP也要加进去  此时密码以明文方 ...

  7. node.js—创建、删除、追加文件等方法汇总

    使用Node.js的fs模块必须在electron项目里 /* 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 3. fs ...

  8. JAVA总结--java数据类型

    一.String 1.String定义是指向堆内存中的引用:String的赋值本身是引用对象的切换,切换前后的对象依然存在:源码为:private final char value[]: 2.对多个S ...

  9. Mybatis-基本步骤

    1.1Mybatis框架概述 Mybatis是基于Java的持久层框架,内部封装了jdbc,使开发者只需关注sql语句本身,而不需要花费精力去处理加载驱动.创建连接.创建Statement等繁杂的过程 ...

  10. Qt5 对xml文件常用的操作(读写,增删改查)

    转自:https://blog.csdn.net/hpu11/article/details/80227093 项目配置 pro文件里面添加QT+=xml include <QtXml>, ...