日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/3 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用树的先序和后序遍历打印os中的目录树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【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)先序遍歷以兄弟為根的子樹; download source code:https://github.com/pacosonTang/dataStructure-algorithmAnalysis/blob/master/chapter4/p68_preorder_common_tree.c

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 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; }
打印結果如下:


2.2)后序遍歷步驟:(不同于二叉樹的后序)
step1)后序遍歷以兒子為根的子樹;
step2)訪問根節點; step3)后序遍歷以兄弟為根的子樹; download source code:https://github.com/pacosonTang/dataStructure-algorithmAnalysis/blob/master/chapter4/p69_postorder_commone_tree.c
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 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中的目录树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。