题意:



代码实现:

#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. 【Oracle】Oracle 10g下载路径

    ORACLE 10g下载地址 下载方法: 直接复制下面的链接,打开迅雷,自动会识别下载的内容 Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise ...

  2. LeetCode993. 二叉树的堂兄弟节点

    题目 1 class Solution { 2 public: 3 TreeNode* r1;TreeNode* r2; 4 bool isCousins(TreeNode* root, int x, ...

  3. DNS基础概要

    dns服务系统由客户端和服务器组成,提供域名到ip地址的解析,或者提供ip地址到域名的逆向解析. 1.DNS域名空间 每个dns域名由分级的label构成,如www.sina.com.cn,由www. ...

  4. SQLHelper ------ python实现

    SQLHelper ------ python实现 1.第一种: import pymysql import threading from DBUtils.PooledDB import Pooled ...

  5. Python編碼格式錯誤解決方案及案例

    Python格式錯誤解決方案及案例 這幾天在玩爬蟲,在解析和提取内容時經常出現由於内容格式問題導致出錯,為防止以後出錯,整下一下,以下是這幾天的總結: 1. 特殊符號或表情符號等 背景:爬取一個烹飪教 ...

  6. 使用pushplus+python实现亚马逊到货消息推送微信

    xbox series和ps5发售以来,国内黄牛价格一直居高不下.虽然海外amazon上ps5补货很少而且基本撑不过一分钟,但是xbox series系列明显要好抢很多. 日亚.德亚的xbox ser ...

  7. RSA2对于所有商户都是单独一对一的,并且只支持开发平台密钥管理和沙箱

    蚂蚁金服开发者社区 https://openclub.alipay.com/club/history/read/1495 RSA 和 RSA2 签名算法区别 - 支付宝开放平台 https://ope ...

  8. apache httpclient 4 范例

    下面是一个通过apache httpclient 4 实现http/https的普通访问和BasicAuth认证访问的例子.依赖的第三方库为: 下面是具体实现: package test; impor ...

  9. Apache Http Server 一些资料

    配置虚拟主机的例子: http://httpd.apache.org/docs/current/vhosts/examples.html

  10. .axios的特点有哪些

    从浏览器中创建XMLHttpRequests:node.js创建http请求:支持Promise API:拦截请求和响应:转换请求数据和响应数据:取消请求:自动换成json.axios中的发送字段的参 ...