C++数据结构之二叉树非递归操作
2011-12-03 13:23
465 查看
#include <iostream>
using namespace std;
const int MaxSize = 100;
template <typename T> //二叉树的结点结构
struct BiNode
{
T data;
BiNode<T> *lchild,*rchild;
BiNode<T> *btr;
int flag;
};
template <typename T>
class BiTree
{
private:
BiNode<T> *root;
BiNode<T> *Create();
void Release(BiNode<T> *root); //析构函数调用
public:
BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入
~BiTree(); //析构函数,释放二叉链表中各结点的存储空间
BiNode<T> *GetRoot(); //获得指向根结点的指针
void PreOrder(BiNode<T> *root); //前序遍历二叉树
void InOrder(BiNode<T> *root); //中序遍历二叉树
void PostOrder(BiNode<T> *root); //后序遍历二叉树
void LevelOrder(BiNode<T> *root); //层序遍历二叉树
};
/*
*前置条件:无
*输 入:无
*功 能:调用Create()函数
*输 出:无
*后置条件:无
*/
template <typename T>
BiTree<T>::BiTree()
{
this->root = Create();
}
/*
*前置条件:空二叉树
*输 入:数据ch;
*功 能:初始化一棵二叉树,构造函数调用
*输 出:无
*后置条件:产生一棵二叉树
*/
template <typename T>
BiNode<T> *BiTree<T>::Create()
{
BiNode<T> *root;
T ch;
cin>>ch;
if(ch == '#')
root = NULL;
else{
root = new BiNode<T>; //生成一个结点
root->data = ch;
root->lchild = Create(); //递归建立左子树
root->rchild = Create(); //递归建立右子树
}
return root;
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:获取指向二叉树根结点的指针
*输 出:指向二叉树根结点的指针
*后置条件:二叉树不变
*/
template <typename T>
BiNode<T> *BiTree<T>::GetRoot()
{
return root;
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:释放二叉链表中各结点的存储空间
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
BiTree<T>::~BiTree(void)
{
Release(root);
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:前序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> *Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
cout<<root->data<<" ";
Stack[++top] = root;
root = root->lchild;
}
if(top != -1)
{
root = Stack[top--];
root = root->rchild;
}
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:中序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::InOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> *Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
Stack[++top] = root;
root = root->lchild;
}
if(top != -1)
{
root = Stack[top--];
cout<<root->data<<" ";
root = root->rchild;
}
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:后序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
top++;
Stack[top].btr = root;
Stack[top].flag = 0;
root = root->lchild;
}
while(top != -1 && Stack[top].flag == 1)
{
root = Stack[top--].btr;
cout<<root->data<<" ";
}
if(top != -1)
{
Stack[top].flag = 1;
root =Stack[top].btr;
root = root->rchild;
}
else
break;
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:层序遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::LevelOrder(BiNode<T> *root)
{
int front,rear;
front = rear = 0; //采用顺序队列,并假定不会发生上溢
BiNode<T> *Queue[MaxSize];
if(root == NULL)
return ;
Queue[++rear] = root;
while(front != rear)
{
BiNode<T> *btr;
btr = Queue[++front];
cout<<btr->data<<" ";
if(btr->lchild != NULL)
Queue[++rear] = btr->lchild;
if(btr->rchild != NULL)
Queue[++rear] = btr->rchild;
}
}
/*
*前置条件:二叉树已经存在
*输 入:无
*功 能:释放二叉树的存储空间,析构函数调用
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
void BiTree<T>::Release(BiNode<T>* root)
{
if (root != NULL){
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;
}
}
int main()
{
BiTree<char> bitree; //创建一棵树
BiNode<char> *binode;
binode = bitree.GetRoot(); //获取指向根结点的指针
cout<<"------前序非递归遍历------"<<endl;
bitree.PreOrder(binode);
cout<<endl;
cout<<"------中序非递归遍历------"<<endl;
bitree.InOrder(binode);
cout<<endl;
cout<<"------后序非递归遍历------"<<endl;
bitree.PostOrder(binode);
cout<<endl;
cout<<"------层序遍历------"<<endl;
bitree.LevelOrder(binode);
cout<<endl;
return 0;
}
using namespace std;
const int MaxSize = 100;
template <typename T> //二叉树的结点结构
struct BiNode
{
T data;
BiNode<T> *lchild,*rchild;
BiNode<T> *btr;
int flag;
};
template <typename T>
class BiTree
{
private:
BiNode<T> *root;
BiNode<T> *Create();
void Release(BiNode<T> *root); //析构函数调用
public:
BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入
~BiTree(); //析构函数,释放二叉链表中各结点的存储空间
BiNode<T> *GetRoot(); //获得指向根结点的指针
void PreOrder(BiNode<T> *root); //前序遍历二叉树
void InOrder(BiNode<T> *root); //中序遍历二叉树
void PostOrder(BiNode<T> *root); //后序遍历二叉树
void LevelOrder(BiNode<T> *root); //层序遍历二叉树
};
/*
*前置条件:无
*输 入:无
*功 能:调用Create()函数
*输 出:无
*后置条件:无
*/
template <typename T>
BiTree<T>::BiTree()
{
this->root = Create();
}
/*
*前置条件:空二叉树
*输 入:数据ch;
*功 能:初始化一棵二叉树,构造函数调用
*输 出:无
*后置条件:产生一棵二叉树
*/
template <typename T>
BiNode<T> *BiTree<T>::Create()
{
BiNode<T> *root;
T ch;
cin>>ch;
if(ch == '#')
root = NULL;
else{
root = new BiNode<T>; //生成一个结点
root->data = ch;
root->lchild = Create(); //递归建立左子树
root->rchild = Create(); //递归建立右子树
}
return root;
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:获取指向二叉树根结点的指针
*输 出:指向二叉树根结点的指针
*后置条件:二叉树不变
*/
template <typename T>
BiNode<T> *BiTree<T>::GetRoot()
{
return root;
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:释放二叉链表中各结点的存储空间
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
BiTree<T>::~BiTree(void)
{
Release(root);
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:前序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> *Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
cout<<root->data<<" ";
Stack[++top] = root;
root = root->lchild;
}
if(top != -1)
{
root = Stack[top--];
root = root->rchild;
}
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:中序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::InOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> *Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
Stack[++top] = root;
root = root->lchild;
}
if(top != -1)
{
root = Stack[top--];
cout<<root->data<<" ";
root = root->rchild;
}
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:后序非递归遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{
int top = -1;
BiNode<T> Stack[MaxSize];
while(root != NULL || top != -1)
{
while(root != NULL)
{
top++;
Stack[top].btr = root;
Stack[top].flag = 0;
root = root->lchild;
}
while(top != -1 && Stack[top].flag == 1)
{
root = Stack[top--].btr;
cout<<root->data<<" ";
}
if(top != -1)
{
Stack[top].flag = 1;
root =Stack[top].btr;
root = root->rchild;
}
else
break;
}
}
/*
*前置条件:二叉树已存在
*输 入:无
*功 能:层序遍历二叉树
*输 出:二叉树中结点的一个线性排列
*后置条件:二叉树不变
*/
template <typename T>
void BiTree<T>::LevelOrder(BiNode<T> *root)
{
int front,rear;
front = rear = 0; //采用顺序队列,并假定不会发生上溢
BiNode<T> *Queue[MaxSize];
if(root == NULL)
return ;
Queue[++rear] = root;
while(front != rear)
{
BiNode<T> *btr;
btr = Queue[++front];
cout<<btr->data<<" ";
if(btr->lchild != NULL)
Queue[++rear] = btr->lchild;
if(btr->rchild != NULL)
Queue[++rear] = btr->rchild;
}
}
/*
*前置条件:二叉树已经存在
*输 入:无
*功 能:释放二叉树的存储空间,析构函数调用
*输 出:无
*后置条件:二叉树不存在
*/
template<class T>
void BiTree<T>::Release(BiNode<T>* root)
{
if (root != NULL){
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;
}
}
int main()
{
BiTree<char> bitree; //创建一棵树
BiNode<char> *binode;
binode = bitree.GetRoot(); //获取指向根结点的指针
cout<<"------前序非递归遍历------"<<endl;
bitree.PreOrder(binode);
cout<<endl;
cout<<"------中序非递归遍历------"<<endl;
bitree.InOrder(binode);
cout<<endl;
cout<<"------后序非递归遍历------"<<endl;
bitree.PostOrder(binode);
cout<<endl;
cout<<"------层序遍历------"<<endl;
bitree.LevelOrder(binode);
cout<<endl;
return 0;
}
相关文章推荐
- C++数据结构之二叉树递归操作
- 数据结构——排序/搜索二叉树(非递归)的基本操作实现
- C++基于递归和非递归算法判定两个二叉树结构是否完全相同(结构和数据都相同)
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之二叉树(前序 中序 后续线索话非递归方式)
- C++ 模板类 栈基本操作 数据结构
- c++模板实现二叉树,线索化,线索化遍历,非递归遍历及一些基本操作
- C++二叉树结构的建立与基本操作
- 数据结构(十二) 二叉树的基本操作 --- 创建一个二叉树 前中后序遍历二叉树
- 数据结构——二叉树的递归/非递归遍历
- 数据结构——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- [C++]数据结构:平衡的二叉搜索树之AVL树的结构特点与基础插入删除操作
- 【数据结构与算法】二叉树递归与非递归遍历(附完整源码)(转)
- 二叉树(10)----比較两个二叉树是否同样(结构和数据),递归和非递归
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之树(四)——二叉树的操作(理论篇)
- 数据结构基础 之 树与二叉树 各类操作、思想与实现
- 常用递归结构数据表,返会指点节点,跟其子节点的操作
- 【数据结构基础】二叉树的建立和递归遍历
- 数据结构(一)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现