JavaScript: Trees

JS: Trees → Definitions

Definition

  • Files — leaf nodes
  • The directories are internal. May contain both files and directories inside the propertychildren
  • meta - an object with arbitrary data, for example, size, date of creation, and so on
  • The directory from the file is distinguished, first of all, by the type specified by the corresponding property

JS: Trees → Traversal

Depth-first search

  1. Check if vertex A has children. If so, we run the traversal recursively for each child independently.
  2. Inside the first recursive call is the following subtree:
  1. Inside there is a leaf element E. The function makes sure that the node has no children, does the necessary work and returns the result to the top.
  2. Again we find ourselves in a situation:

JS: Trees → Map

JS: Trees → Filter

JS: Trees → Reduce

JS: Trees → Search

  1. We initialize an reducearray
  2. If the type of the node directoryand it does not have children - add to the array
  1. Since our implementation reducedoes not provide information on how deep the current node is, we can perform a manual walk by passing two batteries: one with a final array, the second with a nesting level.
  2. You can expand the description of the tree itself by adding information about the nesting level there. On the one hand, such a method will not require a change in the application code, since it suffices to change the implementation of the interface functions mkdirand mkfile. On the other hand, in such a scheme, the depth of all nodes will be calculated relative to the root, which is inconvenient.
  3. You can write reducewho can tell how deep to go. And then solve the problem with its use.
  1. Now you need to keep track of the current depth, so it is transmitted to iter, increasing with each iteration.
  2. If you go deeper than necessary, then return the result.

JS: Trees → Aggregation

Additional resources

--

--

--

Full Stack Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Next.js Lazy Loading, Search & Sorting

My First Week in JS

Basic JavaScript coding standards

javascript

Get an overview of hapi.js by building a URL shortener

hapi.js logo

Understanding TypeScript

Lets visit the core fundamental of JavaScript

Firebase Cloud Functions Tutorial — Creating a REST API

Getting started with Nodejs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahendra Choudhary

Mahendra Choudhary

Full Stack Developer

More from Medium

Tutorial: Sorting In JavaScript

Awesome JavaScript Resources

Javascript Recursion

Working with Nested Objects in JavaScript