C++二叉树的实现
来源:互联网 发布:nba2k17季后赛数据 编辑:程序博客网 时间:2024/04/30 15:44
#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 *); //后序遍历 void display1() {Preorder(root); cout<<endl;} void display2() {inorder(root);cout<<endl;} void display3() {Postorder(root); cout<<endl;} 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); } return n; }}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;}void main(){ Btree A; 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]<<" "; A.create_Btree(array[i]); } cout<<endl; cout<<"二叉树节点个数: "<<A.count(A.root)<<endl; cout<<"二叉树叶子个数:"<<A.findleaf(A.root)<<endl; cout<<"二叉树中度数为1的结点的数量为:"<<A.findnode(A.root)<<endl; cout<<endl<<"先序遍历序列: "<<endl; A.display1(); cout<<endl<<"中序遍历序列: "<<endl; A.display2(); cout<<endl<<"后序遍历序列: "<<endl; A.display3();}
0 0
- 二叉树 c实现
- 二叉树 C实现
- 二叉树 c 实现
- c语言二叉树和二叉搜索树的实现
- c语言实现的简单二叉树
- 数据结构的C实现_二叉树
- 二叉树的基本功能实现(c++)
- c语言二叉树的实现
- C语言实现二叉树的遍历
- 二叉树的简单C实现
- 哈弗曼二叉树的C简单实现
- 二叉查找树的实现(using C)
- [数据结构]C语言二叉树的实现
- 数据结构 二叉树的实现 c语言版
- 二叉树的C语言实现
- C语言一个二叉树的实现
- 【C基础】二叉搜索树的实现
- 二叉查找树的C语言实现
- 剑指offer--从尾到头打印链表
- oracle事务
- 算法练习-NOJ-1009-迷宫问题
- 在最新的Xcode中创建Protocol和Category文件
- 黑马程序员-------------面向对象和类
- C++二叉树的实现
- c语言获取当前工作路径(转)
- bzoj 3672: [Noi2014]购票
- 算法练习-NOJ-1141-走迷宫
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- margin(0 auto)无法居中的原因
- AsyncHttpClient笔记——如何构造客户端
- iOS:堆(heap)和栈(stack)的理解
- Merge Two Sorted Lists