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

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

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

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

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

博客时间:2014-5-4;

编程语言:C++ ;

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

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

制图工具:office 2010 ppt;

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

my words

Money can be stolen, but not knowledge. Studying benefits me forever.

problem( from beauty of programming )

travel the binary tree by level, and visit nodes form left to right every level.

make the function:  int TravelByLevel(node * root, int level)

eg: the binary tree follows

the 2 level nodes: 3,7,12,15

my solution

first action: make tree stored with a two-dimensional table like following

then getting nodes of every level becomes very easy.

time cost O(n), n is the numbers node of the binary tree, and this algorithm is faster  than that algorithm with queue and stack which I proposed yesterday.

function follows( we assume the binary tree is not so big, or new more space just ok)

int _TravelByLevel(node * T,int level)
{
	cout<<"travel start level "<<level<<endl;
	// first action
	table * table_tree = new table[length];
	queue<std::pair<node *,int>> Q;

	if(T != NULL)
		Q.push(std::make_pair(T,0));

	// second action
	std::pair<node*,int> p;
	while(! Q.empty())
	{
		// first action: 2.1
		p = Q.front();
		Q.pop();
		((table_tree[p.second]).list)[( (table_tree[p.second]).num)++] = p.first;

		// second action: 2.2
		if((p.first)->left != NULL)
			Q.push(std::make_pair((p.first)->left,p.second+1));
		if((p.first)->right != NULL)
			Q.push(std::make_pair((p.first)->right,p.second+1));
	}

	// third action
	node * p1;

	if( level < length && table_tree[level].num > 0 )
	{
		for(int j =0;j<(table_tree[level]).num;j++)
		{
			p1 = ((table_tree[level]).list)[j];
			cout<<p1->data<<endl;
		}
		cout<<"travel over"<<endl;
		return 1;
	}
	cout<<"travel over"<<endl;
	return 0;
}

program run out:

my code

test.cpp

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

const int length = 100;
//const int Longlength = 10000;

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

class table
{
public:
	int num;
	node ** list;
	table(){
		num = 0;
		list = new node*[length];
	}

};

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

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 ;
				}
			}
		}
    }
}

int _TravelByLevel(node * T,int level)
{
	cout<<"travel start level "<<level<<endl;
	// first action
	table * table_tree = new table[length];
	queue<std::pair<node *,int>> Q;

	if(T != NULL)
		Q.push(std::make_pair(T,0));

	// second action
	std::pair<node*,int> p;
	while(! Q.empty())
	{
		// first action: 2.1
		p = Q.front();
		Q.pop();
		((table_tree[p.second]).list)[( (table_tree[p.second]).num)++] = p.first;

		// second action: 2.2
		if((p.first)->left != NULL)
			Q.push(std::make_pair((p.first)->left,p.second+1));
		if((p.first)->right != NULL)
			Q.push(std::make_pair((p.first)->right,p.second+1));
	}

	// third action
	node * p1;

	if( level < length && table_tree[level].num > 0 )
	{
		for(int j =0;j<(table_tree[level]).num;j++)
		{
			p1 = ((table_tree[level]).list)[j];
			cout<<p1->data<<endl;
		}
		cout<<"travel over"<<endl;
		return 1;
	}
	cout<<"travel over"<<endl;
	return 0;
}

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);
	for(int i= 0;i<5;i++)
		_TravelByLevel(T,i);
    system("pause");
    return 0;
}

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

时间: 2024-10-10 10:55:57

travel the binary tree by level 5 ( from down to top and from left to right every level )的相关文章

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

travel the binary tree by level( from top to down)

个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:travel the binary tree by level( from top to down); 博客时间:2014-5-3; 编程语言:C++ ; 编程坏境:Windows 7 专业版 x64; 编程工具:vs2008 32位编译器; 制图工具:office 2010 ppt; 硬件信息:7G

travel the binary tree by level 2 ( from top to down )

个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:travel the binary tree by level 2 ( from top to down ); 博客时间:2014-5-3; 编程语言:C++ ; 编程坏境:Windows 7 专业版 x64; 编程工具:vs2008 32位编译器; 制图工具:office 2010 ppt; 硬件信

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

travel the binary tree by level 3 ( from down to top ) 个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:travel the binary tree by level 3 ( from down to top ) 博客时间:2014-5-3; 编程语言:C++ ; 编程坏境:Windows 7

LeetCode: Binary Tree Level Order Traversal &amp;&amp; Binary Tree Zigzag Level Order Traversal

Title: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re

Leetcode 103. Binary Tree Zigzag Level Order Traversal

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>>

【树】Binary Tree Zigzag Level Order Traversal

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its