题意:



代码实现:

#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. v-model语法糖

    其实v-model就是一个结合了v-bind和v-on的语法糖,实现了双向数据绑定. 举个(栗子):

  2. 使用gui_upload的总结

    今天使用gui_upload函数将文本文件的内容读取到内表.出现了一个问题,总是程序宕掉,出项的提示是 Type conflict when calling a function module. 原来 ...

  3. IDEA安装codota插件和使用,开发人员的知心伙伴

    打开IDEA 点击左上角的File之后,如下图 成功后如图所示

  4. 超微服务器重置ipmi登录密码

    超微服务器的ipmi登录密码不对,需要重置但是bios内并没有找到可以设置的选项. 以下是解决办法: 安装IPMITOOLyum install ipmitool -y 执行以下命令加载模块:modp ...

  5. (09)-Python3之--类的三大特性(封装、继承、多态)

    1.封装 封装,就是只能在类的内部访问,外部访问属性或方法会报异常,python中的封装很简单,只要在属性前或者方法名前加上两个下划线就可以,如self.__name,def __eat(self)这 ...

  6. Spark底层原理详细解析(深度好文,建议收藏)

    Spark简介 Apache Spark是用于大规模数据处理的统一分析引擎,基于内存计算,提高了在大数据环境下数据处理的实时性,同时保证了高容错性和高可伸缩性,允许用户将Spark部署在大量硬件之上, ...

  7. 如何使用 Vuepress

    项目结构 ├─docs │ ├─.vuepress --vuepress相关文件路径 (主要配置) │ │ ├─dist --build 打包生成路径 (自定义) │ │ ├─nav --导航条配置 ...

  8. "INVALID" is not a valid start token

    Search · is not a valid start token https://github.com/prometheus/prometheus/search?q=is+not+a+valid ...

  9. list里放map list 放list

    Map<String,Integer> hashMap = new HashMap<String, Integer>(); Map<String,Integer> ...

  10. mysql本地中127.0.0.1连接不上数据库怎么办

    首先在本地使用Navicat for MySQL建立一个bai数据库.在dreamweaver中建立一个PHP格式的网页,方便链接测试.测试发du现,如果zhi无法使用localhost链接mysql ...