参考文献:《数据结构(c++描述)(第二版)》
目录
前言
一、数据类型BinaryTreeNode
二、数据类型BinaryTree
三、测试cpp文件
总结
前言
复习过程中~~将书本的二叉树结点类/二叉树类模板手打一遍
BinaryTreeNode.h文件
#ifndef __BINARYTREENODE_H__
#define __BINARYTREENODE_H__
#ifndef NULL
#define NULL 0
#endif
//enum boolean{FALSE,TRUE};
using namespace std;
template
class BinaryTreeNode
{
public:
T data;
BinaryTreeNode* LeftChild,* RightChild;//左右孩子
BinaryTreeNode(void){LeftChild=RightChild=NULL;}
BinaryTreeNode(const T& e);
BinaryTreeNode(const T& e,BinaryTreeNode* l,BinaryTreeNode* r);
void FreeBinaryTreeNode(BinaryTreeNode* p){delete p;}
BinaryTreeNode* GetBinaryTreeNode(T item,BinaryTreeNode* lptr=NULL,BinaryTreeNode* rptr=NULL);
};
template
BinaryTreeNode::BinaryTreeNode(const T& e)
{
data=e;
LeftChild=RightChild=NULL;
}
template
BinaryTreeNode::BinaryTreeNode(const T& e,BinaryTreeNode* l,BinaryTreeNode* r)
{
data=e;
LeftChild=l;
RightChild=r;
}
template
BinaryTreeNode* BinaryTreeNode::GetBinaryTreeNode(T item,BinaryTreeNode* lptr,BinaryTreeNode* rptr)
{
BinaryTreeNode* p;
p=new BinaryTreeNode(item,lptr,rptr);
if(p==NULL)cerr<<"Memory allocation failure!\n";
return p;
}
#endif
BinaryTree.h文件
#ifndef __BINARYTREE_H__
#define __BINARYTREE_H__
#ifndef NULL
#define NULL 0
#endif
#include
#include "BinaryTreeNode.h"
using namespace std;
template
class BinaryTree
{
public:
//指向树根的指针
BinaryTreeNode* root;
BinaryTree(void){root=NULL;}
~BinaryTree(void){}
bool IsEmpty(void) const{return root? true:false;}
bool Root(T& x) const;
BinaryTreeNode* MakeTree(const T& element,BinaryTreeNode* left=NULL,BinaryTreeNode* right=NULL);
void PreOrder(BinaryTreeNode* root);
void InOrder(BinaryTreeNode* root);
void PostOrder(BinaryTreeNode* root);
};
template
bool BinaryTree::Root(T& x) const
{
//x 为返回的根信息
//如果二叉树为空,则返回false
if(root)
{
x=root->data;
return true;
}
else return false;
}
template
BinaryTreeNode* BinaryTree::MakeTree(const T& element,BinaryTreeNode* left,BinaryTreeNode* right)
{
//结合指针left、right和结点信息element产生新的树
//左右子树为不同的树
//构建组合后的新树
root = new BinaryTreeNode(element,left,right);
if(root==NULL)
{
cerr<<"Memory allocation failure!\n";
return 0;
}
return root;
}
template
void BinaryTree::PreOrder(BinaryTreeNode* root)
{
if(root)
{
cout<data<<" ";
PreOrder(root->LeftChild);
PreOrder(root->RightChild);
}
}
template
void BinaryTree::InOrder(BinaryTreeNode* root)
{
if(root)
{
InOrder(root->LeftChild);
cout<data<<" ";
InOrder(root->RightChild);
}
}
template
void BinaryTree::PostOrder(BinaryTreeNode* root)
{
if(root)
{
PostOrder(root->LeftChild);
PostOrder(root->RightChild);
cout<data<<" ";
}
}
#endif
test.cpp文件
#include
#include "BinaryTreeNode.h"
#include "BinaryTree.h"
using namespace std;
BinaryTree a;
BinaryTreeNode *b1,*b2,*b3,*b4,*b5,*b6;
int main()
{
//构建二叉树
b1=a.MakeTree(6,NULL,NULL);
b2=a.MakeTree(5,NULL,NULL);
b3=a.MakeTree(4,NULL,NULL);
b4=a.MakeTree(3,b1,NULL);
b5=a.MakeTree(2,b3,b2);
b6=a.MakeTree(1,b5,b4);
//树遍历
cout<<"The PreOrder:\n";
a.PreOrder(a.root);
cout<<"\n";
cout<<"The InOrder:\n";
a.InOrder(a.root);
cout<<"\n";
cout<<"The PostOrder:\n";
a.PostOrder(a.root);
}
图——树——二叉树——xx二叉树等等,是非常重要的一块内容,之后会有很多变式算法,最基础的就是二叉树的建立/遍历。之后也要将书上的代码都打一遍呀!