C++实现二叉树的基本操作:建立、遍历、计算深度、节点数、叶子数等
题意:

代码实现:
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//二叉树节点
struct BinaryTreeNode
{
char data;
BinaryTreeNode* leftChild;
BinaryTreeNode* rightChild;
};
//堆栈节点,用于深度遍历
struct stackNode
{
BinaryTreeNode* ptr;
char tag;//tag=0标志进入左子树,tag=1标志进入右子树
};
class BinaryTree //二叉树的类
{
public:
//根据完全前序遍历创建二叉树
void createBinaryTree(BinaryTreeNode* &root)
{
root=new BinaryTreeNode();
char newData;
cin>>newData;
if(newData=='#')
{
root=NULL;
}
else
{
root->data=newData;
createBinaryTree(root->leftChild);
createBinaryTree(root->rightChild);
}
}
//递归实现前序遍历
void preTraversal(BinaryTreeNode* root)
{
if(root!=NULL)
{
cout<<root->data<<" ";
preTraversal(root->leftChild);
preTraversal(root->rightChild);
}
}
//递归实现后续遍历
void lastTraversal(BinaryTreeNode* root)
{
if(root!=NULL)
{
lastTraversal(root->leftChild);
lastTraversal(root->rightChild);
cout<<root->data<<" ";
}
}
//非递归实现中序遍历
void mid(BinaryTreeNode* root)
{
stack<BinaryTreeNode*> S;
BinaryTreeNode* p=root;
do
{
while(p!=NULL)
{
S.push(p);
p=p->leftChild;
}
if(!S.empty())
{
p=S.top();
cout<<p->data<<" ";
S.pop();
p=p->rightChild;
}
}
while(p!=NULL||!S.empty());
}
//计算节点总数
int nodeCount(BinaryTreeNode* &root)
{
if(root==NULL)
{
return 0;
}
else
{
return nodeCount(root->leftChild)+nodeCount(root->rightChild)+1;
}
}
//计算二叉树的高度
int treeHight(BinaryTreeNode* &root)
{
if(root==NULL)
{
return 0;
}
else
{
int LH=treeHight(root->leftChild);
int RH=treeHight(root->rightChild);
return LH > RH ? LH+1 : RH+1;
}
}
//计算二叉树的叶子个数
int getLeavesCount(BinaryTreeNode* &root)
{
if(root==NULL)
{
return 0;
}
else if (root->leftChild == NULL && root->rightChild == NULL)
{
return 1;
}
else
{
int leftLeavesCount = getLeavesCount(root->leftChild);
int rightLeavesCount = getLeavesCount(root->rightChild);
return leftLeavesCount + rightLeavesCount;
}
}
//查找值=x的节点个数
int findNode(BinaryTreeNode* &root,char x,int coun)
{
if(root!=NULL)
{
if(root->data==x) coun++;
findNode(root->leftChild,x,coun);
findNode(root->rightChild,x,coun);
}
return coun;
}
//以缩格文本形式输出所有节点
void outputNode(BinaryTreeNode* &root,int x)
{
if(root!=NULL)
{
for(int i=0;i<x;i++) cout<<" ";
cout<<root->data<<endl;
x=x+2;
outputNode(root->leftChild,x);
outputNode(root->rightChild,x);
}
}
};
int main()
{
BinaryTree tree;
BinaryTreeNode* treeRoot;
char func;
while(cin>>func){
if(func=='C')
{
tree.createBinaryTree(treeRoot);
cout<<"Created success!";
}
if(func=='1') {cout<<"Preorder is:";tree.preTraversal(treeRoot);cout<<".";}
if(func=='2') {cout<<"Inorder is:";tree.mid(treeRoot);cout<<".";}
if(func=='3') {cout<<"Postorder is:";tree.lastTraversal(treeRoot);cout<<".";}
if(func=='N') cout<<"Nodes="<<tree.nodeCount(treeRoot)<<".";
if(func=='H') cout<<"Height="<<tree.treeHight(treeRoot)<<".";
if(func=='L') cout<<"Leaf="<<tree.getLeavesCount(treeRoot)<<".";
if(func=='F')
{
char x;
cin>>x;
cout<<"The count of "<<x<<" is "<<tree.findNode(treeRoot,x,0)<<".";
}
if(func=='P')
{
cout<<"The tree is:"<<endl;
tree.outputNode(treeRoot,0);
}
cout<<endl;
}
return 0;
}
PS:有时间再补充注意点吧
C++实现二叉树的基本操作:建立、遍历、计算深度、节点数、叶子数等的更多相关文章
- <二叉树的基本操作(有层次遍历)>
#include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...
- C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历、中序遍历、后续遍历)
树 利用顺序存储和链式存储的特点,可以实现树的存储结构的表示,具体表示法有很多种. 1)双亲表示法:在每个结点中,附设一个指示器指示其双亲结点在数组中的位置. 2)孩子表示法:把每个结点的孩子排列起来 ...
- 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出
用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...
- c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define ...
- Python --- 二叉树的层序建立与三种遍历
二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有....(此处省略好多字)....等的优良特点. 之前在刷LeetCode的时候把有关树的题目全部跳过了,(ORZ:我这种连数据结 ...
- 二叉树的基本操作(C语言版)
今天走进数据结构之二叉树 二叉树的基本操作(C 语言版) 1 二叉树的定义 二叉树的图长这样: 二叉树是每个结点最多有两个子树的树结构,常被用于实现二叉查找树和二叉堆.二叉树是链式存储结构,用的是二叉 ...
- 二叉树的基本操作(含Huffman树)
大二时候写的烂代码,翻出来复习复习(o(╯□╰)o). 代码: #include <stdio.h> #include <stdlib.h> #define Max_Size ...
- <二叉树的基本操作>
#include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...
- 数据结构《10》----二叉树 Morris 中序遍历
无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...
- SDUT 3344 数据结构实验之二叉树五:层序遍历
数据结构实验之二叉树五:层序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...
随机推荐
- mysql—if函数
在mysql中if()函数的具体语法如下:IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,则返回expr3的值. 开始实 ...
- Zabbix监控虚拟机服务-告警与自动恢复
今天稍微空闲,使用下zabbix的5.0版本,目前生产环境是4.x版本 今天就只实现一个目的:监控任意一个服务(示例中监控的是docker.service),如果服务挂了,自动给恢复,先看一个动图 搭 ...
- Django实现文件在本地的存储和读取
需求介绍:将图片存入本地的电脑文件夹中,将图片的路径保存到数据库,然后通过数据库的路径读取文件: 1.文件的存入: 前端文件: <form class="form-horizontal ...
- django form和form组件
form介绍: 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是 ...
- 六个你不知道的PR快捷键,拯救你的剪辑效率
5G时代到来,会剪辑视频的人,无论在校园还是未来步入职场都很吃香.对于普通人来说,视频处理也成为了一个通用技能.PR是我们大多数人剪辑中,经常会用到的剪辑工具,之前的文章中已经给大家总结了pr的一些提 ...
- 配接Cisco设备
- 纯手工撸一个vue框架
前言 vue create 真的很方便,但是很多人欠缺的是手动撸一遍.有些人离开脚手架都不会开发了. Vue最简单的结构 步骤 搭建最基本的结构 打开空文件夹,通过 npm init 命令生成pack ...
- The Garbage Collection Handbook
The Garbage Collection Handbook The Garbage Collection Handbook http://gchandbook.org/editions.html ...
- 进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道
管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...
- RMI笔记
这是<java核心技术> 第11章 分布式对象的笔记. RMI基本原理 我们使用远程方法调用是希望达到这样的目的: 可以像调用本地方法一样去调用一个远程方法. 实现远程调用的方式是 为客户 ...