Skip to content

Commit

Permalink
0.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
mike442144 committed Dec 29, 2017
1 parent e96b20f commit 2f9d6a3
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 18 deletions.
27 changes: 17 additions & 10 deletions BinaryTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = class BinaryTree{
let queue = new Queue(), node = null;
queue.enqueue(this._root);

while( queue.length > 0 ){
while( queue.length ){
node = queue.dequeue();
if(node.left)
queue.enqueue(node.left);
Expand All @@ -62,7 +62,22 @@ module.exports = class BinaryTree{
return 0;
}

let queue = new Queue(), e = null;
queue.enqueue({depth:0, node: this._root});

while( queue.length ){
e = queue.dequeue();

if(val === e.node.val){
return e.depth;
}

if(e.node.left)
queue.enqueue({node: e.node.left, depth:e.depth + 1});

if(e.node.right)
queue.enqueue({node: e.node.right, depth:e.depth + 1});
}
}

/*
Expand All @@ -71,19 +86,11 @@ module.exports = class BinaryTree{
height(){

}

toString(){

}

toArray(){

return [..._inOrder(this._root)];
}

toList(){

}

clear(){
this._root = null;
}
Expand Down
14 changes: 7 additions & 7 deletions LinkedStack.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@

'use strict'

const DoublyLinkedList = require('./DoublyLinkedList.js')
const LinkedList = require('./LinkedList.js')

module.exports = class LinkedStack{
constructor(){
this._list = new DoublyLinkedList();
this._list = new LinkedList();
}

pushNode(node){
this._list.insertEndNode(node);
this._list.insertStartNode(node);
return this;
}

push(val){
this._list.insertEnd(val);
this._list.insertStart(val);
return this;
}

popNode(){
return this._list.deleteLast();
return this._list.deleteFirst();
}

pop(){
return this._list.deleteLast().val;
return this._list.deleteFirst().val;
}

peek(){
return this._list.tail.val;
return this._list.head.val;
}

get length(){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-ds",
"version": "0.0.8",
"version": "0.0.9",
"description": "A common data-structure and basic algorithm implemention in javascript",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions tests/BinaryTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ describe("Binary Tree", function(){
ele.should.be.equal(arr.shift());
}
});

it('should return the number of edges from the root to the node', function(){
construct(tree);

tree.depth(tree.root.val).should.be.equal(0);
tree.depth(tree.root.left.val).should.be.equal(1);
tree.depth(tree.root.right.val).should.be.equal(1);
tree.depth(tree.root.left.left.val).should.be.equal(2);
tree.depth(tree.root.left.right.val).should.be.equal(2);
tree.depth(tree.root.right.left.val).should.be.equal(2);
tree.depth(tree.root.right.right.val).should.be.equal(2);
});

it('should delete when clear() is called', function(){
tree.root = BinaryTree.Node(1);
Expand Down
99 changes: 99 additions & 0 deletions tests/Recursion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

"use strict"

const Stack = require("../LinkedStack");
const BinaryTree = require("../BinaryTree");
const should = require('should');

const construct = function(tree){
tree.root = BinaryTree.Node(5);
tree.root.left = BinaryTree.Node(3);
tree.root.right = BinaryTree.Node(7);
tree.root.left.left = BinaryTree.Node(2);
tree.root.left.right = BinaryTree.Node(4);
tree.root.right.left = BinaryTree.Node(6);
tree.root.right.right = BinaryTree.Node(8);
}

function mock(n){
let stack = new Stack(), result = 0;
stack.push({num:n, status:0});

while(stack.length){
let cur = stack.peek();
let status = cur.status++;

if(status === 0){
if(cur.num===1){
result = 1;
stack.pop();
}
}else if (status === 1){
stack.push({num: cur.num -1, status:0});
}else if(status === 2){
result = result + cur.num;
stack.pop();
}
}

return result;
}

function *mockTraverse(tree){
let stack = new Stack(), cur = null;

stack.push({node: tree.root, status: 0});// calling function
let status = 0;
while(stack.length){
cur = stack.peek(); // executing function
status= cur.status ++;

if(status === 0){
if(!cur.node){
stack.pop();// remove context
}
}else if(status === 1){
stack.push({node: cur.node.left, status: 0});
}else if(status === 2){
yield cur.node.val;
}else if(status === 3){
stack.push({node: cur.node.right, status: 0});
}else{
stack.pop(); // remove context
}
//mock function stack end
}
}

function recursion(n){
if(n===1) return 1;

return n + recursion(n-1);
}

describe("Mock recursion", function(){
beforeEach(() => {

});

afterEach(() => {

});

it('should execute recursively', function(){
let n = 10;
recursion(n).should.be.equal(55);
});

it('should have same result with recursion using stack', function(){
let n = 10;
mock(n).should.be.equal(55);
});

it('should work without recursion', function(){
let tree = new BinaryTree(), arr = [ 2, 3, 4, 5, 6, 7, 8 ];
construct(tree);
[...tree.inOrder()].should.be.eql(arr);
[...mockTraverse(tree)].should.be.eql(arr);
});
});

0 comments on commit 2f9d6a3

Please sign in to comment.