Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recursion: sort binary search tree #16

Merged
merged 6 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions common/TreeNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

class TreeNode {
node = 0;
leftTree = {}; // TreeNode
rightTree = {}; // TreeNode

constructor(node, leftTree, rightTree) {
this.node = node;
this.leftTree = leftTree;
this.rightTree = rightTree;
}

getNode() {
if (this) return this.node;

return
}

getLeftTree() {
return this.leftTree;
}

getRightTree() {
return this.rightTree;
}
};

module.exports = {
TreeNode
};
20 changes: 19 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
const colors = require('colors');
const TreeNode = require('./TreeNode.js').TreeNode;

const buildTree = (multiplierFactor = 1) => {
const emptyNode = new TreeNode(null, null, null);

const firstNodeLeftTree = new TreeNode(2 * multiplierFactor, emptyNode, emptyNode);
const secondNodeLeftTree = new TreeNode(3 * multiplierFactor, firstNodeLeftTree, emptyNode);

const leftTree = new TreeNode(4 * multiplierFactor, secondNodeLeftTree, emptyNode)

const firstNodeRightTree = new TreeNode(6 * multiplierFactor, emptyNode, emptyNode);
const secondNodeRightTree = new TreeNode(11 * multiplierFactor, emptyNode, emptyNode);

const rightTree = new TreeNode(10 * multiplierFactor, firstNodeRightTree, secondNodeRightTree);

return new TreeNode(5 * multiplierFactor, leftTree, rightTree);
};

const isArrayEqual = (arr1, arr2) => {
if (arr1.length !== arr2.length) return false;
Expand Down Expand Up @@ -85,5 +102,6 @@ module.exports = {
isArrayEqual,
isArrayOfObjectsEqual,
printTestResult,
printMatrix
printMatrix,
buildTree
}
28 changes: 28 additions & 0 deletions interviews/recursion/sort-binary-tree/sort-binary-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const common = require('../../../common/utils.js');
const testData = require('./testData/index.js');

/*
* Arguments: tree: TreeNode
*
* returns array representing sorted node values
*
* eg.
* tree = 5
* 4 10
* 3 6 11
* 2
* sortTree(tree)
* returns [ 2, 3, 4, 5, 6, 10, 11 ]
*/

function sortTree(tree) {
if (tree.getNode() === null) {
return;
} else {
const leftTree = [].concat(sortTree(tree.getLeftTree())).concat(tree.getNode());
const rightTree = [].concat(sortTree(tree.getRightTree()));
return [...leftTree, ...rightTree].filter(Boolean);
}
}

common.printTestResult(sortTree, testData.testTrees, 'sorted tree', 'array');
33 changes: 33 additions & 0 deletions interviews/recursion/sort-binary-tree/testData/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const utils = require('../../../../common/utils.js');

const testTrees = [
{
params: utils.buildTree(1),
expectedResult: [2, 3, 4, 5, 6, 10, 11],
label: 'tree-1'
},
{
params: utils.buildTree(2),
expectedResult: [4, 6, 8, 10, 12, 20, 22],
label: 'tree-2'
},
{
params: utils.buildTree(3),
expectedResult: [6, 9, 12, 15, 18, 30, 33],
label: 'tree-3'
},
{
params: utils.buildTree(4),
expectedResult: [8, 12, 16, 20, 24, 40, 44],
label: 'tree-4'
},
{
params: utils.buildTree(5),
expectedResult: [10, 15, 20, 25,30, 50, 55],
label: 'tree-5'
},
];

module.exports = {
testTrees,
};