本博客前面文章已对树与二叉树有过简单的介绍,本文主要是重点介绍有关二叉树的一些具体操作与应用

阅读本文前,可以先参考本博客 各种基本算法实现小结(三)—— 树与二叉树   和  各种基本算法实现小结(二)—— 堆 栈

二叉树

深度层数、叶子数、节点数和广度优先算法

以及树的先序、中序、后序的递归与非递归(深度优先)


测试环境:VS2008(C)

#include "stdafx.h" #include <stdlib.h> #include <malloc.h> #define DataType char int d_tree=0; /* tree's depth */ int l_tree=0; /* tree's leaf */ int n_tree=0; /* tree's node */ /**************************************/ /******** 树的结构定义 ********/ /**************************************/ struct _tree { DataType data; struct _tree *lchild; struct _tree *rchild; }; typedef struct _tree tree, *ptree; /**************************************/ /******** 栈的结构定义 ********/ /**************************************/ struct _node { ptree pt; struct _node *next; }; typedef struct _node node, *pnode; struct _stack { int size; pnode ptop; }; typedef struct _stack stack, *pstack; /**************************************/ /******** 堆的结构定义 ********/ /**************************************/ struct _queue { pnode front; pnode rear; }; typedef struct _queue queue, *pqueue; /**************************************/ /******** 栈的数据操作 ********/ /**************************************/ pstack init_stack() { pnode pn=NULL; pstack ps=NULL; pn=(pnode)malloc(sizeof(node)); ps=(pstack)malloc(sizeof(stack)); pn->pt=NULL; pn->next=NULL; ps->ptop=pn; return ps; } int empty_stack(pstack ps) { if(ps->ptop->next==NULL) return 1; else return 0; } void push_stack(pstack ps, ptree pt) /* flag for post tree: 0 for lchild; 1 for rchild */ { pnode pn=NULL; pn=(pnode)malloc(sizeof(node)); pn->pt=pt; pn->next=ps->ptop; ps->ptop=pn; } ptree pop_stack(pstack ps) { ptree pt=NULL; pnode pn=NULL; if(!empty_stack(ps)) { pn=ps->ptop; ps->ptop=ps->ptop->next; pt=pn->pt; free(pn); } return pt; } ptree gettop_stack(pstack ps) { if(!empty_stack(ps)) return ps->ptop->pt; } /**************************************/ /******** 堆的数据操作 ********/ /**************************************/ queue init_queue() { pnode pn=NULL; queue qu; pn=(pnode)malloc(sizeof(node)); pn->pt=NULL; pn->next=NULL; qu.front=qu.rear=pn; /* init: pn is header */ return qu; } int empty_queue(queue &qu) { if(qu.front==qu.rear) return 1; else return 0; } void en_queue(queue &qu, ptree pt) { pnode pn=NULL; pn=(pnode)malloc(sizeof(node)); pn->pt=pt; pn->next=qu.rear->next; qu.rear->next=pn; qu.rear=qu.rear->next; } ptree de_queue(queue &qu) { ptree pt=NULL; pnode pn=NULL; if(!empty_queue(qu)) { pn=qu.front->next; if(pn==qu.rear) /* qu is null: qu.front==qu.rear */ qu.rear=qu.front; else qu.front->next=qu.front->next->next; pt=pn->pt; free(pn); } return pt; } /**************************************/ /******** 树的数据操作 ********/ /**************************************/ ptree init_tree() { ptree pt=NULL; pt=(ptree)malloc(sizeof(tree)); pt->data='0'; pt->lchild=NULL; pt->rchild=NULL; return pt; } ptree create_tree() { char ch; ptree pt=NULL; 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; }

运行结果:

   

======================================

   

===================================================================

转载于:https://www.cnblogs.com/springside4/archive/2010/06/22/2481808.html

树与二叉树的深度优先与广度优先算法(递归与非递归)相关推荐

  1. 树型结构的深度优先和广度优先算法

    构造一个树模型 const tree = {val: 'a',children: [{val: 'b',children: [{val: 'd',children: [{val: 'g',childr ...

  2. python爬虫算法深度优先_爬虫课程(四)|深度优先和广度优先算法

    深度优先和广度优先算法在爬取一个整站上经常用到,本课程主要讲解这两个算法的原理以及使用过程. 一.网站的树结构 1.1.一个网站的url结构图 以知乎为例,知乎目前有发现.话题.Live.书店.圆桌. ...

  3. 深度优先和广度优先算法(例题)

    在LeetCode上面刷题刷到一道二叉树的题,这道题我认为对学习树的深度优先和广度优先算法有一定的帮助,先来看一下题 这道题是这样的: 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返 ...

  4. 图的深度优先和广度优先算法(DFS递归与非递归)

    无向图--邻接矩阵的深度优先和广度优先算法实现 测试环境:VS2008(C) [cpp:showcolumns] view plaincopyprint? ·········10········20· ...

  5. 给定一个n节点的二叉树,写出一个O(n)时间非递归过程,将该树每个节点关键字输出,可以使用一个栈作为辅助数据结构(算法导论第十章10.4-3)

    给定一个n节点的二叉树,写出一个O(n)时间非递归过程,将该树每个节点关键字输出,可以使用一个栈作为辅助数据结构 (算法导论第十章10.4-3) template<typename T> ...

  6. python创建树结构、求深度_数据结构-树以及深度、广度优先遍历(递归和非递归,python实现)...

    前面我们介绍了队列.堆栈.链表,你亲自动手实践了吗?今天我们来到了树的部分,树在数据结构中是非常重要的一部分,树的应用有很多很多,树的种类也有很多很多,今天我们就先来创建一个普通的树.其他各种各样的树 ...

  7. 二叉树创建及遍历算法(递归及非递归)(转)

    //二叉树处理头文件 //包括二叉树的结构定义,二叉树的创建,遍历算法(递归及非递归), /* 作者:成晓旭 时间:2001年10月7日(18:49:38-20:00:00) 内容:完成二叉树创建,二 ...

  8. 树的递归与非递归遍历算法

    树的递归与非递归遍历算法 树的递归与非递归遍历算法 树的遍历 实例 树遍历的口诀 树的递归遍历代码 树的先序遍历 树的中序遍历 树的后序遍历 递归遍历思想 树的非递归遍历 树的先序非递归遍历 先序遍历 ...

  9. 九十五、二叉树的递归和非递归的遍历算法模板

    @Author:Runsen 刷Leetcode,需要知道一定的算法模板,本次先总结下二叉树的递归和非递归的遍历算法模板. 二叉树的四种遍历方式,前中后加上层序遍历.对于二叉树的前中后层序遍历,每种遍 ...

最新文章

  1. 第三章| 3.1文件处理
  2. HashSet中的add()方法( 三 )(详尽版)
  3. dump mysql_mysql/mariadb知识点总结(28):mysql备份工具之mysqldump
  4. js for循环与for in循环的区别
  5. 解决MyBatis的报错 There is no getter for property named ‘*‘ in ‘class java.lang.String‘
  6. cross-domain policy file
  7. 区块链中密码学与安全技术
  8. WebHook入门教程:快速实现自动化运维,如自动热部署、自动重启服务、自动备份数据库等等
  9. win7系统怎么拷贝到u盘_win7/10系统复制文件到u盘提示文件过大怎么办
  10. 二维标准正态分布的matlab方程
  11. 算法系列15天速成——第五天 五大经典查找【中】
  12. QT谷歌拼音输入法的移植
  13. COG注释[Ubuntu 15.10系统]
  14. C#程序设计第三版(李春葆)第12章文件操作课后习题答案
  15. python自学软件-学习python用什么软件
  16. spring test如何设置DebuggingClassWriter.DEBUG_LOCATION_PROPERTY
  17. <XII>无人值守安装脚本
  18. python语音识别推荐_Python 实时语音识别
  19. 基于STM32震动感应灯
  20. LDAP搜索中的CN,OU,DC是什么?

热门文章

  1. python正则匹配ip地址_Python正则表达式匹配和提取IP地址
  2. 华为交换机忘了密码如何恢复
  3. linux java javac版本_linux下java 和 javac version 不一致问题
  4. linux设置双屏拼接_双屏办公,用起来到底有多爽
  5. python xpath爬虫_Python爬虫(2):XPath语法
  6. C++实现输出内容存入到TXT文档中
  7. SQL SERVER while循环
  8. 概率机器人总结——(扩展)卡尔曼滤波先实践再推导
  9. 多视图几何总结——基础矩阵、本质矩阵和单应矩阵的求解过程
  10. tensorflow学习(7. GAN实现MNIST分类)