二叉树中序、后序和先序的递归遍历:
inorder中序遍历:
template<typename NT>
void inorder(NT* root, void (*visit)(NT*))
{
if(root != 0)
{
inorder(root->lc_, visit); //中序遍历左子树;
visit(root); //访问根节点;
inorder(root->rc_, visit); //中序遍历右子树;
}
}
postorder后续遍历:
template<typename NT>
void postorder(NT* root, void (*visit)(NT*))
{
if(root != 0)
{
postorder(root->lc_, visit); //后序遍历左子树;
postorder(root->rc_, visit); //后续遍历右子树;
visit(root); //访问根节点;
}
}
preorder先序遍历:
template<typename NT>
void preorder(NT* root, void (*visit)(NT*))
{
if(root != 0)
{
visit(root); //访问根节点;
preorder(root->lc_, visit); //先序遍历左子树;
preorder(root->rc_, visit); //先序遍历右子树;
}
}
inorder中序遍历:
template<typename NT>
void inorder(NT* root, void (*visit)(NT*))
{
if(root != 0)
{
inorder(root->lc_, visit); //中序遍历左子树;
visit(root); //访问根节点;
inorder(root->rc_, visit); //中序遍历右子树;
}
}
postorder后续遍历:
template<typename NT>
void postorder(NT* root, void (*visit)(NT*))
{
if(root != 0)
{
postorder(root->lc_, visit); //后序遍历左子树;
postorder(root->rc_, visit); //后续遍历右子树;
visit(root); //访问根节点;
}
}
preorder先序遍历:
template<typename NT>
void preorder(NT* root, void (*visit)(NT*))
{
if(root != 0)
{
visit(root); //访问根节点;
preorder(root->lc_, visit); //先序遍历左子树;
preorder(root->rc_, visit); //先序遍历右子树;
}
}
/*************************************************************************/
/***********************************************************************/
levelorder按层遍历方式(levelorder)
template<typename NT>
void levelorder(NT* root, void (*visit)(NT*))
{
if(root == 0)
return;
std::queue<NT*> q;
q.push(root);
while(!q.empty())
{
NT* t = q.front();
q.pop();
if(t->haslc())
q.push(t->lc_);
if(t->hasrc())
q.push(t->rc_);
visit(t);
}
} //~void levelorder(...)
template<typename NT>
void levelorder(NT* root, void (*visit)(NT*))
{
if(root == 0)
return;
std::queue<NT*> q;
q.push(root);
while(!q.empty())
{
NT* t = q.front();
q.pop();
if(t->haslc())
q.push(t->lc_);
if(t->hasrc())
q.push(t->rc_);
visit(t);
}
} //~void levelorder(...)
/****************************************************************/
/********************************************************/
inorder,preorder,postorder,levelorder函数的应用例子:
#include <iostream>
template<typename NT>
void print(NT* p)
{
std::cout<< p->value()<< " ";
}
int main(void)
{
typedef Btnode<char> NT;
NT i('I', 0, 0); NT h('H', 0, 0);
NT f('F', &h, &i); NT g('G', 0, 0);
NT e('E', &g, 0); NT d('D', 0, 0);
NT c('C', 0, &f); NT b('B', &d, &e);
NT a('A', &b, &c); NT* root = &a;
inorder(root,&print<NT>); cout<< endl;
preorder(root,&print<NT>); cout<< endl;
postorder(root,&print<NT>); cout<< endl;
levelorder(root,&print<NT>); cout<< endl;
cout<<" height= "<<height(root)<<endl;
cout<<"number of nodes= "<<size(root)<<endl;
return 0;
}
#include <iostream>
template<typename NT>
void print(NT* p)
{
std::cout<< p->value()<< " ";
}
int main(void)
{
typedef Btnode<char> NT;
NT i('I', 0, 0); NT h('H', 0, 0);
NT f('F', &h, &i); NT g('G', 0, 0);
NT e('E', &g, 0); NT d('D', 0, 0);
NT c('C', 0, &f); NT b('B', &d, &e);
NT a('A', &b, &c); NT* root = &a;
inorder(root,&print<NT>); cout<< endl;
preorder(root,&print<NT>); cout<< endl;
postorder(root,&print<NT>); cout<< endl;
levelorder(root,&print<NT>); cout<< endl;
cout<<" height= "<<height(root)<<endl;
cout<<"number of nodes= "<<size(root)<<endl;
return 0;
}
/****************************************************/
后续递归遍历应用:求二叉树的高度和节点个数;
template<typename NT>
int height(NT const* root)
{
if(root == 0)
return 0;
int ld = height(root->lc_);
int rd = height(root->rc_);
return 1 + std::max(ld, rd);
}
template<typename NT>
int size(NT const* root)
{
if(root == 0)
return 0;
return 1 + size(root->lc_)+ size(root->rc_);
}
template<typename NT>
int height(NT const* root)
{
if(root == 0)
return 0;
int ld = height(root->lc_);
int rd = height(root->rc_);
return 1 + std::max(ld, rd);
}
template<typename NT>
int size(NT const* root)
{
if(root == 0)
return 0;
return 1 + size(root->lc_)+ size(root->rc_);
}