【0】README
0.1)本代码均为原创,旨在将树的遍历应用一下下以加深印象而已;(回答了学习树的遍历到底有什么用的问题?)你对比下linux 中的文件树 和我的打印结果就明理了;
0.2)我们采用的是 儿子兄弟表示法 来 表示树的整体节点构造;
0.3)儿子兄弟表示法介绍
0.3.1)如下图所示: 向下的箭头(左指针)指向第一个儿子节点, 从左到右的箭头(右指针)指向下一个兄弟节点;(间接说明了树的节点有两个指针)
0.3.2)树节点定义代码如下:

struct Tree;
typedef struct Tree *Tree;// we adopt child-sibling notation
struct Tree
{ElementType value;Tree firstChild;Tree nextSibling;
};
0.4)哥子第一次 使用着 丑到逼爆 的 编辑器,也是醉了,主要是markdown 对于源代码文件显示不够清晰, oh m g;
【1】任务来了
我们想要列出目录中所有文件的名字, 我们的输出格式将是:深度为 depth 的文件的名字将被 depth 次跳格缩进后打印出来;
【2】给出先序遍历+后序遍历目录树的实现代码
2.1)先序遍历步骤:
step1)访问根节点;
step2)先序遍历以儿子为根的子树;
step3)先序遍历以兄弟为根的子树;
download source code:https://github.com/pacosonTang/dataStructure-algorithmAnalysis/blob/master/chapter4/p68_preorder_common_tree.c
source code at a glance:
#include <stdio.h>
#include <malloc.h>#define ElementType char
#define Error(str) printf("\n error: %s \n",str)   struct Tree;
typedef struct Tree *Tree;Tree createTree();
Tree makeEmpty(Tree t);
Tree insert(ElementType e, Tree t);// we adopt child-sibling notation
struct Tree
{ElementType value;Tree firstChild;Tree nextSibling;
};// create a tree with root node
Tree createTree()
{   Tree t;t = (Tree)malloc(sizeof(struct Tree));if(!t) {Error("out of space, from func createTree");        return NULL;}    t->firstChild = NULL;t->nextSibling = NULL;    t->value = '/';return t;
}// make the tree empty
Tree makeEmpty(Tree t)
{if(t){makeEmpty(t->firstChild);makeEmpty(t->nextSibling);        free(t);}           return NULL;
}//
Tree insert(ElementType e, Tree parent)
{Tree child;Tree newSibling;if(!parent){Error("for parent tree node is empty , you cannot insert one into the parent node, from func insert");        return NULL;}newSibling = (Tree)malloc(sizeof(struct Tree));if(!newSibling) {Error("out of space, from func insert");        return NULL;}newSibling->value = e;newSibling->nextSibling = NULL;newSibling->firstChild = NULL;// building the node with value e overchild = parent->firstChild;   if(!child) {parent->firstChild = newSibling;return parent;}while(child->nextSibling)child = child->nextSibling; // find the last child of parent nodechild->nextSibling = newSibling;return parent;
}// find the tree root node with value equaling to e
Tree find(ElementType e, Tree root)
{Tree temp;if(root == NULL)return NULL;if(root->value == e)return root;temp = find(e, root->firstChild); if(temp) return temp;elsereturn     find(e, root->nextSibling);
}// analog print directories and files name in the tree, which involves preorder traversal.
void printPreorder(int depth, Tree root)
{           int i;if(root) {        for(i = 0; i < depth; i++)printf("    ");printf("%c\n", root->value);          printPreorder(depth + 1, root->firstChild);                                 printPreorder(depth, root->nextSibling);}
}int main()
{Tree tree;tree = createTree();printf("\n test for insert 'A' 'B' into the parent '/' and 'C' 'D' into the parent 'A' \n");  insert('A', tree);    insert('B', find('/', tree));   insert('C', find('A', tree));insert('D', find('A', tree));printPreorder(1, tree);printf("\n test for insert 'E' 'F' into the parent '/'  \n");  insert('E', find('/', tree));insert('F', find('/', tree));printPreorder(1, tree);printf("\n test for insert 'G' 'H' into the parent 'E' and 'I' into the parent 'H' and even 'J' 'K' into the parent 'I' \n");    insert('G', find('E', tree));insert('H', find('E', tree));insert('I', find('H', tree));insert('J', find('I', tree));insert('K', find('I', tree));printPreorder(1, tree);return 0;
}
打印结果如下:
2.2)后序遍历步骤:(不同于二叉树的后序)
step1)后序遍历以儿子为根的子树;
step2)访问根节点;
step3)后序遍历以兄弟为根的子树;
download source code:https://github.com/pacosonTang/dataStructure-algorithmAnalysis/blob/master/chapter4/p69_postorder_commone_tree.c

source code at a glance:
#include <stdio.h>
#include <malloc.h>#define ElementType char
#define Error(str) printf("\n error: %s \n",str)   struct Tree;
typedef struct Tree *Tree;Tree createTree();
Tree makeEmpty(Tree t);
Tree insert(ElementType e, Tree t);// we adopt child-sibling notation
struct Tree
{ElementType value;Tree firstChild;Tree nextSibling;
};// create a tree with root node
Tree createTree()
{   Tree t;t = (Tree)malloc(sizeof(struct Tree));if(!t) {Error("out of space, from func createTree");        return NULL;}    t->firstChild = NULL;t->nextSibling = NULL;    t->value = '/';return t;
}// make the tree empty
Tree makeEmpty(Tree t)
{if(t){makeEmpty(t->firstChild);makeEmpty(t->nextSibling);        free(t);}           return NULL;
}//
Tree insert(ElementType e, Tree parent)
{Tree child;Tree newSibling;if(!parent){Error("for parent tree node is empty , you cannot insert one into the parent node, from func insert");        return NULL;}newSibling = (Tree)malloc(sizeof(struct Tree));if(!newSibling) {Error("out of space, from func insert");        return NULL;}newSibling->value = e;newSibling->nextSibling = NULL;newSibling->firstChild = NULL;// building the node with value e overchild = parent->firstChild;   if(!child) {parent->firstChild = newSibling;return parent;}while(child->nextSibling)child = child->nextSibling; // find the last child of parent nodechild->nextSibling = newSibling;return parent;
}// find the tree root node with value equaling to e
Tree find(ElementType e, Tree root)
{Tree temp;if(root == NULL)return NULL;if(root->value == e)return root;temp = find(e, root->firstChild); if(temp) return temp;elsereturn     find(e, root->nextSibling);
}// analog print directories and files name in the tree, which involves postorder traversal.
void printPostorder(int depth, Tree root)
{           int i;if(root) {                        printPostorder(depth + 1, root->firstChild);                                            for(i = 0; i < depth; i++)printf("    ");       printf("%c\n", root->value);           printPostorder(depth, root->nextSibling);}
}int main()
{Tree tree;tree = createTree();printf("\n ====== test for postordering the common tree presented by child_sibling structure  ====== \n");    printf("\n test for insert 'A' 'B' into the parent '/' and 'C' 'D' into the parent 'A' \n");  insert('A', tree);    insert('B', find('/', tree));   insert('C', find('A', tree));insert('D', find('A', tree));printPostorder(1, tree);printf("\n test for insert 'E' 'F' into the parent '/'  \n"); insert('E', find('/', tree));insert('F', find('/', tree));printPostorder(1, tree);printf("\n test for insert 'G' 'H' into the parent 'E' and 'I' into the parent 'H' and even 'J' 'K' into the parent 'I' \n");   insert('G', find('E', tree));insert('H', find('E', tree));insert('I', find('H', tree));insert('J', find('I', tree));insert('K', find('I', tree));printPostorder(1, tree);return 0;
} 

打印结果如下:

利用树的先序和后序遍历打印os中的目录树相关推荐

  1. 手动创建一棵二叉树,然后利用前序、中序、后序、层序进行遍历(从创建二叉树到各种方式遍历)(含运行结果)

    手动创建一棵二叉树,然后利用前序.中序.后序.层序进行遍历 import java.util.LinkedList; import java.util.List; import java.util.Q ...

  2. 把一个数组的值存入二叉树中,然后利用前序、中序、后序3种方式进行遍历(完整代码以及运行结果)(Java)

    把一个数组的值存入二叉树中,然后利用前序.中序.后序3种方式进行遍历(完整代码以及运行结果) 在最近的面试过程中,听说有小伙伴被面试官要求创建二叉树,然后对该二叉树进行遍历,感觉这一直以来都是一个大家 ...

  3. 【练习】树(Tree, UVa 548)给一棵点带权(权值各不相同)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小。

    给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...

  4. c++ 树的先序、中序和后序的非递归实现(附完整源码)

    树的先序.中序和后序的非递归实现 一.先序遍历 1.递归算法 2.非递归算法 二.中序遍历 1.递归算法 2.非递归算法 三.后序遍历 1.递归算法 2.非递归算法 一.先序遍历 1.递归算法 str ...

  5. java二叉树合并_Java(树的前中后序遍历构造二叉树题型整合)前序和中序、中序和后序、前序和后序遍历序列构造二叉树算法整合归纳...

    前言 二叉树各种花里胡哨的算法题真的把我搞晕了,今天特地整理出一类有关二叉树的算法题,希望能帮助阅读到此文章的人,今后不再受此类题型的困扰. 一.题目类型 已知二叉树的两种遍历序列,请根据该序列构建二 ...

  6. 【数据结构笔记26】根据一棵树的先序/中序遍历Push与Pop内容,输出这棵树的先序、中序、后序遍历数组(不需要真的建立出树)

    本次笔记内容: 练习题-TTA.1 题意理解 练习题-TTA.2 核心算法 文章目录 题意理解 根据Push与Pop直接得出先序.中序数组 根据pre和in生成post C实现 题意理解 先来回忆非递 ...

  7. C语言利用二叉树的操作实现根据给定的字符串生成二叉树并前序、中序、后序输出二叉树。

    C语言利用二叉树的操作实现根据给定的字符串生成二叉树并前序.中序.后序输出二叉树. Description 根据给定的字符串生成二叉树并前序.中序.后序此二叉树. Input 给定一字符串,其中#表示 ...

  8. Suzy找到实习了吗Day 18 | 二叉树进行中:513 找树左下角的值,112 路径总和 ,106.从中序与后序遍历序列构造二叉树

    513 找树左下角的值 solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, val ...

  9. 树-树的遍历(先序、中序、后序)

    树的遍历 树的遍历方式主要分为四种,先序.中序.后序和层序,在这篇博客中我将仔细介绍一下树的这四种遍历方式. 先序遍历 先序遍历,也叫先根遍历.前序遍历,首先访问根结点然后遍历左子树,最后遍历右子树. ...

最新文章

  1. 人工智能下一个前沿:可解释性
  2. 40种为网页设计师准备的高品质和免费的多媒体PSD文件
  3. (七)boost库之单例类
  4. 【slighttpd】基于lighttpd架构的Server项目实战(4)—简单的echo服务器
  5. c# html文件转换word,C#实现word转换成html文档 源码
  6. python中list index out of range_Python知识精解:str split()方法
  7. amp sqlserver中 什么意思_股票术语中的做空到底是什么意思?
  8. 获取Django中model字段名 字段的verbose_name
  9. (一) js + Vue 写扫雷
  10. HALCON学习笔记 1
  11. HD TUNE 下载使用
  12. digester_Apache Digester示例–轻松配置
  13. JavaScript 如何求两个数的最小公倍数
  14. 中国石油大学《机械原理》第二次在线作业
  15. SeedLab1: Sniffing Spoofing Lab
  16. 三次Bezier曲线/B样条曲线转换成隐函数方程的方法
  17. 猿辅导9-12编程题3道
  18. 高等数学笔记-乐经良老师-第四章-微分中值定理和导数的应用-第五节-曲线的曲率
  19. 详解python中readlines函数的参数hint
  20. git push 提示当前分支没有对应的远程分支?

热门文章

  1. 【PKUWC2018】随机游走【Min-Max容斥】【树形dp】【FWT】
  2. Acwing 236. 格鲁吉亚和鲍勃(博弈论妙题)
  3. I - Triple HDU - 5517
  4. 武汉工程大学2020GPLT选拔赛(上)
  5. YBTOJ洛谷P3209:平面图判定(2-SAT)
  6. 牛客国庆集训派对day6TJ-DefenseTower【贪心】
  7. ssl初一组周六模拟赛【2018.4.7】
  8. Codeforces Round #674 (Div. 3)
  9. 【DP】方格计数(nowcoder 20107-B)
  10. 洛谷 P1967货车运输 并查集+贪心 不需要用LCA!