二叉树的C++实现
2013-12-12 18:04
281 查看
/*--------BTree.h---------*/
#ifndef _BTREE_H_
#define _BTREE_H_
#include <iostream>
using namespace std;
//树节点的结构
typedef class bTree
{
public:
int data;
bTree *lchild,*rchild;
}bTreeNode,*pbTreeNode;
class BTree
{
public:
pbTreeNode root;
BTree();
~BTree();
void Create_BTree(int); //建立二叉树
void Preorder(pbTreeNode); //前序遍历
void Inorder(pbTreeNode); //中序遍历
void Postorder(pbTreeNode); //后序遍历
int count(pbTreeNode); //计算二叉树结点的个数
int LeafNum(pbTreeNode); //计算二叉树叶子的个数
int NodeNum(pbTreeNode); //计算二叉树度数为1时的结点个数
private:
static int node_num,leaf_num;
};
#endif
/*-------------BTree.cpp------------*/
#include "BTree.h"
int BTree::node_num=0;
int BTree::leaf_num=0;
BTree::BTree()
{
root=NULL;
}
BTree::~BTree()
{
delete root;
}
//建立二叉树
void BTree::Create_BTree(int value)
{
pbTreeNode newNode=new bTreeNode;
if(NULL==newNode)
{
cout<<"Memory allocate failed!\n";
exit(-1);
}
newNode->data=value;
newNode->rchild=newNode->lchild=NULL;
if(NULL==root)
{
root=newNode;
}
else
{
pbTreeNode current=root,back=NULL;
while(NULL!=current)
{
back=current;
if((current->data)>value)
current=current->lchild;
else
current=current->rchild;
}
if((back->data)>value)
back->lchild=newNode;
else
back->rchild=newNode;
}
}
//计算二叉树结点的个数
int BTree::count(pbTreeNode Pointer)
{
if(NULL==Pointer)
return 0;
else
return (count(Pointer->lchild)+count(Pointer->rchild)+1);
}
//计算二叉树叶子的个数
int BTree::LeafNum(pbTreeNode Pointer)
{
if(NULL==Pointer)
return 0;
else
{
if(NULL==Pointer->lchild && NULL==Pointer->rchild)
return leaf_num++;
else
{
LeafNum(Pointer->lchild);
LeafNum(Pointer->rchild);
}
return leaf_num;
}
}
//计算二叉树度数为1时的结点个数
//结点所拥有的子树的个数称为结点的度
int BTree::NodeNum(pbTreeNode Pointer)
{
if(NULL==Pointer)
return 0;
else
{
if(NULL!=Pointer->lchild && NULL!=Pointer->rchild)
{
NodeNum(Pointer->lchild);
NodeNum(Pointer->rchild);
}
if(NULL!=Pointer->lchild && NULL==Pointer->rchild)
{
node_num++;
NodeNum(Pointer->lchild);
}
if(NULL==Pointer->lchild && NULL!=Pointer->rchild)
{
node_num++;
NodeNum(Pointer->rchild);
}
}
return node_num;
}
//前序遍历
void BTree::Preorder(pbTreeNode Pointer)
{
if(NULL!=Pointer)
{
cout<<" ["<<Pointer->data<<"] ";
Preorder(Pointer->lchild);
Preorder(Pointer->rchild);
}
}
//中序遍历
void BTree::Inorder(pbTreeNode Pointer)
{
if(NULL!=Pointer)
{
Inorder(Pointer->lchild);
cout<<" ["<<Pointer->data<<"] ";
Inorder(Pointer->rchild);
}
}
//后序遍历
void BTree::Postorder(pbTreeNode Pointer)
{
if(NULL!=Pointer)
{
Postorder(Pointer->lchild);
Postorder(Pointer->rchild);
cout<<" ["<<Pointer->data<<"] ";
}
}
/*************main.cpp*************/
#include "BTree.h"
int main()
{
int arr[]={7,4,1,5,16,8,11,12,15,9,2};
int len=sizeof(arr)/sizeof(arr[0]);
BTree btree;
cout<<"原数组元素为:\n";
for(int i=0;i<len;++i)
{
cout<<" "<<arr[i];
btree.Create_BTree(arr[i]);
}
cout<<"\n\n";
cout<<"二叉树节点个数:"<<btree.count(btree.root)<<endl;
cout<<"\n二叉树叶子个数:"<<btree.LeafNum(btree.root)<<endl;
cout<<"\n二叉树度数为1的节点个数:"<<btree.NodeNum(btree.root)<<endl;
cout<<"\n先序遍历:\n";
btree.Preorder(btree.root);
cout<<"\n\n中序遍历:\n";
btree.Inorder(btree.root);
cout<<"\n\n后序遍历:\n";
btree.Postorder(btree.root);
cout<<"\n\n";
return 0;
}
相关文章推荐
- 二叉树链表C++实现
- C++ 实现求二叉树的深度及便利(递归与非递归算法)
- [置顶] 二叉树的非递归前序、中序以及后序遍历C++模版类实现
- 数据结构学习笔记(五)二叉树及其C++实现
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增
- 二叉树的C++实现
- C++实现的二叉树---家谱图
- 用C++实现二叉树的三种遍历方式
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 二叉树的顺序存储结构的C++实现
- c++模板类实现基础二叉树的各种操作
- 算法与数据结构基础4:C++二叉树实现及遍历方法大全
- 二叉树的遍历C++实现
- C++:将表达式构建为二叉树实现一个简单计算器
- C++实现搜索二叉树
- c++实现二叉树的插入、删除、查询、遍历和树形打印
- 二叉树的深度优先遍历与广度优先遍历 [C++实现]
- 二叉树的基本操作以及相关问题的求解—C++实现
- c++模板实现二叉树,线索化,线索化遍历,非递归遍历及一些基本操作
- 二叉树的链式存储结构 C++代码实现