forked from jmcilhargey/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathref-tree-graph-class.js
More file actions
67 lines (58 loc) · 1.85 KB
/
ref-tree-graph-class.js
File metadata and controls
67 lines (58 loc) · 1.85 KB
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
60
61
62
63
64
65
66
67
// Binary trees are trees limited to two children nodes per parent node
function BinaryTreeNode(value) {
this.value = value;
this.left = null;
this.right = null;
}
BinaryTreeNode.prototype.insertLeft = function(value) {
this.left = new BinaryTreeNode(value);
return this.left;
};
BinaryTreeNode.prototype.insertRight = function(value) {
this.right = new BinaryTreeNode(value);
return this.right;
};
// We have to make a function because of Javascript hoisting, causing the recursive function definitions to be called be console.log!
function printNode(value) {
console.log(value);
}
function inOrderTraversal(node) {
if (node !== null) {
inOrderTraversal(node.left);
printNode(node.value);
inOrderTraversal(node.right);
}
}
function preOrderTraversal(node) {
if (node !== null) {
printNode(node.value);
inOrderTraversal(node.left);
inOrderTraversal(node.right);
}
}
// Graphs are like trees except that they may have cycles in them
// For this reason, it's important to have a flag property to determine if a node has been visited, otherwise you can get inifinite loops
function Node(value) {
this.value = value;
this.edges = [];
this.visited = false;
}
// Breadth first is good for finding the shortest path between two nodes
function breadthFirstSearch(rootNode) {
// See sample queue class in Chpt 2 solutions
var queueOfNodes = new Queue();
var currentNode = rootNode;
// As long as we have nodes to visit
while (!queueOfNodes.isEmpty()) {
// Go through all the connections and queue any not visited yet
currentNode.edges.forEach(function(edge) {
if (!edge.visited) {
queueOfNodes.enqueue(edge);
}
});
// Print the current node, then dequeue the next in line
printNode(currentNode);
currentNode.visited = true;
currentNode = queueOfNodes.dequeue();
}
}