最近,闲着没事,发现自己的数据结构知识忘的差不多.于是,又赶紧拿起尘土飞扬的书本进行复习.今天复习了二叉树的基本概念和操作,于是用c++类对二叉树的基本操作进行了封装.分为BinaryTreeNode类和BinaryTree类.代码如下:
BinaryTreeNode.cpp
BinaryTreeNode类对树形节点进行了封装,而BinaryTree则对二叉树的基本操作进行了封装,代码如下:
BinaryTree.cpp
使用该代码,test.cpp代码如下:
BinaryTreeNode.cpp
Code
BinaryTreeNode类对树形节点进行了封装,而BinaryTree则对二叉树的基本操作进行了封装,代码如下:
BinaryTree.cpp
#pragma once
#include "BinaryTreeNode.h"
template<class elemType>
class BTree
{
public:
BinaryTreeNode<elemType>* root;
public:
void visit(elemType);
void InOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
void PreOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
void PostOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
void LevelOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
inline BTree(void)
{
root = 0;
}
bool CreateBinaryTree(bool(*Input)(elemType&),bool(*IsEmpty)(elemType),BinaryTreeNode<elemType>*&);
bool Root(elemType& x);
void DestoryTree(BinaryTreeNode<elemType>*);
bool BiTreeEmpty();
int BiTreeDepth(BinaryTreeNode<elemType>* root);
public:
~BTree(void);
};
template<class elemType>
void BTree<elemType>::PostOrder(BinaryTreeNode<elemType>* t,void(*Visit)(elemType))
{
if( t != 0)
{
PostOrder(t->lchild,Visit);
PostOrder(t->rchild,Visit);
Visit(t->data);
}
}
template<class elemType>
void BTree<elemType>::PreOrder(BinaryTreeNode<elemType>* t,void(*Visit)(elemType))
{
if(t != 0)
{
Visit(t->data);
PreOrder(t->lchild,Visit);
PreOrder(t->rchild,Visit);
}
}
template<class elemType>
void BTree<elemType>::InOrder(BinaryTreeNode<elemType>*t,void(*Visit)(elemType))
{
if(t != 0)
{
InOrder(t->lchild,Visit);
Visit(t->data);
InOrder(t->rchild,Visit);
}
}
template<class elemType>
BTree<elemType>::~BTree(void)
{
}
template<class elemType>
void BTree<elemType>::DestoryTree(BinaryTreeNode<elemType>* t)
{
if(t != 0)
{
DestoryTree(t->lchild);
DestoryTree(t->rchild);
delete t;
}
}
template<class elemType>
bool BTree<elemType>::BiTreeEmpty()
{
return root == 0;
}
template<class elemType>
bool Root(elemType& x)
{
bool result = false;
if(root != 0)
{
x = root->data;
result = true;
}
return result;
}
template<class elemType>
bool BTree<elemType>::CreateBinaryTree(bool (*Input)(elemType&),bool(*IsEmpty)(elemType),BinaryTreeNode<elemType>* &t)
{
elemType data;
if(Input(data))
{
if(IsEmpty(data))
{
return 0;
}
else
{
if(root == 0)
{
root = new BinaryTreeNode<elemType>;
root->data = data;
CreateBinaryTree(Input,IsEmpty,root->lchild);
CreateBinaryTree(Input,IsEmpty,root->rchild);
}
else
{
t = new BinaryTreeNode<elemType>;
t->data = data;
CreateBinaryTree(Input,IsEmpty,t->lchild);
CreateBinaryTree(Input,IsEmpty,t->rchild);
}
}
}
}
template<class elemType>
int BTree<elemType>::BiTreeDepth(BinaryTreeNode<elemType>* t)
{
int depth = 0;
int ldepth =0,rdepth = 0;
if(t == 0)
{
return 0;
}
else
{
ldepth = BiTreeDepth(t->lchild) + 1;
rdepth = BiTreeDepth(t->rchild) + 1;
if(ldepth > rdepth)
{
return ldepth;
}
else
return rdepth;
}
}
#include "BinaryTreeNode.h"
template<class elemType>
class BTree
{
public:
BinaryTreeNode<elemType>* root;
public:
void visit(elemType);
void InOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
void PreOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
void PostOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
void LevelOrder(BinaryTreeNode<elemType>*,void(*Visit)(elemType));
inline BTree(void)
{
root = 0;
}
bool CreateBinaryTree(bool(*Input)(elemType&),bool(*IsEmpty)(elemType),BinaryTreeNode<elemType>*&);
bool Root(elemType& x);
void DestoryTree(BinaryTreeNode<elemType>*);
bool BiTreeEmpty();
int BiTreeDepth(BinaryTreeNode<elemType>* root);
public:
~BTree(void);
};
template<class elemType>
void BTree<elemType>::PostOrder(BinaryTreeNode<elemType>* t,void(*Visit)(elemType))
{
if( t != 0)
{
PostOrder(t->lchild,Visit);
PostOrder(t->rchild,Visit);
Visit(t->data);
}
}
template<class elemType>
void BTree<elemType>::PreOrder(BinaryTreeNode<elemType>* t,void(*Visit)(elemType))
{
if(t != 0)
{
Visit(t->data);
PreOrder(t->lchild,Visit);
PreOrder(t->rchild,Visit);
}
}
template<class elemType>
void BTree<elemType>::InOrder(BinaryTreeNode<elemType>*t,void(*Visit)(elemType))
{
if(t != 0)
{
InOrder(t->lchild,Visit);
Visit(t->data);
InOrder(t->rchild,Visit);
}
}
template<class elemType>
BTree<elemType>::~BTree(void)
{
}
template<class elemType>
void BTree<elemType>::DestoryTree(BinaryTreeNode<elemType>* t)
{
if(t != 0)
{
DestoryTree(t->lchild);
DestoryTree(t->rchild);
delete t;
}
}
template<class elemType>
bool BTree<elemType>::BiTreeEmpty()
{
return root == 0;
}
template<class elemType>
bool Root(elemType& x)
{
bool result = false;
if(root != 0)
{
x = root->data;
result = true;
}
return result;
}
template<class elemType>
bool BTree<elemType>::CreateBinaryTree(bool (*Input)(elemType&),bool(*IsEmpty)(elemType),BinaryTreeNode<elemType>* &t)
{
elemType data;
if(Input(data))
{
if(IsEmpty(data))
{
return 0;
}
else
{
if(root == 0)
{
root = new BinaryTreeNode<elemType>;
root->data = data;
CreateBinaryTree(Input,IsEmpty,root->lchild);
CreateBinaryTree(Input,IsEmpty,root->rchild);
}
else
{
t = new BinaryTreeNode<elemType>;
t->data = data;
CreateBinaryTree(Input,IsEmpty,t->lchild);
CreateBinaryTree(Input,IsEmpty,t->rchild);
}
}
}
}
template<class elemType>
int BTree<elemType>::BiTreeDepth(BinaryTreeNode<elemType>* t)
{
int depth = 0;
int ldepth =0,rdepth = 0;
if(t == 0)
{
return 0;
}
else
{
ldepth = BiTreeDepth(t->lchild) + 1;
rdepth = BiTreeDepth(t->rchild) + 1;
if(ldepth > rdepth)
{
return ldepth;
}
else
return rdepth;
}
}
使用该代码,test.cpp代码如下:
// BinaryTree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "BTree.h"
#include <iostream>
using namespace std;
template<class elemType>
void Visit(elemType e)
{
cout<<e<<' ';
return ;
}
template<class elemType>
bool Input(elemType& e)
{
cin>>e;
return true;
}
template<class elemType>
bool IsEmpty(elemType e)
{
return e == 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
BTree<int> tree;
BinaryTreeNode<int>* p ;
tree.CreateBinaryTree(Input,IsEmpty,p);
tree.PostOrder(tree.root,Visit);
cout<<endl;
tree.InOrder(tree.root,Visit);
cout<<endl;
tree.PreOrder(tree.root,Visit);
cout<<endl;
cout<<" tree depth:"<<tree.BiTreeDepth(tree.root);
tree.DestoryTree(tree.root);
return 0;
}
//
#include "stdafx.h"
#include "BTree.h"
#include <iostream>
using namespace std;
template<class elemType>
void Visit(elemType e)
{
cout<<e<<' ';
return ;
}
template<class elemType>
bool Input(elemType& e)
{
cin>>e;
return true;
}
template<class elemType>
bool IsEmpty(elemType e)
{
return e == 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
BTree<int> tree;
BinaryTreeNode<int>* p ;
tree.CreateBinaryTree(Input,IsEmpty,p);
tree.PostOrder(tree.root,Visit);
cout<<endl;
tree.InOrder(tree.root,Visit);
cout<<endl;
tree.PreOrder(tree.root,Visit);
cout<<endl;
cout<<" tree depth:"<<tree.BiTreeDepth(tree.root);
tree.DestoryTree(tree.root);
return 0;
}