【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. Linux signal那些事儿【转】

    转自:http://blog.chinaunix.net/uid-24774106-id-4061386.html Linux编程,信号是一个让人爱恨交加又不得不提的一个领域.最近我集中学习了Linu ...

  2. 对/proc和/sys的一些理解

    一切皆文件,设备(文件)可以通过读写来操作:/proc procfs:/sys sysfs: 个人的理解(不知对不对,感觉有些片面)/proc是内存中有关系统进程的实时信息:/sys是有关系统内核以及 ...

  3. Python学习杂记_1_PyCharm使用的一些收获

    一. 界面及字体的调整 装好PyCharm默认的界面是白色的,编辑区域和Console区域的字体也比较小.我个人比较喜欢界面是黑底的,主要关注区域上的字体,大一些,看着清楚一些.调整办法是这样滴~! ...

  4. 通过使用集合Properties完成JDBC的连接工具类

    1.将数据库连接对象所需参数保存在本地文件中 database.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localh ...

  5. Cryptography I 学习笔记 --- 零碎

    1. KDF(密钥推导函数,key derivation function),根据用户输入的一个初始密钥来生成一系列的后续密钥.可以使用PRF来生成 2. 可以用salt与slow hash func ...

  6. Codeforces 777E Hanoi Factory(线段树维护DP)

    题目链接 Hanoi Factory 很容易想到这是一个DAG模型,那么状态转移方程就出来了. 但是排序的时候有个小细节:b相同时看a的值. 因为按照惯例,堆塔的时候肯定是内半径大的在下面. 因为N有 ...

  7. POJ 3710 Christmas Game [博弈]

    题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...

  8. Mapxtreme 在64位系统运行网站,提示未能加载文件或程序集,或它的某一个依赖项

    在32位系统上开发的网站,现在需要布署到64位系统上运行,布署好后访问提示提示未能加载文件或程序集,或它的某一个依赖项.在网上搜索后,发现是64位下引用dll出现的这个问题.这个问题通常出在引用第三方 ...

  9. JAVA实现EXCEL公式专题(四)——字符串函数

    直接上代码: /** * 项目名称: * 文件说明: ExCEL公式类型:字符串公式 * 主要特点: * 版本:1.0 * 制作人:刘晨曦 * 创建时间:2013-12-3 **/ package E ...

  10. Centos7/RedHat7 下 python3使用cx-freeze打包matplotlib程序遇到的问题和解决办法

    折腾了一天遇到了几个头疼的问题,还好回去前解决掉了 第一个:执行cxfreeze打包好的程序遇到 tkinter 和 _tkinter的缺失问题 首先终端:python tkinter python ...