二叉树代码实现与详解--(C++版)
来源:互联网 发布:数据结构 算法 c 编辑:程序博客网 时间:2024/06/01 23:27
首先把本次要分析的二叉树代码放出来#_#
#include<iostream>using namespace std;struct tree{ int data; tree *left,*right;};class Btree{ static int n; static int m; public: tree *root; Btree() { root =NULL; } void create_Btree(int); void preOrder(tree *);//先序遍历 void inOrder(tree *);//中序遍历 void postOrder(tree *);//后序遍历 int count(tree *);//计算二叉树的个数 int findLeaf(tree *);//求二叉树叶子的个数 int findNode(tree *);//求二叉树中度数为1的结点数量};int Btree::n=0;int Btree::m=0;void Btree::create_Btree(int x){ tree *newNode = new tree; newNode->data=x; newNode->right=newNode->left=NULL; if(root==NULL) root=newNode; else{ tree *back; tree *current=root; while(current!=NULL){ back = current; if(current->data > x) current=current->left; else current=current->right; } if(back->data > x) back->left=newNode; else back->right=newNode; }}//计算二叉树的个数int Btree::count(tree *p){ if(p==NULL) return 0; else return count(p->left)+count(p->right)+1;}//先序遍历二叉树void Btree::preOrder(tree *temp){ if(temp!=NULL){ cout<<temp->data<<" "; preOrder(temp->left); preOrder(temp->right); }}//中序遍历二叉树void Btree::inOrder(tree *temp){ if(temp!=NULL){ inOrder(temp->left); cout<<temp->data<<" "; inOrder(temp->right); }}//后序遍历二叉树void Btree::postOrder(tree *temp){ if(temp!=NULL){ postOrder(temp->left); postOrder(temp->right); cout<<temp->data<<" "; }}//统计叶子的数量int Btree::findLeaf(tree *temp){ if(temp == NULL) return 0; else{ if(temp->left == NULL && temp->right==NULL) return n+=1; else{ findLeaf(temp->left); findLeaf(temp->right); } }}//二叉树中度数为1的结点数量int Btree::findNode(tree *temp){ if(temp==NULL) return 0; else{ if(temp->left!=NULL && temp->right!=NULL) { findNode(temp->left); findNode(temp->right); } if(temp->left!=NULL&&temp->right==NULL) { m+=1; findNode(temp->left); } if(temp->left==NULL&&temp->right!=NULL) { m+=1; findNode(temp->right); } } return m;}int main(){ Btree bin; int array[]={7,4,2,3,15,35,6,45,55,20,1,14,56,57,58}; int k; k = sizeof(array)/sizeof(array[0]); cout<<"建立排序二叉树顺序:"<<endl; for(int i=0;i<k;i++){ cout<<array[i]<<" "; bin.create_Btree(array[i]); } cout<<endl; cout<<"二叉树节点个数: "<<bin.count(bin.root)<<endl; cout<<"二叉树叶子个数:"<<bin.findLeaf(bin.root)<<endl; cout<<"二叉树中度数为1的结点的数量为:"<<bin.findNode(bin.root)<<endl; cout<<endl<<"先序遍历序列: "<<endl; bin.preOrder(bin.root); cout<<endl; cout<<endl<<"中序遍历序列: "<<endl; bin.inOrder(bin.root); cout<<endl<<"后序遍历序列: "<<endl; bin.postOrder(bin.root);}
下面是程序运行的结果
我们先来看一下main()函数中的操作,首先定义了一个定长的数组,然后遍历数组把每个元素存储到二叉树中,然后在对数组进行其他的一些操作。
程序中最难理解的部分就是元素存储到二叉树的部分,我手画了一张图,希望能帮助各位理解!
下面是先序遍历的顺序(彩色线)
下面是中序遍历的顺序(彩色线)
下面是后序遍历的顺序(彩色线)
阅读全文
0 0
- 二叉树代码实现与详解--(C++版)
- 二叉树构造与遍历的C程序实现代码
- C语言二叉树知识点讲解与实现代码
- C语言二叉树与队列实现基础代码
- 二叉树数组实现[C/C++]代码
- 二叉搜索树详解及实现代码(BST)
- 线索二叉树详解以及代码实现
- 二叉查找树C实现代码
- 二叉树链式结构C代码实现
- 二叉树线索化C代码实现
- 详解二叉查找树算法的实现(c语言)
- 二叉树的链式存储与实现(C++)
- 平衡二叉搜索树(AVL树)的原理及实现源代码(有图文详解和C++、Java实现代码)
- 二叉树实现代码
- 二叉树实现详解
- 二叉搜索树的基本操作(C代码实现)
- 简单的C语言二叉树的实现代码
- 二叉树用数组顺序存储(完全二叉树)并实现三种遍历,代码详解!!!
- bootstrap自定义
- MTK电话本联系人备份加密与破解
- xml文件的解析
- mysql关于时间查询 : 获取2017-06-14 17:33:03到现在的所有订单
- IOS Monkey测试1
- 二叉树代码实现与详解--(C++版)
- [树的同构][二分][可并堆维护哈希] LOJ#6066 || BZOJ4928 && 2017 山东一轮集训 Day3. 第二题
- HtmlAgilityPack之HtmlNode类
- Spring Web MVC处理请求的流程
- 限制EditText只输入数字和字母
- javascript Date format(js日期格式化)
- 【二叉树】计算左叶的和
- 在xml中的一些特殊符号的使用如:> < &等等
- nagios监控mysql(percona-monitoring-plugins)