树的遍历
来源:互联网 发布:mac yy服务器连接错误 编辑:程序博客网 时间:2024/06/04 22:07
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stack>
using std::stack;
template<typename T>
struct Node
{
Node(T value):mValue(value),mLeftChild(0),mRightChild(0)
{
}
T mValue;
Node *mLeftChild;
Node *mRightChild;
};
/* build a tree which is
1
/ \
2 3
/ \ / \
4 5 6 7
*/
Node<int>*
buildTree()
{
Node<int> *n1 = new Node<int>(1);
Node<int> *n2 = new Node<int>(2);
Node<int> *n3 = new Node<int>(3);
Node<int> *n4 = new Node<int>(4);
Node<int> *n5 = new Node<int>(5);
Node<int> *n6 = new Node<int>(6);
Node<int> *n7 = new Node<int>(7);
n1->mLeftChild = n2;
n1->mRightChild = n3;
n2->mLeftChild = n4;
n2->mRightChild = n5;
n3->mLeftChild = n6;
n3->mRightChild = n7;
return n1;
}
template<typename T>
void
bfsTravesal(Node<T> *root)
{
deque<Node<T> *> result;
result.push_back(root);
while( result.size() )
{
Node<T> *head = result.front();
result.pop_front();
cout << head->mValue<<" ";
if ( head->mLeftChild )
{
result.push_back(head->mLeftChild);
}
if ( head->mRightChild )
{
result.push_back(head->mRightChild);
}
}
}
template<typename T>
void
preOrderTraversal(Node<T> *node)
{
if ( 0 == node )
return ;
cout << node->mValue << " ";
preOrderTraversal(node->mLeftChild);
preOrderTraversal(node->mRightChild);
}
template<typename T>
void
inOrderTraversal(Node<T> *node)
{
if (0 == node)
{
return ;
}
inOrderTraversal(node->mLeftChild);
cout << node->mValue << " ";
inOrderTraversal(node->mRightChild);
}
template<typename T>
void
postOrderTraversal(Node<T> *node)
{
if (0 == node)
{
return ;
}
postOrderTraversal(node->mLeftChild);
postOrderTraversal(node->mRightChild);
cout << node->mValue << " ";
}
template<typename T>
void
preOrderNoneRecursiveTravesal (Node<T> *node)
{
stack<Node<T> *> s;
while (node!=NULL||!s.empty())
{
if ( 0 != node )
{
cout << node->mValue << " ";
s.push(node);
node = node->mLeftChild;
}
else
{
node = s.top();
node = node->mRightChild;
s.pop();
}
}
}
template<typename T>
void
inOrderNoneRecursiveTravesal (Node<T> *node)
{
stack<Node<T> *> s;
Node<T> *currentNode = node;
while (currentNode!=NULL || !s.empty())
{
while (node)
{
s.push(node);
node = node->mLeftChild;
}
if( ! s.empty() )
{
node = s.top();
s.pop();
cout << node->mValue<<" ";
node = node ->mRightChild;
}
}
}
template<typename T>
void
postOrderTraversal (Node<T> *node)
{
}
int main(int argc,char *argv[])
{
Node<int> *root = buildTree();
inOrderNoneRecursiveTravesal(root);
cout << "inOrderNoneRecursiveTravesal finish "<<endl;
preOrderNoneRecursiveTravesal(root);
cout << "preOrderNoneRecursiveTravesal finish "<<endl;
bfsTravesal(root);
cout << "bfs end"<<endl<<endl;
preOrderTraversal(root);
cout << "preOrderTraversal"<<endl<<endl;
inOrderTraversal(root);
cout << "inOrderTraversal"<<endl<<endl;
postOrderTraversal(root);
cout << "postOrderTraversal"<<endl<<endl;
return 0;
}
- 二叉树的遍历(层遍历和深度遍历)
- 二叉树的前序中序后序遍历,非递归遍历 层次遍历
- 二叉树的先中后序遍历,递归遍历,非递归遍历
- 二叉树遍历-----前序后序迭代遍历的新思路
- 二叉树的遍历-按层次遍历
- 二叉树的遍历(递归遍历)
- 二叉树的遍历-层次遍历
- 树的遍历
- 六、 树的遍历
- 二叉树的遍历
- haffman树的遍历
- 二叉树的遍历
- 树的各种遍历
- 树的遍历
- 二叉树的遍历
- C++树的遍历
- Java树的遍历
- 树的简单遍历
- 修改Python IDLE代码配色及语法高亮主题
- Android智能指针SP WP使用方法介绍
- Java-Regex-识别并转换成HTML可点击的超链接
- Android 控件最上层显示方法
- POJ 2239 Selecting Courses (二分匹配)
- 树的遍历
- 原创:servlet技术实现用户名唯一的验证处理详解
- Android实现获取系统应用列表
- Java——创建简单的用户界面
- 1041. Be Unique
- Firmware 加载原理分析
- Java初始化次序
- DB2数据库备份还原
- 20130313日报