# 【数据结构】二叉树的遍历

#include
using namespace std;

template
class TreeNode
{
protected:
TreeNode* left;
TreeNode* right;
public:
T data;

TreeNode (const T& item, TreeNode *lptr,TreeNode *rptr):
data(item), left(lptr), right(rptr)    {}
virtual ~TreeNode() {}
TreeNode* Left() const    {
return left;
}
TreeNode* Right() const {
return right;
}

//递归遍历
void Preorder(TreeNode *const node )const ;
void Inorder(TreeNode  *const node )const ;
void Postorder(TreeNode *const node)const;

void Preorder() {
Preorder(this);
}
void Inorder() {
Preorder(this);
}
void Postorder() {
Preorder(this);
}
//非递归遍历
void PreTraverse(TreeNode *const node )const ;
void InTraverse(TreeNode  *const node )const ;
void PostTraverse(TreeNode *const node)const;

void PreTraverse() {
PreTraverse(this);
}
void InTraverse() {
InTraverse(this);
}
void PostTraverse() {
PostTraverse(this);
}
};

// preorder recursive scan of the nodes in a tree
template
void TreeNode::Preorder (TreeNode *const t) const
{
if (t != NULL) {
cout << t->data << " "; // visit the node
Preorder (t->Left());   // descend left
Preorder (t->Right());  // descend right
}
}

// inorder recursive scan of the nodes in a tree
template
void TreeNode::Inorder (TreeNode*const t) const
{
if (t != NULL) {
Inorder (t->Left());    // descend left
cout << t->data << " ";                        // visit the node
Inorder (t->Right());  // descend right
}
}

// postorder recursive scan of the nodes in a tree
template
void TreeNode::Postorder (TreeNode *const t) const
{
if (t != NULL) {
Postorder (t->Left());  // descend left
Postorder(t->Right()); // descend right
cout << t->data << " "; // visit the node
}
}

template
void TreeNode::PreTraverse(TreeNode *const node ) const
{
if (node == NULL)
return;
TreeNode *t = node;
stack< TreeNode* > s;
s.push(t);
while (! s.empty()) {
t = s.top();
s.pop();
cout << t->data << " ";
if(t->right != NULL)
s.push(t->right);
if(t->left != NULL)
s.push(t->left);
}
}
template
void TreeNode::InTraverse(TreeNode *const node ) const
{
if (node == NULL)
return;
TreeNode *t = node;
stack< TreeNode* > s;
do {
while(t != NULL) {
s.push(t);
t = t->left;
}
t = s.top();
s.pop();
cout << t->data << " ";
t = t->right;
} while (!s.empty()|| t!=NULL);
}
template
void TreeNode::PostTraverse(TreeNode *const node ) const
{
stack<TreeNode* > s;
stack sf;
TreeNode *t = node;
int flag;
while (!s.empty() || t!=NULL){
while (t!=NULL){
s.push(t);
sf.push(0);
t = t->left;
}
t = s.top(),s.pop();
flag = sf.top(),sf.pop();
if (flag == 0) {
s.push(t);
sf.push(1);
t =  t->right;
}else {
cout << t->data << " ";
t = NULL;
}
}
}一个简单的测试程序
1 int main()
2 {
3     TreeNode<int> n1(10,NULL,NULL);
4     TreeNode<int> n2(9,NULL,NULL);
5     TreeNode<int> n3(6,&n1,&n2);
6     TreeNode<int> n4(7,NULL,NULL);
7     TreeNode<int> n5(8,NULL,NULL);
8     TreeNode<int> n6(3,&n4,&n5);
9     TreeNode<int> n7(1,&n3,&n6);
10
11     n7.PreTraverse(); cout << endl;
12     n7.InTraverse();  cout << endl;
13     n7.PostTraverse();cout << endl;
14
15     system("pause");
16     return 0;
17 }

posted @ 2012-07-17 21:03  Keosu  阅读(389)  评论(0编辑  收藏  举报