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. shell脚本中执行python脚本并接收其返回值的例子

    1.在shell脚本执行python脚本时,需要通过python脚本的返回值来判断后面程序要执行的命令 例:有两个py程序  hello.py 复制代码代码如下: def main():    pri ...

  2. TensorFlow学习笔记11-开始用TensorFlow

    TensorFlow运作方式 要用到的代码都在Github上.当然,如果你本地装了TensorFlow,也可以用Everything直接搜索以下文件: mnist.py fully_connected ...

  3. python-IDE的使用(小白先看)

    一.定义 IDE:集成开发环境(Integrated Development Environment) 二.常见的IDE工具: 1.VIM,经典的Linux下的文本编辑器 2.Emacs,LInux的 ...

  4. MySQL-第十三篇使用ResultSetMetaData分析结果集

    1.Result里面包含了一个getMetaData()方法,该方法返回该ResultSet对应的ResultSetMetaData对象. 2.ResultSetMetaData包含的方法: 1> ...

  5. webpack基本介绍及使用

    1.什么是webpack webpack是一个前端资源加载/打包工具.它根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. 官网地址:https://www.webpac ...

  6. python pickle模块的用法

    pickle用于python特有的类型,和python的数据类型间进行转换,提供四个功能 dumps,dump,loads,load. pickle 的用法 #pickle.dumps 将数据通过特殊 ...

  7. 14、前端知识点--Vue生命周期浅析

    vue生命周期 每个Vue实例或组件从创建到显示再到废弃的过程就是vue的生命周期.很多时候我们希望能在这个过程中执行一些操作,于是就有了生命周期钩子. 生命周期钩子函数允许我们在实例不同阶段执行各种 ...

  8. Spark Streaming 单词计数

    Receiver 从数据源接收数据,然后把数据存储在内存中供spark streaming使用,在本地运行spark streaming不能设置master为local或者local[1],此时运行的 ...

  9. python学习笔记(7)文件的访问与函数式编程

    一.文件读写的3中方法 1.直接读入 fiel1=open('test.txt') file2=open('output.txt') while True: line=file1.readLine() ...

  10. js中跳出forEach循环

    缘由:近期在项目中使用lodash.js中的_.foreach方法处理数据,需要在满足条件时结束循环并不执行后面的js代码. 因为foreach中默认没有break方法.在尝试中使用了return f ...