码迷,mamicode.com
首页 > 其他好文 > 详细

travel the binary tree by level 4 ( from down to top and from left to right every level )

时间:2014-05-04 18:22:16      阅读:381      评论:0      收藏:0      [点我收藏+]

标签:面试题

travel the binary tree by level 4 ( from down to top and from left to right every level )

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:travel the binary tree by level 4 ( from down to top and from left to right every level )

博客时间:2014-5-3;

编程语言:C++ ;

编程坏境:Windows 7 专业版 x64;

编程工具:vs2008 32位编译器;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;


my words

to be or not to be.

problem

travel the binary tree by level from down to top and from left to right every level, with classing them by level.

eg: the binary tree follows

bubuko.com,布布扣

travelling result follows:

bubuko.com,布布扣

my solution

travel the binary tree with queue from top to down

travel these nodes in queue with stack to get reversed order

visit nodes in every level in stack with stack again

algorithm follows


void _TravelByLevel(node * T)
{
	queue<node *> Q;
	stack<node *> S;
	stack<node *> S2;
	cout<<"travel start"<<endl;
	if(T != NULL)
	{
		Q.push(T);
		Q.push(NULL);
		node * p;

		while(! Q.empty())
		{

			// first action
			p = Q.front();
			Q.pop();
			//_Visit(p);
			S.push(p);

			// second action
			if(p->left != NULL)
				Q.push(p->left);
			if(p->right != NULL)
				Q.push(p->right);

			// third action
			if(Q.front() == NULL)
			{
				Q.pop();
				if(Q.empty())
					break;
				S.push(NULL);
				Q.push(NULL);
				//cout<<"level"<<endl;
			}
		}

		while(! S.empty())
		{
			if(S.top() != NULL)
			{
				S2.push(S.top());
				S.pop();
			}
			else 
			{
				while( ! S2.empty() )
				{
					_Visit(S2.top());
					S2.pop();
				}
				S.pop();
				if(! S.empty())
					cout<<"level"<<endl;			
			}
		}

		// visit root
		_Visit(S2.top());

	}
	cout<<"travel over"<<endl;
	
}

my code

test.cpp

#include<iostream>
#include<queue>
#include<stack>
using namespace std;



class node
{
public:
    int data ;
    node * left ;
    node * right ;
    node()
	{
        data = 0 ;
        left = right = NULL ;
    }
};


void _MakeTree(node * &T,int *data,int length);
void _Insert(node * & T,int data);
void _Visit(node * T);
void _TravelByLevel(node * T);

void _MakeTree(node * &T,int *data,int length)
{
    for(int i=0;i<length;i++)
    {
		_Insert(T,data[i]);
    }   
}


void _Insert(node * & T,int data)
{
    if(T == NULL)
    {
		T = new node();
		T -> data = data;
    }
    else
    {
		node * p = T;
		while(p != NULL)
		{
			if(data == p->data )
			break;
			else if(data < p->data )
			{
				if(p->left != NULL)
				{
					p = p ->left;
				}
				else{
					p->left = new node();
					(p->left) ->data = data;
					break;
				}
			}
			else{
				if(p->right != NULL)
					p = p->right;
				else{
					p->right = new node() ;
					(p->right) ->data = data ;
					break ;
				}
			}
		}
    }
}

void _TravelByLevel(node * T)
{
	queue<node *> Q;
	stack<node *> S;
	stack<node *> S2;
	cout<<"travel start"<<endl;
	if(T != NULL)
	{
		Q.push(T);
		Q.push(NULL);
		node * p;

		while(! Q.empty())
		{

			// first action
			p = Q.front();
			Q.pop();
			//_Visit(p);
			S.push(p);

			// second action
			if(p->left != NULL)
				Q.push(p->left);
			if(p->right != NULL)
				Q.push(p->right);

			// third action
			if(Q.front() == NULL)
			{
				Q.pop();
				if(Q.empty())
					break;
				S.push(NULL);
				Q.push(NULL);
				//cout<<"level"<<endl;
			}
		}

		while(! S.empty())
		{
			if(S.top() != NULL)
			{
				S2.push(S.top());
				S.pop();
			}
			else 
			{
				while( ! S2.empty() )
				{
					_Visit(S2.top());
					S2.pop();
				}
				S.pop();
				if(! S.empty())
					cout<<"level"<<endl;			
			}
		}

		// visit root
		_Visit(S2.top());

	}
	cout<<"travel over"<<endl;
	
}

void _Visit(node * T)
{
	if(T != NULL)
		cout<<T->data<<endl;
	else cout<<"visit NULL"<<endl;
}

int main()
{
    int data[] = {8,6,3,7,2,1,13,12,15,17};

    node * T = NULL;
    _MakeTree(T,data,10);
	_TravelByLevel(T);
    system("pause");
    return 0;
}


travel the binary tree by level 4 ( from down to top and from left to right every level ),布布扣,bubuko.com

travel the binary tree by level 4 ( from down to top and from left to right every level )

标签:面试题

原文地址:http://blog.csdn.net/cqs_experiment/article/details/24965989

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!