MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / traverseDFS

Function traverseDFS

Trees/DepthFirstSearch.js:8–25  ·  view source on GitHub ↗
(tree, rootValue)

Source from the content-addressed store, hash-verified

6
7// traverses a give tree from specified root's value
8function traverseDFS(tree, rootValue) {
9 const stack = []
10 const res = []
11 stack.push(searchDFS(tree, rootValue))
12 // if root is not present in the tree, returning empty array
13 if (!stack[0]) return res
14 while (stack.length) {
15 const curr = stack.pop()
16 res.push(curr.value)
17 if (curr.left) {
18 stack.push(tree[curr.left])
19 }
20 if (curr.right) {
21 stack.push(tree[curr.right])
22 }
23 }
24 return res.reverse()
25}
26
27function searchDFS(tree, value) {
28 const stack = []

Callers 1

Calls 4

searchDFSFunction · 0.85
reverseMethod · 0.80
pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected