头文件:
/*
Name: 二叉树基本操作
Author: Unimen
Date: 2011/10/6 23:42:57
*/
#ifndef _BITREE_H_
#define _BITREE_H_
const int maxn = 100; //树结点的个数
typedef char ElemType;
typedef struct node
{
ElemType value;
struct node *pLChild, *pRChild;
}BiTNode, *PBiTNode;
//前序建立二叉树
void CreateBiTree(PBiTNode &root);
//二叉树前序递归遍历
void RecurPreOrder(PBiTNode root);
//二叉对前序遍历非递归
void PreOrder(PBiTNode root);
//二叉树中序递归遍历
void RecurInOrder(PBiTNode root);
//二叉树中序遍历非递归
void InOrder(PBiTNode root);
//二叉树后序递归遍历
void RecurPostOrder(PBiTNode root);
//二叉树后序遍历非递归
void PostOrder(PBiTNode root);
//按层遍历即广搜
void bfs(PBiTNode root);
#endif
函数实现文件
/*
Name: 二叉树基本操作
Author: Unimen
Date: 2011/10/6 23:42:57
*/
#include <iostream>
#include <cassert>
#include "BiTree.h"
using namespace std;
void CreateBiTree(PBiTNode &root)
{
char ch;
ch = cin.get();
if (' ' == ch)
{
while ((ch=cin.get()) != '\n');
root = NULL;
}
else
{
root = new BiTNode;
root->value = ch;
while ((ch=cin.get()) != '\n');
CreateBiTree(root->pLChild);
CreateBiTree(root->pRChild);
}
}
void RecurPreOrder(PBiTNode root)
{
if (root != NULL)
{
cout<<root->value<<" ";
PreOrder(root->pLChild);
PreOrder(root->pRChild);
}
}
void PreOrder(PBiTNode root)
{
assert(root);
PBiTNode stack[maxn];
int top = 0;
++top;
stack[top] = root;
while (top > 0)
{
root = stack[top];
--top;
cout<<root->value<<" ";
if (root->pRChild != NULL)
{
++top;
stack[top] = root->pRChild;
}
if (root->pLChild != NULL)
{
++top;
stack[top] = root->pLChild;
}
}
}
void RecurInOrder(PBiTNode root)
{
if (root != NULL)
{
RecurInOrder(root->pLChild);
cout<<root->value<<" ";
RecurInOrder(root->pRChild);
}
}
void InOrder(PBiTNode root)
{
assert(root);
PBiTNode stack[maxn];
int top = 0;
PBiTNode temp = NULL;
while (root!=NULL || top>0)
{
if (root != NULL)
{
++top;
stack[top] = root;
root = root->pLChild;
}
else
{
temp = stack[top];
--top;
cout<<temp->value<<" ";
root = temp->pRChild;
}
}
}
void RecurPostOrder(PBiTNode root)
{
if (root != NULL)
{
RecurPostOrder(root->pLChild);
RecurPostOrder(root->pRChild);
cout<<root->value<<" ";
}
}
//进栈向左移,出栈向右看,访问了就访问,没访问向右传
void PostOrder(PBiTNode root)
{
assert(root);
bool bVisitedRight[maxn] = {false};
PBiTNode stack[maxn];
int top = 0;
PBiTNode temp = NULL;
while (root!=NULL || top>0)
{
if (root != NULL)
{
++top;
stack[top] = root;
root = root->pLChild;
bVisitedRight[top] = false;
}
else
{
temp = stack[top];
if (bVisitedRight[top] == true)
{
cout<<temp->value<<" ";
--top;
}
else
{
bVisitedRight[top] = true;
root = temp->pRChild;
}
}
}
}
void bfs(PBiTNode root)
{
assert(root);
PBiTNode queue[maxn];
PBiTNode temp = NULL;
int front, near;
near = front = 0;
++near;
queue[near] = root;
while (front < near)
{
front = (front + 1) % maxn;
temp = queue[front];
cout<<temp->value<<" ";
if (temp->pLChild != NULL)
{
near = (near + 1) % maxn;
queue[near] = temp->pLChild;
}
if (temp->pRChild != NULL)
{
near = (near + 1) % maxn;
queue[near] = temp->pRChild;
}
}
}
功能测试:
/*
Name: 二叉树基本操作
Author: Unimen
Date: 2011/10/6 23:42:57
*/
#include <iostream>
#include "BiTree.h"
using namespace std;
int main()
{
PBiTNode root;
CreateBiTree(root);
cout<<"递归前序遍历: ";
RecurPreOrder(root);
cout<<endl;
cout<<"非递归前序遍历:";
PreOrder(root);
cout<<endl;
cout<<"递归中序遍历: ";
RecurInOrder(root);
cout<<endl;
cout<<"非递归中序遍历:";
InOrder(root);
cout<<endl;
cout<<"递归后序遍历: ";
RecurPostOrder(root);
cout<<endl;
cout<<"非递归后序遍历:";
PostOrder(root);
cout<<endl;
cout<<"按层遍历:";
bfs(root);
cout<<endl;
return 0;
}