【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)先序遍历以兄弟为根的子树;
 
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 over child = parent->firstChild;
if(!child) {
parent->firstChild = newSibling;
return parent;
} while(child->nextSibling)
child = child->nextSibling; // find the last child of parent node
child->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;
else
return 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 = ; i < depth; i++)
printf(" ");
printf("%c\n", root->value);
printPreorder(depth + , 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(, tree); printf("\n test for insert 'E' 'F' into the parent '/' \n");
insert('E', find('/', tree));
insert('F', find('/', tree));
printPreorder(, 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(, tree); return ;
}
 
打印结果如下:
 
2.2)后序遍历步骤:(不同于二叉树的后序)
step1)后序遍历以儿子为根的子树; 
step2)访问根节点;
step3)后序遍历以兄弟为根的子树;
 
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 over child = parent->firstChild;
if(!child) {
parent->firstChild = newSibling;
return parent;
} while(child->nextSibling)
child = child->nextSibling; // find the last child of parent node
child->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;
else
return 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 + , root->firstChild);
for(i = ; 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(, tree); printf("\n test for insert 'E' 'F' into the parent '/' \n");
insert('E', find('/', tree));
insert('F', find('/', tree));
printPostorder(, 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(, tree); return ;
}
 
打印结果如下:

利用树的先序和后序遍历打印 os 中的目录树的更多相关文章

  1. 树的三种DFS策略(前序、中序、后序)遍历

    之前刷leetcode的时候,知道求排列组合都需要深度优先搜索(DFS), 那么前序.中序.后序遍历是什么鬼,一直傻傻的分不清楚.直到后来才知道,原来它们只是DFS的三种不同策略. N = Node( ...

  2. 二叉排序树的构造 && 二叉树的先序、中序、后序遍历 && 树的括号表示规则

    二叉排序树的中序遍历就是按照关键字的从小到大顺序输出(先序和后序可没有这个顺序) 一.以序列 6 8 5 7 9 3构建二叉排序树: 二叉排序树就是中序遍历之后是有序的: 构造二叉排序树步骤如下: 插 ...

  3. JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)

    import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; ...

  4. ZT 二叉树先序,中序,后序遍历非递归实现

    二叉树先序,中序,后序遍历非递归实现 分类: 数据结构及算法2012-04-28 14:30 8572人阅读 评论(6) 收藏 举报 structc 利用栈实现二叉树的先序,中序,后序遍历的非递归操作 ...

  5. Java实现二叉树的先序、中序、后序、层序遍历(递归和非递归)

    二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易 ...

  6. 【数据结构与算法】二叉树的 Morris 遍历(前序、中序、后序)

    前置说明 不了解二叉树非递归遍历的可以看我之前的文章[数据结构与算法]二叉树模板及例题 Morris 遍历 概述 Morris 遍历是一种遍历二叉树的方式,并且时间复杂度O(N),额外空间复杂度O(1 ...

  7. 二叉树的前序和中序得到后序 hdu1710

    今天看学长发过来的资料上面提到了中科院机试会有一个二叉树的前序中序得到后序的题目.中科院的代码编写时间为一个小时,于是在七点整的时候我开始拍这个题目.这种类型完全没做过,只有纸质实现过,主体代码半个小 ...

  8. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...

  9. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. 组合模式Composite Pattern(转)

    什么是组合模式呢?简单来说组合模式就是将对象合成树形结构以表示“部分整体”的层次结构,组合模式使用户对单个对象和组合对象使用具有一致性. 组合模式(Composite Pattern)有时候又叫部分- ...

  2. Qualcomm 專業名詞

    APSS Application processor subsystem software BAM Bus Access Manager blsp BAM low speed peripheral T ...

  3. JS与jquery书写插件规范

    什么是封装呢? 我的理解就是 把一个功能单独做成一个组件,就像做饺子,以前做饺子必须自己先用面粉做饺子皮,再做饺子馅,然后再手工包饺子,但是现在人们发明了自动包饺子机器,虽然机器里面的每一步骤和你自己 ...

  4. HDU 1998 奇数阶魔方【模拟填数/注意边界和细节】

    奇数阶魔方 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. Codeforces Round #450 (Div. 2) B. Position in Fraction【数论/循环节/给定分子m 分母n和一个数c,找出c在m/n的循环节第几个位置出现,没出现过输出-1】

    B. Position in Fraction time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)

    令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...

  7. 分享Kali Linux 2017.1镜像

     分享Kali Linux 2017.1镜像 Kali Linux官方于4月24日发布Kali Linux 2017.1版本.该版本仍然采用滚动更新方式,所以软件源为kali-rolling.至现在分 ...

  8. 代码统计利器--CLOC

    MAC下安装命令:$ brew install cloc 其他的linux安装 $ aptitude install cloc 使用方法.到目录下运行: $ cloc . The default ou ...

  9. Sample example for Speech to Text in iOS

    There are several libraries for this kind of conversion - I host two of those on GitHub: libsprec (t ...

  10. Android JNI&NDK编程小结及建议

    前言 由于网上关于JNI/NDK相关的知识点介绍的比较零散而且不具备参照性,所以写了这篇JNI/NDK笔记,便于作为随时查阅的工具类型的文章,本文主要的介绍了在平时项目中常用的命令.JNI数据类型.签 ...