利用树的先序和后序遍历打印 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 ...
随机推荐
- 启动tomcat服务器自动执行一个方法
第一步:配置web.xml文件 添加如下代码 <servlet> <servlet-name>Timer</servlet-name> <servlet-cl ...
- AC日记——中庸之道 codevs 2021
2021 中庸之道 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给定一个长度为N的序列 ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm[盒子装仓鼠/余数]
B. Hamster Farm time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 初探Java类型擦除
本篇博客主要介绍了Java类型擦除的定义,详细的介绍了类型擦除在Java中所出现的场景. 1. 什么是类型擦除 为了让你们快速的对类型擦除有一个印象,首先举一个很简单也很经典的例子. // 指定泛型为 ...
- 洛谷——P1713 麦当劳叔叔的难题
P1713 麦当劳叔叔的难题 题目描述 话说我们铭铭小朋友成功的回答了爸爸的问题,自然少不了要去索要些奖励,抠门的爸爸一看报纸,嘿,门口的麦当劳在搞活动,还有免费午餐哦,不过前提条件:得正确回答麦当劳 ...
- SELinux下更改mysql端口
默认情况下 mysql更改端口后是不能通过selinux的 提示启动错误,那么首先就要看mysql的错误日志 可是我不知道mysql错误日志的位置 首先,更改selinux的模式为passive 然后 ...
- 非常有用的开发工具之Android Studio插件
我们都知道Eclipse开发Android将在今年年底google不再继续提供相应的开发支持,转而开始强烈发展Android Studio,现在我就分享几款能帮助团队提升工作效率的几个Android ...
- 【freeCodeCamp】免费晋级前台工程师呦!!!!
首页地址:https://www.freecodecamp.org/ GitHub:https://github.com/freeCodeCamp/freeCodeCamp ============= ...
- VS2010 MFC中 使用CListCtrl的排序功能
list 控件是creat的,不是拖在对话框上的.想使用CListCtrl的排序功能却犯了愁~~~ 还好找到方法,如下: .h文件里:afx_msg void OnLvnColumnclickList ...
- 15.【nuxt起步】-Nuxt使用jsweixin sdk
npm install weixin-js-sdk --save 这个不行,这个是vue前端用的 网上找了一些vue jsweixin的案例 不能直接用 因为nuxt是后端运行,windows对象取不 ...