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



struct Tree;
typedef struct Tree *Tree; // we adopt child-sibling notation
struct Tree
{
ElementType value;
Tree firstChild;
Tree nextSibling;
};

#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 ;
}

#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 中的目录树的更多相关文章
- 树的三种DFS策略(前序、中序、后序)遍历
之前刷leetcode的时候,知道求排列组合都需要深度优先搜索(DFS), 那么前序.中序.后序遍历是什么鬼,一直傻傻的分不清楚.直到后来才知道,原来它们只是DFS的三种不同策略. N = Node( ...
- 二叉排序树的构造 && 二叉树的先序、中序、后序遍历 && 树的括号表示规则
二叉排序树的中序遍历就是按照关键字的从小到大顺序输出(先序和后序可没有这个顺序) 一.以序列 6 8 5 7 9 3构建二叉排序树: 二叉排序树就是中序遍历之后是有序的: 构造二叉排序树步骤如下: 插 ...
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; ...
- ZT 二叉树先序,中序,后序遍历非递归实现
二叉树先序,中序,后序遍历非递归实现 分类: 数据结构及算法2012-04-28 14:30 8572人阅读 评论(6) 收藏 举报 structc 利用栈实现二叉树的先序,中序,后序遍历的非递归操作 ...
- Java实现二叉树的先序、中序、后序、层序遍历(递归和非递归)
二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易 ...
- 【数据结构与算法】二叉树的 Morris 遍历(前序、中序、后序)
前置说明 不了解二叉树非递归遍历的可以看我之前的文章[数据结构与算法]二叉树模板及例题 Morris 遍历 概述 Morris 遍历是一种遍历二叉树的方式,并且时间复杂度O(N),额外空间复杂度O(1 ...
- 二叉树的前序和中序得到后序 hdu1710
今天看学长发过来的资料上面提到了中科院机试会有一个二叉树的前序中序得到后序的题目.中科院的代码编写时间为一个小时,于是在七点整的时候我开始拍这个题目.这种类型完全没做过,只有纸质实现过,主体代码半个小 ...
- LeetCode二叉树的前序、中序、后序遍历(递归实现)
本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...
- [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 ...
随机推荐
- Hibernate更新某些字段的几种update方法
Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: public class TeacherTest { @Test pu ...
- [SaltStack] 基础介绍
今天有时间把以前研究过的saltstack梳理总结下 -:) salt是干什么的我就不多说了, 大家Google下资料很多的, 简单来说就是func+puppet: 配置文件管理 远程命令调用 Cro ...
- 通过Java实现斗地主
功能:洗牌,发牌,对玩家手中的牌排序,看牌 package demo06; import java.util.ArrayList; import java.util.Collections; impo ...
- hdu 4995(离散化下标+模拟)
Revenge of kNN Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 使用PyQt4制作一个正则表达式测试小工具
最近在做一些网络爬虫的时候,会经常用到正则表达式.为了写出正确的正则表达式,我经常在这个网站上进行测试:Regex Tester.这个页面上面一个输入框输入正则表达式,下面一个输入框输入测试数据,上面 ...
- AC日记——数据流中的算法 51nod 1785
数据流中的算法 思路: 线段树模拟: 时间刚刚卡在边界上,有时超时一个点,有时能过: 来,上代码: #include <cstdio> #include <cstring> # ...
- Block为什么使用Copy?
block:本质就是一个object-c对象block:存储位置,可能分为3个地方:代码去,堆区.栈区(ARC情况下会自动拷贝到堆区,因此ARC下只能有两个地方:代码去.堆区)代码区:不访问栈区的变量 ...
- getServletConfig().getInitParameter("count1") java.lang.NullPointerException
通常在doget中 System.out.println(getServletConfig()); System.out.println(getServletConfig().getInitParam ...
- redis--服务器与客户端
初始化服务器 从启动 Redis 服务器,到服务器可以接受外来客户端的网络连接这段时间,Redis 需要执行一系列初始化操作. 整个初始化过程可以分为以下六个步骤: 初始化服务器全局状态. 载入配置文 ...
- linux系统故障分析与排查
在处理Linux系统出现的各种故障时,故障的症状是最先发现的,而导致这以故障的原因才是最终排除故障的关键.熟悉Linux系统的日志管理,了解常见故障的分析与解决办法,将有助于管理员快速定位故障点.“对 ...