Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program in C that get the directory and print all the files as a tree Sample output: The tree command recursively prints out

Write a program in C that get the directory and print all the files as a tree

image text in transcribedimage text in transcribed

Sample output:

image text in transcribed

The "tree" command recursively prints out all of the files and directories in a lovely tree display For this assignment, you can use an array to simplify the implementation. Use Breadth-First Search to traverse all of the files and directories, putting the names in a tree data structure. Use Depth-First Search to traverse the tree you built to print out all the directories and files. A Tree data structure char* path // The directory this file is in, such as "~/Fall2020/resources/assignemnt1/" char* name // The name of this file, such as "tree.c" TreeNode* children TreeNode* siblings int level // How deep in the tree this node is Define the tree struct and relevant functions in an appropriately named.h file, such as tree.h. Recall, with Trees, every node in a tree is also a tree. So, like a linked list, the root of the tree is just a TreeNode, and has children that are TreeNodes (or whatever you want to call them). CreateTreeNode(char* path, char* name) AddChild(TreeNode* root, char* path, char* name) DestroyTreeNode(TreeNode*) Step 2: Define and Implement a Queue data structure Now that you've defined a tree node, use that to implement a Queue that holds TreeNodes. The structure and functions should be declared in a .h file, and implemented in a .c file. You'll need the following operations: EnqueueTreeNode*) DequeueTreeNode*) Depending on how you implement it, you may need to include Create() and Destroy() functions. However, as noted above, if you want to just use an array defined globally, that is fine for this assignment. Step 3: Define and Implement a Stack data structure Implement a Stack that holds TreeNodes. The structure and functions should be declared in a .h file, and implemented in a .c file. You'll need the following operations: Push(TreeNode*) Pop(TreeNode*) Step 4: Build your Tree of Files To build this tree, the algorithm is: Create a TreeNode representing the directory you are starting at Put that root TreeNode in a queue While there are TreeNodes left in the queue, get the next TreeNode and do the following: Add all the files in the directory represented by the current tree node to that tree node If any of the files (now children) are actually directories instead of files, add that child tree node to the queue to be handled later. Getting Files in a Directory Step 5: Print out the Tree Now, you'll do a depth-first search to print out the tree. The algorithm looks something like this: Put the root TreeNode into your stack. While there are more TreeNodes in your stack, get the next TreeNode and do the following: Add all the children of the TreeNode to the stack Print out the current TreeNode -bash-4.2$ tree resources/ resources/ a2 a2 a2.c a2.h a2_test.c Makefile readme.md a3 a.out linkedlist.c linkedlist.h #main.c# main.c maintest.c Makefile operationtest.c operationtest.h random.c random.c~ README.md sort.c sort.h a4 quicksort.c show_time.c a6 a.out day1 L README.md lab2 L lab2.c lab3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

What is linear transformation? Define with example

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago