|
1 | 1 | /*
|
2 |
| - * Author: Surendra Kumar |
| 2 | + * Author: Sundaram Kumar Jha |
3 | 3 | * DFS Algorithm implementation in JavaScript
|
4 | 4 | * DFS Algorithm for traversing or searching graph data structures.
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -function traverseDFS (root) { |
8 |
| - const stack = [root] |
9 |
| - const res = [] |
| 7 | +function dfs(tree) { |
| 8 | + if (!tree) return; |
| 9 | + console.log(tree.value); |
| 10 | + for (const child of tree.children) { |
| 11 | + dfs(child); |
| 12 | + } |
| 13 | +} |
10 | 14 |
|
11 |
| - while (stack.length) { |
12 |
| - const curr = stack.pop() |
13 |
| - res.push(curr.key) |
| 15 | +// This function takes a tree node as an argument, and uses recursion to traverse |
| 16 | +// the tree in a depth-first manner. The function first logs the value of the current |
| 17 | +// node and then iterates through its children, calling the function on each child. |
| 18 | +// The function stops when it encounters a null node. |
14 | 19 |
|
15 |
| - if (curr.right) { |
16 |
| - stack.push(curr.right) |
17 |
| - } |
| 20 | +function searchDFS(tree, target) { |
| 21 | + if (tree.value === target) { |
| 22 | + return true; |
| 23 | + } |
18 | 24 |
|
19 |
| - if (curr.left) { |
20 |
| - stack.push(curr.left) |
| 25 | + for (let i = 0; i < tree.children.length; i++) { |
| 26 | + if (searchDFS(tree.children[i], target)) { |
| 27 | + return true; |
21 | 28 | }
|
22 | 29 | }
|
23 | 30 |
|
24 |
| - return res.reverse() |
| 31 | + return false; |
25 | 32 | }
|
26 | 33 |
|
27 |
| -function searchDFS (tree, value) { |
28 |
| - const stack = [] |
| 34 | +// This function takes two arguments: tree, which is the tree to be searched, and target, |
| 35 | +// which is the value you're searching for. The function uses a depth-first search algorithm to |
| 36 | +// traverse the tree and look for the target value. |
29 | 37 |
|
30 |
| - stack.push(tree[0]) |
| 38 | +// The function starts by checking if the current node has the target value. |
| 39 | +// If it does, it returns true to indicate that the target value has been found. |
| 40 | +// If not, the function loops over the children of the current node and calls itself recursively on each child. |
| 41 | +// If any of the recursive calls returns true, the function also returns true to indicate that the target value |
| 42 | +// has been found. If none of the recursive calls returns true, the function returns false to indicate that the |
| 43 | +// target value was not found in the tree |
31 | 44 |
|
32 |
| - while (stack.length !== 0) { |
33 |
| - for (let i = 0; i < stack.length; i++) { |
34 |
| - const node = stack.pop() |
| 45 | +const tree = { |
| 46 | + value: 1, |
| 47 | + children: [ |
| 48 | + { |
| 49 | + value: 2, |
| 50 | + children: [ |
| 51 | + { |
| 52 | + value: 4, |
| 53 | + children: [ |
| 54 | + { |
| 55 | + value: 8, |
| 56 | + children: [ |
| 57 | + { |
| 58 | + value: 16, |
| 59 | + children: [], |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + value: 9, |
| 65 | + children: [ |
| 66 | + { |
| 67 | + value: 17, |
| 68 | + children: [], |
| 69 | + }, |
| 70 | + { |
| 71 | + value: 18, |
| 72 | + children: [], |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + { |
| 79 | + value: 5, |
| 80 | + children: [ |
| 81 | + { |
| 82 | + value: 10, |
| 83 | + children: [ |
| 84 | + { |
| 85 | + value: 19, |
| 86 | + children: [], |
| 87 | + }, |
| 88 | + { |
| 89 | + value: 20, |
| 90 | + children: [], |
| 91 | + }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + { |
| 95 | + value: 11, |
| 96 | + children: [ |
| 97 | + { |
| 98 | + value: 21, |
| 99 | + children: [], |
| 100 | + }, |
| 101 | + { |
| 102 | + value: 22, |
| 103 | + children: [], |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + { |
| 112 | + value: 3, |
| 113 | + children: [ |
| 114 | + { |
| 115 | + value: 6, |
| 116 | + children: [ |
| 117 | + { |
| 118 | + value: 12, |
| 119 | + children: [ |
| 120 | + { |
| 121 | + value: 23, |
| 122 | + children: [], |
| 123 | + }, |
| 124 | + { |
| 125 | + value: 24, |
| 126 | + children: [], |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + { |
| 131 | + value: 13, |
| 132 | + children: [ |
| 133 | + { |
| 134 | + value: 25, |
| 135 | + children: [], |
| 136 | + }, |
| 137 | + { |
| 138 | + value: 26, |
| 139 | + children: [], |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + ], |
| 144 | + }, |
| 145 | + { |
| 146 | + value: 7, |
| 147 | + children: [ |
| 148 | + { |
| 149 | + value: 14, |
| 150 | + children: [ |
| 151 | + { |
| 152 | + value: 27, |
| 153 | + children: [], |
| 154 | + }, |
| 155 | + { |
| 156 | + value: 28, |
| 157 | + children: [], |
| 158 | + }, |
| 159 | + ], |
| 160 | + }, |
| 161 | + { |
| 162 | + value: 15, |
| 163 | + children: [ |
| 164 | + { |
| 165 | + value: 29, |
| 166 | + children: [], |
| 167 | + }, |
| 168 | + { |
| 169 | + value: 30, |
| 170 | + children: [], |
| 171 | + }, |
| 172 | + ], |
| 173 | + }, |
| 174 | + ], |
| 175 | + }, |
| 176 | + ], |
| 177 | + }, |
| 178 | + ], |
| 179 | +}; |
35 | 180 |
|
36 |
| - if (node.value === value) { |
37 |
| - return node |
38 |
| - } |
39 |
| - if (node.right) { |
40 |
| - stack.push(tree[node.right]) |
41 |
| - } |
42 |
| - if (node.left) { |
43 |
| - stack.push(tree[node.left]) |
44 |
| - } |
45 |
| - } |
46 |
| - } |
47 |
| - return null |
48 |
| -} |
| 181 | +console.log(dfs(tree)); |
| 182 | + |
| 183 | +const target = 275; |
| 184 | +const result = searchDFS(tree, target); |
| 185 | +console.log(result); // true or false |
| 186 | + |
| 187 | + |
| 188 | +// The tree argument is an object that represents a tree data structure, |
| 189 | +// where each node has a value and an array of child nodes. In this particular example, |
| 190 | +// the tree has a root node with value 1. The root node has two child nodes, represented by objects with values 2 and 3. |
| 191 | +// These child nodes each have two child nodes, and so on. |
| 192 | + |
| 193 | +// Each node in the tree is represented as an object with two properties: value and children. |
| 194 | +// The value property holds the value of the node, and the children property is an array of child nodes. |
| 195 | +// If a node has no children, its children property is an empty array []. |
| 196 | + |
| 197 | +// Here's a visual representation of the tree argument: |
| 198 | + |
| 199 | +// 1 |
| 200 | +// / \ |
| 201 | +// 2 3 |
| 202 | +// / \ / \ |
| 203 | +// 4 5 6 7 |
| 204 | +// /\ /\ /\ /\ |
| 205 | +// 8 9 10 11 12 13 14 15 |
| 206 | +// /\ /\ /\ /\ /\ /\ /\ /\ |
| 207 | +// 161718192021 22222324252627282930 |
| 208 | + |
| 209 | +// This tree can be traversed using the dfs function provided earlier, |
| 210 | +// which visits nodes in a depth-first order. The dfs function uses recursion |
| 211 | +// to traverse the tree, starting from the root node and visiting each child node |
| 212 | +// in turn before moving on to the next sibling node. |
49 | 213 |
|
50 |
| -const tree = [ |
51 |
| - { value: 6, left: 1, right: 2 }, |
52 |
| - { value: 5, left: 3, right: 4 }, |
53 |
| - { value: 7, left: null, right: 5 }, |
54 |
| - { value: 3, left: 6, right: null }, |
55 |
| - { value: 4, left: null, right: null }, |
56 |
| - { value: 9, left: 7, right: 8 }, |
57 |
| - { value: 2, left: 9, right: null }, |
58 |
| - { value: 8, left: null, right: null }, |
59 |
| - { value: 10, left: null, right: null }, |
60 |
| - { value: 1, left: null, right: null } |
61 |
| -] |
62 |
| - |
63 |
| -searchDFS(tree, 9) |
64 |
| -searchDFS(tree, 10) |
65 |
| - |
66 |
| -traverseDFS(6) |
67 |
| - |
68 |
| -// 6 |
69 |
| -// / \ |
70 |
| -// 5 7 |
71 |
| -// / \ \ |
72 |
| -// 3 4 9 |
73 |
| -// / / \ |
74 |
| -// 2 8 10 |
75 |
| -// / |
76 |
| -// 1 |
|
0 commit comments