Skip to content

Commit

Permalink
Start binaryTree test
Browse files Browse the repository at this point in the history
  • Loading branch information
gusaiani committed Mar 17, 2020
1 parent 30bba52 commit 1b38ef7
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
66 changes: 66 additions & 0 deletions binaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
function createBinaryNode(key) {
return {
key,
left: null,
right: null,
addLeft(leftKey) {
const newLeft = createBinaryNode(leftKey)
this.left = newLeft
return newLeft

},
addRight(rightKey) {
const newRight = createBinaryNode(rightKey)
this.right = newRight
return newRight
}
}
}

const TRAVERSALS = {
IN_ORDER: (node, visitFn) => {
if (node !== null) {
TRAVERSALS.IN_ORDER(node.left, visitFn)
visitFn(node)
TRAVERSALS.IN_ORDER(node.right, visitFn)
}
},
PRE_ORDER: (node, visitFn) => {
if (node !== null) {
visitFn(node)
TRAVERSALS.PRE_ORDER(node.left, visitFn)
TRAVERSALS.PRE_ORDER(node.right, visitFn)
}
},
POST_ORDER: (node, visitFn) => {
if (node !== null) {
TRAVERSALS.POST_ORDER(node.left, visitFn)
TRAVERSALS.POST_ORDER(node.right, visitFn)
visitFn(node)
}

}
}

function createBinaryTree(rootKey) {
const root = createBinaryNode(rootKey)

return {
root,
print(traversalType = 'IN_ORDER') {
let result = ''

const visit = node => {
result += result.length === 0
? node.key
: ` => ${node.key}`
}

TRAVERSALS[traversalType](this.root, visit)

return result
}
}
}

module.exports = createBinaryTree
17 changes: 17 additions & 0 deletions tests/binaryTree.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const createBinaryTree = require('../binaryTree')

describe('binaryTree', () => {
test('should pass sanity check', () => {
const tree = createBinaryTree('a')
const b = tree.root.addLeft('b')
const c = tree.root.addRight('c')
const d = b.addLeft('d')
const e = b.addRight('e')
const f = c.addLeft('f')
const g = c.addRight('g')
const h = d.addLeft('h')
const i = d.addRight('i')

console.log(tree.print());
})
})
2 changes: 1 addition & 1 deletion graph.test.js → tests/graph.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createGraph = require('./graph')
const createGraph = require('../graph')

describe('graph', () => {
describe('addNode', () => {
Expand Down
2 changes: 1 addition & 1 deletion linkedList.test.js → tests/linkedList.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createLinkedList = require('./linkedList')
const createLinkedList = require('../linkedList')

describe('linked list', () => {
describe('get', () => {
Expand Down
2 changes: 1 addition & 1 deletion stack.test.js → tests/stack.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createStack = require('./stack')
const createStack = require('../stack')

const element1 = Symbol()
const element2 = Symbol()
Expand Down
2 changes: 1 addition & 1 deletion tree.test.js → tests/tree.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createTree = require('./tree')
const createTree = require('../tree')

describe('tree', () => {
test('html tree', () => {
Expand Down

0 comments on commit 1b38ef7

Please sign in to comment.