题意:



代码实现:

#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++实现二叉树的基本操作:建立、遍历、计算深度、节点数、叶子数等的更多相关文章

  1. <二叉树的基本操作(有层次遍历)>

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...

  2. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历、中序遍历、后续遍历)

    树 利用顺序存储和链式存储的特点,可以实现树的存储结构的表示,具体表示法有很多种. 1)双亲表示法:在每个结点中,附设一个指示器指示其双亲结点在数组中的位置. 2)孩子表示法:把每个结点的孩子排列起来 ...

  3. 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出

    用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...

  4. c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)

    #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define ...

  5. Python --- 二叉树的层序建立与三种遍历

    二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有....(此处省略好多字)....等的优良特点. 之前在刷LeetCode的时候把有关树的题目全部跳过了,(ORZ:我这种连数据结 ...

  6. 二叉树的基本操作(C语言版)

    今天走进数据结构之二叉树 二叉树的基本操作(C 语言版) 1 二叉树的定义 二叉树的图长这样: 二叉树是每个结点最多有两个子树的树结构,常被用于实现二叉查找树和二叉堆.二叉树是链式存储结构,用的是二叉 ...

  7. 二叉树的基本操作(含Huffman树)

    大二时候写的烂代码,翻出来复习复习(o(╯□╰)o). 代码: #include <stdio.h> #include <stdlib.h> #define Max_Size ...

  8. <二叉树的基本操作>

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...

  9. 数据结构《10》----二叉树 Morris 中序遍历

    无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...

  10. SDUT 3344 数据结构实验之二叉树五:层序遍历

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

随机推荐

  1. 【Linux】rsync的相关用途

    Rsync,代表"remote sync",它是本地和远程主机文件同步工具.它只同步更改的文件,以此实现最小化传输数据. 我使用Ubuntu 16.04做为例子,但是你可以把它应用 ...

  2. 使用 TensorBoard 可视化模型、数据和训练

    使用 TensorBoard 可视化模型.数据和训练 在 60 Minutes Blitz 中,我们展示了如何加载数据,并把数据送到我们继承 nn.Module 类的模型,在训练数据上训练模型,并在测 ...

  3. 使用modify修改内表

    modify修改内表,有这样一种方式,MODIFY TABLE itab FROM wa [TRANSPORTING ..]. 然后这里的内表itab是有条件的,这个itab必须要有table key ...

  4. 24v转3.3v稳压芯片,高效率DC-DC变换器3A输出电流

    PW6206系列是一个高精度,高输入电压低静态电流,高速,低功耗降线性稳压器具有高纹波抑制.输入电压高达40V,负载电流为在VOUT=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造.PW ...

  5. 如何在C#中使用MSMQ

    MSMQ (Microsoft消息队列)是Windows中默认可用的消息队列.作为跨计算机系统发送和接收消息的可靠方法,MSMQ提供了一个可伸缩.线程安全.简单和使用方便的队列,同时为你提供了在Win ...

  6. uni-app 开发随笔(踩坑记录)

    这里总结一些uni-app开发时我遇到的坑 uni-app获取元素高度及屏幕高度(uni-app不可使用document) uni.getSystemInfo({ success: function( ...

  7. vue原生文件上传,可以多文件上传

    1.单文件上传 <template> <div> <label for="fileInput"> <i aria-hidden=" ...

  8. hive 时间相关的函数

    yyyy-MM-dd与yyyyMMdd000000转换的三种方法 方法一:date_format(只支持yyyy-MM-dd -> yyyyMMdd000000) select date_for ...

  9. 一站式入口服务|爱奇艺微服务平台 API 网关实战 原创 弹性计算团队 爱奇艺技术产品团队

    一站式入口服务|爱奇艺微服务平台 API 网关实战 原创 弹性计算团队 爱奇艺技术产品团队

  10. InnoDB 事务隔离级探索

    https://mp.weixin.qq.com/s/gWYL2Th9Go5LDhkyGB_rYQ