利用树的先序和后序遍历打印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 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;
}
#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中的目录树相关推荐
- 手动创建一棵二叉树,然后利用前序、中序、后序、层序进行遍历(从创建二叉树到各种方式遍历)(含运行结果)
手动创建一棵二叉树,然后利用前序.中序.后序.层序进行遍历 import java.util.LinkedList; import java.util.List; import java.util.Q ...
- 把一个数组的值存入二叉树中,然后利用前序、中序、后序3种方式进行遍历(完整代码以及运行结果)(Java)
把一个数组的值存入二叉树中,然后利用前序.中序.后序3种方式进行遍历(完整代码以及运行结果) 在最近的面试过程中,听说有小伙伴被面试官要求创建二叉树,然后对该二叉树进行遍历,感觉这一直以来都是一个大家 ...
- 【练习】树(Tree, UVa 548)给一棵点带权(权值各不相同)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小。
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
- c++ 树的先序、中序和后序的非递归实现(附完整源码)
树的先序.中序和后序的非递归实现 一.先序遍历 1.递归算法 2.非递归算法 二.中序遍历 1.递归算法 2.非递归算法 三.后序遍历 1.递归算法 2.非递归算法 一.先序遍历 1.递归算法 str ...
- java二叉树合并_Java(树的前中后序遍历构造二叉树题型整合)前序和中序、中序和后序、前序和后序遍历序列构造二叉树算法整合归纳...
前言 二叉树各种花里胡哨的算法题真的把我搞晕了,今天特地整理出一类有关二叉树的算法题,希望能帮助阅读到此文章的人,今后不再受此类题型的困扰. 一.题目类型 已知二叉树的两种遍历序列,请根据该序列构建二 ...
- 【数据结构笔记26】根据一棵树的先序/中序遍历Push与Pop内容,输出这棵树的先序、中序、后序遍历数组(不需要真的建立出树)
本次笔记内容: 练习题-TTA.1 题意理解 练习题-TTA.2 核心算法 文章目录 题意理解 根据Push与Pop直接得出先序.中序数组 根据pre和in生成post C实现 题意理解 先来回忆非递 ...
- C语言利用二叉树的操作实现根据给定的字符串生成二叉树并前序、中序、后序输出二叉树。
C语言利用二叉树的操作实现根据给定的字符串生成二叉树并前序.中序.后序输出二叉树. Description 根据给定的字符串生成二叉树并前序.中序.后序此二叉树. Input 给定一字符串,其中#表示 ...
- Suzy找到实习了吗Day 18 | 二叉树进行中:513 找树左下角的值,112 路径总和 ,106.从中序与后序遍历序列构造二叉树
513 找树左下角的值 solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, val ...
- 树-树的遍历(先序、中序、后序)
树的遍历 树的遍历方式主要分为四种,先序.中序.后序和层序,在这篇博客中我将仔细介绍一下树的这四种遍历方式. 先序遍历 先序遍历,也叫先根遍历.前序遍历,首先访问根结点然后遍历左子树,最后遍历右子树. ...
最新文章
- 人工智能下一个前沿:可解释性
- 40种为网页设计师准备的高品质和免费的多媒体PSD文件
- (七)boost库之单例类
- 【slighttpd】基于lighttpd架构的Server项目实战(4)—简单的echo服务器
- c# html文件转换word,C#实现word转换成html文档 源码
- python中list index out of range_Python知识精解:str split()方法
- amp sqlserver中 什么意思_股票术语中的做空到底是什么意思?
- 获取Django中model字段名 字段的verbose_name
- (一) js + Vue 写扫雷
- HALCON学习笔记 1
- HD TUNE 下载使用
- digester_Apache Digester示例–轻松配置
- JavaScript 如何求两个数的最小公倍数
- 中国石油大学《机械原理》第二次在线作业
- SeedLab1: Sniffing Spoofing Lab
- 三次Bezier曲线/B样条曲线转换成隐函数方程的方法
- 猿辅导9-12编程题3道
- 高等数学笔记-乐经良老师-第四章-微分中值定理和导数的应用-第五节-曲线的曲率
- 详解python中readlines函数的参数hint
- git push 提示当前分支没有对应的远程分支?
热门文章
- 【PKUWC2018】随机游走【Min-Max容斥】【树形dp】【FWT】
- Acwing 236. 格鲁吉亚和鲍勃(博弈论妙题)
- I - Triple HDU - 5517
- 武汉工程大学2020GPLT选拔赛(上)
- YBTOJ洛谷P3209:平面图判定(2-SAT)
- 牛客国庆集训派对day6TJ-DefenseTower【贪心】
- ssl初一组周六模拟赛【2018.4.7】
- Codeforces Round #674 (Div. 3)
- 【DP】方格计数(nowcoder 20107-B)
- 洛谷 P1967货车运输 并查集+贪心 不需要用LCA!