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. base64编解码的另外几个版本

    #include "crypto/encode/base64.h" static const std::string base64_chars = "ABCDEFGHIJ ...

  2. js面向对象程序设计之继承

    在面向对象语言中继承分成两种:接口继承和实现继承.解释一下,接口继承只继承方法的签名,而实现继承则是继承实际的方法.但是ECMAScript中的函数没有签名所以无法进行接口继承,只能是实现实现继承.而 ...

  3. 使用pyautogui替代selenium,图像识别进行web自动化测试--基于python语言

    这里演示一下使用pyautogui替代selenium进行web自动化测试,并不建议使用pyautogui进行web自动化测试,因为元素的ui一旦有长宽变化,或者风格的变化,执行时就会发生异常,仅当学 ...

  4. 流程控制: if分支 while循环 for循环

    流程控制 Python程序执行,一定按照某种规律在执行 1.宏观一定是自上而下(逻辑上方代码一定比逻辑下方代码先执行):顺序结构 2.遇到需要条件判断选择不同执行路线的执行方式:分支结构 3.有些事情 ...

  5. 应用安全 - SuiteCRM - 漏洞汇总

    CVE-2019-12598.CVE-2019-12601 SuiteCRM SQL注入与远程代码执行漏洞 SalesAgility SuiteCRM .x版本..x版本和7..5之前的7..x版本中 ...

  6. 转载-linux挂载的意思

    挂载:Liunx采用树形的文件管理系统,也就是在Linux系统中,可以说已经没有分区的概念了.分区在Linux和其他设备一样都只是一个文件.要使用一个分区必须把它加载到文件系统中.这可能难于理解,继续 ...

  7. 什么是 Java 对象深拷贝?面试必问!

    点击上方蓝色链接,关注并"设为星标" Java干货,每天及时推送 介绍 在Java语言里,当我们需要拷贝一个对象时,有两种类型的拷贝:浅拷贝与深拷贝. 浅拷贝只是拷贝了源对象的地址 ...

  8. free野指针问题

    gdb backtrace内容如下: Program received signal SIGABRT, Aborted. (gdb) p cmd No symbol "cmd" i ...

  9. 本地文件夹上传到Github(一)

    1.在要上传的文件夹下单击右键,选择Git Bash here打开Git bash,设置全局用户名和邮箱 语法:git config --global user.name wandou 语法:git ...

  10. .NetCore模拟Postman的BasicAuth生成Authrization

    一.思路 BasicAuth 是一种简单权限,传输UserName=<userName>,Password=<password> 1.用:连接Username,Password ...