二叉树的初始化及遍历

23 篇文章 0 订阅

前序遍历,中序遍历,后序遍历的递归和非递归实现

层次遍历的实现

 

/**
 * @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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值