Skip to content

Commit fc06690

Browse files
authored
fixes: TheAlgorithms#1280 and test: added tests for DFS algorithm (TheAlgorithms#1303)
* fix: fixed error in DepthFirstSearch algorithm and test: added tests for DepthFirstSearch algorithm. * changed traverseDFS function parameters in DepthFirstSearch.js file
1 parent 566d910 commit fc06690

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

Trees/DepthFirstSearch.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,32 @@
44
* DFS Algorithm for traversing or searching graph data structures.
55
*/
66

7-
function traverseDFS (root) {
8-
const stack = [root]
7+
// traverses a give tree from specified root's value
8+
function traverseDFS (tree, rootValue) {
9+
const stack = []
910
const res = []
10-
11+
stack.push(searchDFS(tree, rootValue))
12+
// if root is not present in the tree, returning empty array
13+
if (!stack[0]) return res
1114
while (stack.length) {
1215
const curr = stack.pop()
13-
res.push(curr.key)
14-
15-
if (curr.right) {
16-
stack.push(curr.right)
17-
}
18-
16+
res.push(curr.value)
1917
if (curr.left) {
20-
stack.push(curr.left)
18+
stack.push(tree[curr.left])
19+
}
20+
if (curr.right) {
21+
stack.push(tree[curr.right])
2122
}
2223
}
23-
2424
return res.reverse()
2525
}
2626

2727
function searchDFS (tree, value) {
2828
const stack = []
29-
3029
stack.push(tree[0])
31-
3230
while (stack.length !== 0) {
3331
for (let i = 0; i < stack.length; i++) {
3432
const node = stack.pop()
35-
3633
if (node.value === value) {
3734
return node
3835
}
@@ -59,11 +56,10 @@ const tree = [
5956
{ value: 10, left: null, right: null },
6057
{ value: 1, left: null, right: null }
6158
]
62-
63-
searchDFS(tree, 9)
64-
searchDFS(tree, 10)
65-
66-
traverseDFS(6)
59+
searchDFS(tree, 9) // { value: 9, left: 7, right: 8 }
60+
searchDFS(tree, 200) // null
61+
traverseDFS(tree, 6) // [ 1, 2, 3, 4, 5, 8, 10, 9, 7, 6 ]
62+
traverseDFS(tree, 200) // []
6763

6864
// 6
6965
// / \
@@ -74,3 +70,4 @@ traverseDFS(6)
7470
// 2 8 10
7571
// /
7672
// 1
73+
export { searchDFS, traverseDFS }

Trees/test/DepthFirstSearch.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { searchDFS, traverseDFS } from '../DepthFirstSearch'
2+
3+
describe('Depth First Tree Traversal', () => {
4+
const tree = [
5+
{ value: 6, left: 1, right: 2 },
6+
{ value: 5, left: 3, right: 4 },
7+
{ value: 7, left: null, right: 5 },
8+
{ value: 3, left: 6, right: null },
9+
{ value: 4, left: null, right: null },
10+
{ value: 9, left: 7, right: 8 },
11+
{ value: 2, left: 9, right: null },
12+
{ value: 8, left: null, right: null },
13+
{ value: 10, left: null, right: null },
14+
{ value: 1, left: null, right: null }
15+
]
16+
17+
// 6
18+
// / \
19+
// 5 7
20+
// / \ \
21+
// 3 4 9
22+
// / / \
23+
// 2 8 10
24+
// /
25+
// 1
26+
27+
it('should be null if given value is not present in the tree - DF Search', () => {
28+
const res = searchDFS(tree, 200)
29+
expect(res).toStrictEqual(null)
30+
})
31+
32+
it('should return the node if given value is present in the tree - DF Search', () => {
33+
const res = searchDFS(tree, 9)
34+
expect(res).toStrictEqual({ value: 9, left: 7, right: 8 })
35+
})
36+
37+
it('should return empty array if given root is not present in the tree - DF Traversal', () => {
38+
const traversal = traverseDFS(tree, 200)
39+
expect(traversal).toStrictEqual([])
40+
})
41+
42+
it('should return DFT array of given tree from specified root if given root is present in the tree - DF Traversal', () => {
43+
const traversal = traverseDFS(tree, 6)
44+
expect(traversal).toStrictEqual([1, 2, 3, 4, 5, 8, 10, 9, 7, 6])
45+
})
46+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy