forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRootingTreeFromGraph.js
59 lines (48 loc) · 1.52 KB
/
RootingTreeFromGraph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Author: Denton Kunz
* Tree rooting algorithm in Javascript
* Takes in an undirected graph (adjacency matrix)
* Follows pseudocode given at https://youtu.be/2FFq2_je7Lg?si=rIwT8UCYcaGhxH6h
* Node class can be modified to include a data field
*/
class Node {
constructor(id, parent, children=[]){
this.id = id
this.parent = parent
this.children = children
}
}
function rootTree(graph, rootId=0){
console.log(graph)
//the first node (root) should start with no parents/children
const root = new Node(rootId, null)
//recursively generate the rest of the tree
return buildTree(graph, root, null)
}
function buildTree(graph, node, parent){
//let i represent the id of the child node
for (let i = 0; i < graph[node.id].length; i++) {
//skip until we find a child node
if(graph[node.id][i]==0){
// console.log("skipped 1")
continue
}
//if the child node is the current node, skip this case
if(node.id == i){
// console.log("skipped 2")
continue
}
//avoid creating an edge between child and the current node's parent
if( parent != null && i == parent.id){
// console.log("skipped 3")
continue
}
//create and add the new node
let child = new Node(i, node)
node.children.push(child)
//recursively iterate using that node
buildTree(graph, child, node)
}
return node
}
export {Node, rootTree, buildTree}