树与二叉树的深度优先与广度优先算法(递归与非递归)
本博客前面文章已对树与二叉树有过简单的介绍,本文主要是重点介绍有关二叉树的一些具体操作与应用
阅读本文前,可以先参考本博客
各种基本算法实现小结(三)—— 树与二叉树
和
各种基本算法实现小结(二)—— 堆 栈
二叉树
深度层数、叶子数、节点数和广度优先算法
以及树的先序、中序、后序的递归与非递归(深度优先)
测试环境:VS2008(C)
scanf("%c", &ch);
getchar();
if(ch==' ')
return NULL;
else
{
pt=(ptree)malloc(sizeof(tree));
pt->data=ch;
pt->lchild=create_tree();
pt->rchild=create_tree();
}
return pt;
}
int depth_tree(ptree pt)
{
int l_len, r_len;
ptree p=pt;
if(p==NULL)
return 0;
else
{
l_len=depth_tree(p->lchild);
r_len=depth_tree(p->rchild);
return l_len>r_len?(l_len+1):(r_len+1);
}
}
void leaf_tree(ptree pt)
{
ptree p=pt;
if(p->lchild==NULL && p->rchild==NULL)
l_tree++;
else
{
leaf_tree(p->lchild);
leaf_tree(p->rchild);
}
}
void node_tree(ptree pt)
{
ptree p=pt;
if(p!=NULL)
{
n_tree++;
node_tree(p->lchild);
node_tree(p->rchild);
}
}
void print_pretree(ptree pt)
{
if(pt!=NULL)
{
printf("%3c", pt->data);
print_pretree(pt->lchild);
print_pretree(pt->rchild);
}
}
void print_pretree2(ptree pt)
{
pstack ps=NULL;
ptree p=NULL;
ps=init_stack();
p=pt;
while(p!=NULL || !empty_stack(ps))
{
while(p!=NULL)
{
printf("%3c", p->data);
push_stack(ps, p);
p=p->lchild;
}
if(!empty_stack(ps))
{
p=pop_stack(ps);
p=p->rchild;
}
}
}
void print_midtree(ptree pt)
{
if(pt!=NULL)
{
print_midtree(pt->lchild);
printf("%3c", pt->data);
print_midtree(pt->rchild);
}
}
void print_midtree2(ptree pt)
{
pstack ps=NULL;
ptree p=NULL;
ps=init_stack();
p=pt;
while (p!=NULL || !empty_stack(ps))
{
while(p!=NULL)
{
push_stack(ps, p);
p=p->lchild;
}
if(!empty_stack(ps))
{
p=pop_stack(ps);
printf("%3c", p->data);
p=p->rchild;
}
}
}
void print_posttree(ptree pt)
{
if(pt!=NULL)
{
print_posttree(pt->lchild);
print_posttree(pt->rchild);
printf("%3c", pt->data);
}
}
void print_posttree2(ptree pt)
{
pstack ps=NULL;
ptree p=NULL;
ptree p2=NULL;
ptree lastvisit=NULL;
ps=init_stack();
p=pt;
while (p!=NULL || !empty_stack(ps))
{
while(p!=NULL)
{
push_stack(ps, p);
p=p->lchild;
}
p2=gettop_stack(ps); /* top: rchild==null or sub_root */
if(p2->rchild==NULL || p2->rchild==lastvisit)
{
printf("%3c", p2->data);
lastvisit=pop_stack(ps); /* pop */
}
else
p=p2->rchild;
}
}
void bfs_tree(ptree pt)
{
ptree p=NULL;
queue qu;
qu=init_queue();
p=pt;
if(p!=NULL)
{
en_queue(qu, p);
while (!empty_queue(qu))
{
p=de_queue(qu);
printf("%3c", p->data);
if(p->lchild!=NULL)
en_queue(qu, p->lchild);
if(p->rchild!=NULL)
en_queue(qu, p->rchild);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
ptree pt=NULL;
/*pt=init_tree();*/
printf("Create tree:/n");
pt=create_tree();
/************ recursion ************/
printf("/n/nrecursion...");
printf("/npre tree.../n");
print_pretree(pt);
printf("/nmid tree.../n");
print_midtree(pt);
printf("/npost tree.../n");
print_posttree(pt);
/************ stack ************/
printf("/n/nstack, non recursion:");
printf("/npre tree.../n");
print_pretree2(pt);
printf("/nmid tree.../n");
print_midtree2(pt);
printf("/npost tree.../n");
print_posttree2(pt);
/********** bfs tree **********/
printf("/n/nbfs_tree:/n");
bfs_tree(pt);
/********* tree operation ********/
printf("/n/ntree operation:");
d_tree=depth_tree(pt);
leaf_tree(pt);
node_tree(pt);
printf("/ntree's depth: %d", d_tree);
printf("/ntree's leaf: %d", l_tree);
printf("/ntree's node: %d", n_tree);
printf("/n");
return 0;
}
运行结果:
======================================
===================================================================
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2010-06-22 16:12:52
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!