您的位置:首页 > 其它

二叉树的初始化及遍历

2013-05-23 15:51 288 查看
前序遍历,中序遍历,后序遍历的递归和非递归实现

层次遍历的实现

/**
* @brief Binary Tree
* @author An
* @data  2013.5.22
**/
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
using namespace std;

class BiTNode
{
public:
BiTNode()
{
lchild = NULL;
rchild = NULL;
}
int data;
BiTNode *lchild, *rchild;
};

class BiTree
{
public:
BiTNode *root;
BiTree()
{
root = NULL;
}

};

void CreatBiTree( BiTNode *&t );
void CreatBiTree_( BiTNode *&t, int *array );
// visit for every node
void PrintElement( int e );

// recursive
void PreOrderTraverse( BiTNode *t );
void InOrderTraverse( BiTNode *t );
void PostOrderTraverse( BiTNode *t );

// non-recursive
void PreOrderTraverse_( BiTNode *t );
void InOrderTraverse_( BiTNode *t );
void PostOrderTraverse_( BiTNode *t );
void LevelOrderTraverse( BiTNode *t );
#endif


/**
* @brief Binary Tree
* @author An
* @data  2013.5.22
**/
#include "BinaryTree.h"
#include <stack>
#include <queue>
using namespace std;

void CreatBiTree( BiTNode *&t )
{
int tmp;
cin >> tmp;
if ( tmp == 0 )
{
t = NULL;
}
else
{
t = new BiTNode;
t->data = tmp;
CreatBiTree( t->lchild );
CreatBiTree( t->rchild );
}
}

// cin from an array
void CreatBiTree_( BiTNode *&t, int *array )
{
static int i = -1;
++i;
if ( array[ i ] == 0 )
{
t = NULL;
}
else
{
t = new BiTNode;
t->data = array[ i ];
CreatBiTree_( t->lchild, array );
CreatBiTree_( t->rchild, array );
}
}

void PrintElement( int e )
{
cout << e << " ";
}

// recursive
void PreOrderTraverse( BiTNode *t )
{
if ( t != NULL )
{
PrintElement( t->data );
PreOrderTraverse( t->lchild );
PreOrderTraverse( t->rchild );
}
}

void InOrderTraverse( BiTNode *t )
{
if ( t != NULL )
{
InOrderTraverse( t->lchild );
PrintElement( t->data );
InOrderTraverse( t->rchild );
}
}

void PostOrderTraverse( BiTNode *t )
{
if ( t != NULL )
{
PostOrderTraverse( t->lchild );
PostOrderTraverse( t->rchild );
PrintElement( t->data );
}
}

// non-recursive
void LevelOrderTraverse( BiTNode *t )
{
queue<BiTNode*> que;
if ( t != NULL )
{
que.push( t );
}
while ( que.size() != 0 )
{
PrintElement( (que.front())->data );
if ( (que.front())->lchild != NULL )
{
que.push( (que.front())->lchild );
}
if ( (que.front())->rchild != NULL )
{
que.push( (que.front())->rchild );
}
que.pop();
}
}

// non-recursive
void PreOrderTraverse_( BiTNode *t )
{
stack<BiTNode*> stk;
BiTNode *p = t;
while ( p != NULL || !stk.empty() )
{
while ( p != NULL )
{
PrintElement( p->data );
stk.push( p );
p = p->lchild;
}
if ( !stk.empty() )
{
p = stk.top();
stk.pop();
p = p->rchild;
}
}
}

// non-recursive
void InOrderTraverse_( BiTNode *t )
{
stack<BiTNode*> stk;
BiTNode *p = t;
while ( p != NULL || !stk.empty() )
{
while ( p != NULL )
{
stk.push( p );
p = p->lchild;
}
if ( !stk.empty() )
{
p = stk.top();
PrintElement( p->data );
stk.pop();
p = p->rchild;
}
}
}

// non-recursive
void PostOrderTraverse_( BiTNode *t )
{
stack<BiTNode*> stk;
BiTNode *cur;
BiTNode *pre = NULL;
stk.push( t );
while ( !stk.empty() )
{
cur = stk.top();
// if lchild and rchild is empty, or the lchild or rchild has been visited, then the current node will be visited.
if ( ( cur->lchild == NULL && cur->rchild == NULL ) || ( pre != NULL && ( pre == cur->lchild || pre == cur->rchild ) ) )
{
PrintElement( cur->data );
pre = cur;
stk.pop();
}
else
{
if ( cur->rchild != NULL )
stk.push( cur->rchild );
if ( cur->lchild != NULL )
stk.push( cur->lchild );
}
}
}


#include "LinkedList.h"
#include "BinaryTree.h"
#include <iostream>
using namespace std;

int main()
{
// test Linked list
// 	Link linkedlist;
// 	for ( int i = 0; i < 10; ++i )
// 	{
// 		linkedlist.InsertTail( i );
// 	}
// 	linkedlist.print();
// 	linkedlist.ReverseLink();
// 	linkedlist.print();
// 	linkedlist.DelElem( 5 );
// 	linkedlist.print();

// test Tree
BiTree T;
int array[] = { 1, 2, 3, 0, 5, 0, 0, 4, 0, 0, 6, 7, 0, 8, 0, 0, 0 };
CreatBiTree_( T.root, array );
cout << "Pre: " << endl;
PreOrderTraverse( T.root );
cout << endl;
PreOrderTraverse_( T.root );
cout << endl;
cout << "In: " << endl;
InOrderTraverse( T.root );
cout << endl;
InOrderTraverse_( T.root );
cout << endl;
cout << "Post: " << endl;
PostOrderTraverse( T.root );
cout << endl;
PostOrderTraverse_( T.root );
cout << endl;
cout << "Level: " << endl;
LevelOrderTraverse( T.root );
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: