非递归遍历二叉树

来源:互联网 发布:淘宝解苹果id锁被骗 编辑:程序博客网 时间:2024/06/06 03:30
#include<iostream>
#include<cassert>
#include<cstdlib>
#include<queue>
#include<stack>
using namespace std;
template<class T>
struct TreeNode
{
T _data;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(const T& t=T())
:_data(t)
,left(NULL)
,right(NULL)
{}
};
template<class T>
class Tree
{
typedef TreeNode<T> Node;
public:
Tree()
:_root(NULL)
{}
Tree(T *a,size_t size, const T& invalid)
{
size_t index=0;
_root=CreatNode(a,size,index,invalid);
}
  
Tree<T>& operator=(const Tree<T>& t)
{
if(this!=&t)
    std::swap(_root,t._root );
return *this;  
}
void PrevOrder()
{
_PrevOrder(_root);
cout<<endl;
}
void InOrder()
{
_InOrder(_root);
    cout<<endl;
}
void PostOrder()
{
_PostOrder(_root);
cout<<endl;
}
size_t Size()
{
return _Size(_root);
}
size_t Depth()
{
return _Depth(_root);
}
void Find(const T& x)
{
Node* ret=_Find(_root,x);
cout<<ret->_data;
}
void LevelOrder()
{
queue<Node*> q;
Node* cur=_root;
if(cur)
q.push(cur);
while(!q.empty())
{
cur=q.front();
cout<<cur->_data<<" ";
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
q.pop();
}
  cout<<endl;
}
void PrevOrder_NonR()
{
stack<Node*> s;
Node* cur=_root;
while(cur||!s.empty())
{
while(cur)
{
s.push(cur);
    cout<<cur->_data<<" ";
    cur=cur->left;
}
Node* top=s.top();
s.pop();
cur=top->right;
}
      cout<<endl;
}
void InOrder_NonR()
{
stack<Node*> s;
Node* cur=_root;
while(cur||!s.empty())
{
while(cur)
{
s.push(cur);
    cur=cur->left;
}
Node* top=s.top();
cout<<top->_data<<" ";
s.pop();
cur=top->right;
}
      cout<<endl;
}
void PostOrder_NonR()
{
}
protected:
Node* CreatNode(const T* a,size_t size,size_t& index,const T& invalid)
{
 assert(a);  
Node *root=NULL;
if((index<size)&&(a[index]!=invalid))
{
root = new Node(a[index]);
root->left=CreatNode(a,size,++index,invalid);
root->right=CreatNode(a,size,++index,invalid);

}
    return root;
}
void _PrevOrder(Node* root)
{
if(root==NULL)
return;
cout<<root->_data <<" ";
    _PrevOrder(root->left);
_PrevOrder(root->right);
}
void _InOrder(Node* root)
{
if(root==NULL)
return;
_InOrder(root->left);
cout<<root->_data<<" ";
    _InOrder(root->right);
}
void _PostOrder(Node* root)
{
if(root==NULL)
return;
_PostOrder(root->left);
_PostOrder(root->right);
cout<<root->_data<<" ";
}
void _LevelOrder(Node* root)
{
queue<Node*> q;
Node* cur=_root;
if(root==NULL)
return;
if(cur)
q.push(cur);
while(q.empty())
{
cur=q.front();
cout<<cur->_data<<" ";
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
q.pop();
}
  cout<<endl;
}
size_t _Size(Node* root)
{
if(root==NULL)
return 0;
return _Size(root->left)+_Size(root->right)+1;
}
size_t _Depth(Node* root)
{
if(root==NULL)
return 0;
else
return _Depth(root->left)>_Depth(root->right)?_Depth(root->left)+1:_Depth(root->right)+1;
}
Node* _Find(Node* root,const T& x)
{
if(root==NULL)
return NULL;
if(root->_data==x)
return root;
    Node* ret=_Find(root->left,x);
if(ret==NULL)
ret=_Find(root->right,x);
return ret;
}

private:
Node* _root;
};
void test()
{
int a1[10]={1,2,3,'#','#',4,'#','#',5,6};
Tree<int> t1 (a1,10,'#');
//t1.PostOrder();
/*size_t a=t1.Depth();
cout<<a;*/
//t1.Find(3);
//t1.LevelOrder();
//t1.PrevOrder_NonR();
t1.InOrder_NonR();

}
int main()
{
test();
system("pause");
return 0;


}

0 0