Skip to content

Commit cab2d5a

Browse files
committed
Add layout feature
1 parent 83cb291 commit cab2d5a

File tree

75 files changed

+213
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+213
-96
lines changed

.bin/batchFix.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
const isDirectory = dirPath => fs.lstatSync(dirPath).isDirectory();
5+
const listFiles = dirPath => fs.readdirSync(dirPath).filter(fileName => !fileName.startsWith('.'));
6+
const listDirectories = dirPath => listFiles(dirPath).filter(fileName => isDirectory(path.resolve(dirPath, fileName)));
7+
8+
const rootPath = path.resolve(__dirname, '..');
9+
listDirectories(rootPath).forEach(category => {
10+
const categoryPath = path.resolve(rootPath, category);
11+
listDirectories(categoryPath).forEach(algorithm => {
12+
const algorithmPath = path.resolve(categoryPath, algorithm);
13+
listFiles(algorithmPath).filter(file => /\.js$/.test(file)).forEach(file => {
14+
const filePath = path.resolve(algorithmPath, file);
15+
const content = fs.readFileSync(filePath, 'utf8');
16+
17+
const variables = [];
18+
let lineNumber = -1;
19+
let needDelay = false;
20+
const lines = content.split('\n').map((line, i) => {
21+
const match = /^\s*const (\w+) = new \w*Tracer\(/g.exec(line);
22+
if (match) {
23+
variables.push(match[1]);
24+
lineNumber = i;
25+
line = line.replace(/\.delay\(\s*\)/g, () => {
26+
needDelay = true;
27+
return '';
28+
});
29+
}
30+
return line.replace(' } = require(\'algorithm-visualizer\')', ', Layout, VerticalLayout } = require(\'algorithm-visualizer\')');
31+
});
32+
33+
if (~lineNumber) {
34+
const line = `Layout.setRoot(new VerticalLayout([${variables.join(', ')}]))${needDelay ? '.delay()' : ''};`;
35+
lines.splice(lineNumber + 1, 0, line);
36+
const newContent = lines.join('\n');
37+
fs.writeFileSync(filePath, newContent, 'utf8');
38+
} else {
39+
console.error('wtf');
40+
}
41+
});
42+
});
43+
});

Backtracking/Knight's Tour Problem/code.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Array1DTracer, Array2DTracer, LogTracer } = require('algorithm-visualizer');
1+
const { Array1DTracer, Array2DTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
/*
44
For N>3 the time taken by this algorithm is sufficiently high
@@ -28,7 +28,8 @@ pos[0] = pos[1] = -1;
2828

2929
const boardTracer = new Array2DTracer('Board').set(board);
3030
const posTracer = new Array1DTracer('Knight Position').set(pos);
31-
const logTracer = new LogTracer('Console').delay();
31+
const logTracer = new LogTracer('Console');
32+
Layout.setRoot(new VerticalLayout([boardTracer, posTracer, logTracer])).delay();
3233

3334
function knightTour(x, y, moveNum) {
3435
if (moveNum === N * N) {

Backtracking/N-Queens Problem/code.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Array2DTracer, LogTracer } = require('algorithm-visualizer');
1+
const { Array2DTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const N = 4; // just change the value of N and the visuals will reflect the configuration!
44
const board = (function createArray(N) {
@@ -19,6 +19,7 @@ const queens = (function qSetup(N) {
1919
const boardTracer = new Array2DTracer('Board');
2020
const queenTracer = new Array2DTracer('Queen Positions');
2121
const logger = new LogTracer('Progress');
22+
Layout.setRoot(new VerticalLayout([boardTracer, queenTracer, logger]));
2223

2324
boardTracer.set(board);
2425
queenTracer.set(queens);

Branch and Bound/Binary Search Tree/insertion.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const { Array1DTracer, GraphTracer, LogTracer } = require('algorithm-visualizer');
1+
const { Array1DTracer, GraphTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const T = {};
44

55
const elements = [5, 8, 10, 3, 1, 6, 9, 7, 2, 0, 4]; // item to be inserted
66
const graphTracer = new GraphTracer(' BST - Elements marked red indicates the current status of tree ');
77
const elemTracer = new Array1DTracer(' Elements ').set(elements);
88
const logger = new LogTracer(' Log ');
9+
Layout.setRoot(new VerticalLayout([graphTracer, elemTracer, logger]));
910
graphTracer.log(logger).delay();
1011

1112
function bstInsert(root, element, parent) { // root = current node , parent = previous node

Branch and Bound/Binary Search Tree/search.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { GraphTracer, LogTracer, Randomize } = require('algorithm-visualizer');
1+
const { GraphTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
44
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
@@ -31,6 +31,7 @@ const T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][
3131
const key = new Randomize.Integer(0, G.length - 1).create(); // item to be searched
3232
const tracer = new GraphTracer(' Binary Search Tree ').set(G).layoutTree(5);
3333
const logger = new LogTracer(' Log ');
34+
Layout.setRoot(new VerticalLayout([tracer, logger]));
3435
tracer.log(logger).delay();
3536

3637
function bst(item, node, parent) { // node = current node , parent = previous node

Branch and Bound/Binary Search/iterative.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const { Array1DTracer, ChartTracer, LogTracer, Randomize } = require('algorithm-visualizer');
1+
const { Array1DTracer, ChartTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const chart = new ChartTracer();
44
const tracer = new Array1DTracer().chart(chart);
55
const logger = new LogTracer();
6+
Layout.setRoot(new VerticalLayout([chart, tracer, logger]));
67
const D = new Randomize.Array1D(15, new Randomize.Integer(0, 50)).sorted().create();
78
tracer.set(D).delay();
89

Branch and Bound/Binary Search/recursive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const { Array1DTracer, ChartTracer, LogTracer, Randomize } = require('algorithm-visualizer');
1+
const { Array1DTracer, ChartTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const chart = new ChartTracer();
44
const tracer = new Array1DTracer().chart(chart);
55
const logger = new LogTracer();
6+
Layout.setRoot(new VerticalLayout([chart, tracer, logger]));
67
const D = new Randomize.Array1D(15, new Randomize.Integer(0, 50)).sorted().create();
78
tracer.set(D).delay();
89

Branch and Bound/Depth-Limited Search/code.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const { GraphTracer, LogTracer } = require('algorithm-visualizer');
1+
const { GraphTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const tracer = new GraphTracer();
44
const logger = new LogTracer();
5+
Layout.setRoot(new VerticalLayout([tracer, logger]));
56
tracer.log(logger);
67
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
78
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],

Branch and Bound/Topological Sort/code.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const { GraphTracer, LogTracer } = require('algorithm-visualizer');
1+
const { GraphTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const tracer = new GraphTracer();
44
const logger = new LogTracer();
5+
Layout.setRoot(new VerticalLayout([tracer, logger]));
56
tracer.log(logger);
67
// G[i][j] indicates whether the path from the i-th node to the j-th node exists or not. NOTE: The graph must be Directed-Acyclic
78
const G = [

Brute Force/Binary Tree Traversal/inOrder.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Array1DTracer, GraphTracer, LogTracer } = require('algorithm-visualizer');
1+
const { Array1DTracer, GraphTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
22

33
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
44
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
@@ -30,7 +30,8 @@ const T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][
3030

3131
const treeTracer = new GraphTracer(' Traversal In-order ').set(G).layoutTree(5);
3232
const arrayTracer = new Array1DTracer(' Print In-order ').set(new Array(T.length).fill('-'));
33-
const logger = new LogTracer(' Log ').delay();
33+
const logger = new LogTracer(' Log ');
34+
Layout.setRoot(new VerticalLayout([treeTracer, arrayTracer, logger])).delay();
3435

3536
let index = 0;
3637

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