Skip to content

Add Typescript support #56

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

Merged
merged 5 commits into from
Aug 9, 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
4 changes: 4 additions & 0 deletions . eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
lib/
demo/
typings/
9 changes: 4 additions & 5 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
"node": "current"
}
}
]
],
"@babel/preset-typescript"
],
"plugins": [
"babel-plugin-add-module-exports"
]
}
"plugins": ["babel-plugin-add-module-exports"]
}
44 changes: 11 additions & 33 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
{
"ecmaFeatures": {
"globalReturn": true,
"jsx": true,
"modules": true
},
"env": {
"browser": true,
"es6": true,
"node": true
},
"globals": {
"document": false,
"escape": false,
"navigator": false,
"unescape": false,
"window": false,
"describe": true,
"before": true,
"it": true,
"expect": true,
"sinon": true
},
"parser": "babel-eslint",
"plugins": [],
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
// ... lots of lots of rules here
"@typescript-eslint/explicit-module-boundary-types": "off"
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
}
}
"env": { "es6": true }
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
lib/
coverage/
npm-debug.log
npm-debug.log
typings/
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
"coveralls": "jest --coverage && coveralls < coverage/lcov.info",
"prepare": "npm run build & npm run dev",
"publish:npm": "npm publish --access public",
"test:jest": "jest"
"test:jest": "jest",
"lint": "eslint src/ --ext .ts"
},
"jest": {
"moduleFileExtensions": [
"js",
"jsx"
"jsx",
"ts",
"tsx"
],
"moduleDirectories": [
"node_modules",
Expand All @@ -52,20 +55,27 @@
"@babel/cli": "^7.10.0",
"@babel/core": "^7.10.0",
"@babel/preset-env": "^7.11.0",
"@babel/preset-typescript": "^7.10.4",
"@types/jest": "^26.0.9",
"@typescript-eslint/eslint-plugin": "^3.8.0",
"@typescript-eslint/parser": "^3.8.0",
"@webpack-cli/migrate": "^0.1.0",
"babel-eslint": "^8.2.6",
"babel-jest": "^26.2.2",
"babel-loader": "^7.1.5",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-polyfill": "^6.26.0",
"coveralls": "^3.0.2",
"eslint": "^4.18.2",
"eslint": "^7.6.0",
"eslint-loader": "^2.1.0",
"eslint-plugin-import": "^2.13.0",
"istanbul": "^0.4.5",
"jest": "^26.2.2",
"npm": "^6.14.7",
"phantomjs-prebuilt": "^2.1.15",
"ts-loader": "^8.0.2",
"tslint": "^5.11.0",
"typescript": "^3.9.7",
"uglifyjs-webpack-plugin": "^0.4.6",
"webpack-cli": "^3.1.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import { BST } from './binary-search-tree';
import { BST } from "./binary-search-tree";

describe('Binary Search Tree', () => {
let bst;
describe("Binary Search Tree", () => {
let bst: BST<number>;

beforeEach(() => {
bst = new BST();
bst = new BST<number>();
});

it('should create bst object', () => {
it("should create bst object", () => {
expect(bst).toBeDefined();
});

it('should create BST with null root', () => {
it("should create BST with null root", () => {
expect(bst.root).toBe(null);
expect(bst.len).toBe(0);
});

describe('Height of the node', () => {
describe("Height of the node", () => {
beforeEach(() => {
bst.insert(5);
bst.insert(6);
bst.insert(4);
bst.insert(2);
});

it('should have height of node 4 as 1', () => {
it("should have height of node 4 as 1", () => {
expect(bst.height(bst.lookup(4).currentNode)).toBe(1);
});

it('should have height of tree = 2', () => {
it("should have height of tree = 2", () => {
expect(bst.height()).toBe(2);
});

it('should have height 0 of leaf nodes', () => {
it("should have height 0 of leaf nodes", () => {
expect(bst.height(bst.lookup(6).currentNode)).toBe(0);
});

it('should have height 0 of leaf nodes', () => {
it("should have height 0 of leaf nodes", () => {
expect(bst.height(bst.lookup(2).currentNode)).toBe(0);
});
});

describe('Insertion operation', () => {
it('should insert values in the BST', () => {
describe("Insertion operation", () => {
it("should insert values in the BST", () => {
bst.insert(5);
expect(bst.root.key).toBe(5);
expect(bst.len).toBe(1);
Expand All @@ -57,37 +57,37 @@ describe('Binary Search Tree', () => {
});
});

describe('Lookup operation', () => {
describe("Lookup operation", () => {
beforeEach(() => {
bst.insert(5);
bst.insert(6);
bst.insert(4);
bst.insert(2);
});

it('should lookup for value 6 in the bst and return the node with key 6 and its parent node', () => {
it("should lookup for value 6 in the bst and return the node with key 6 and its parent node", () => {
const findNode = bst.lookup(6);
expect(findNode.hasVal).toBeTruthy();
expect(findNode.currentNode.key).toBe(6);
expect(findNode.parentNode.key).toBe(5);
});

it('should lookup for value 100 in the bst and should return the currentNode and parentNode as null', () => {
it("should lookup for value 100 in the bst and should return the currentNode and parentNode as null", () => {
const findNode = bst.lookup(100);
expect(findNode.hasVal).toBeFalsy();
expect(findNode.currentNode).toBe(null);
expect(findNode.parentNode).toBe(null);
});

it('should lookup for value 5 and should return currentNode as 5 and parentNode as null', () => {
it("should lookup for value 5 and should return currentNode as 5 and parentNode as null", () => {
const findNode = bst.lookup(5);
expect(findNode.hasVal).toBeTruthy();
expect(findNode.currentNode.key).toBe(5);
expect(findNode.parentNode).toBe(null);
});
});

describe('Delete operation', () => {
describe("Delete operation", () => {
beforeEach(() => {
bst.insert(11);
bst.insert(6);
Expand All @@ -102,11 +102,11 @@ describe('Binary Search Tree', () => {
bst.insert(49);
});

it('should have 11 nodes in the bst', () => {
it("should have 11 nodes in the bst", () => {
expect(bst.len).toBe(11);
});

it('should delete leaf with value 5', () => {
it("should delete leaf with value 5", () => {
expect(bst.len).toBe(11);

const lookUpFor5 = bst.lookup(5);
Expand All @@ -119,7 +119,7 @@ describe('Binary Search Tree', () => {
expect(lookUpFor5.parentNode.right).toBe(null);
});

it('should delete node 8, with one child', () => {
it("should delete node 8, with one child", () => {
expect(bst.len).toBe(11);

const lookUpFor8 = bst.lookup(8);
Expand All @@ -133,7 +133,7 @@ describe('Binary Search Tree', () => {
expect(lookUpFor8.parentNode.right.key).toBe(10);
});

it('should delete node 19, with two children', () => {
it("should delete node 19, with two children", () => {
expect(bst.len).toBe(11);

const lookupFor19 = bst.lookup(19);
Expand All @@ -146,7 +146,10 @@ describe('Binary Search Tree', () => {
const successor = bst.findMin(lookupFor19.currentNode.right);
expect(successor.subtree.key).toBe(31);

const dir = lookupFor19.currentNode.key > lookupFor19.parentNode.key ? 'right' : 'left';
const dir =
lookupFor19.currentNode.key > lookupFor19.parentNode.key
? "right"
: "left";

bst.delete(19);
expect(bst.len).toBe(10);
Expand All @@ -155,7 +158,7 @@ describe('Binary Search Tree', () => {
});
});

describe('Traversal Operation', () => {
describe("Traversal Operation", () => {
beforeEach(() => {
bst.insert(11);
bst.insert(6);
Expand All @@ -169,20 +172,68 @@ describe('Binary Search Tree', () => {
bst.insert(31);
bst.insert(49);
});
it('should return inorder list', () => {
expect(bst.traverse('inOrder')).toEqual([4, 5, 6, 8, 10, 11, 17, 19, 31, 43, 49]);
});

it('should return preorder list', () => {
expect(bst.traverse('preOrder')).toEqual([11, 6, 4, 5, 8, 10, 19, 17, 43, 31, 49]);
});

it('should return postorder list', () => {
expect(bst.traverse('postOrder')).toEqual([5, 4, 10, 8, 6, 17, 31, 49, 43, 19, 11]);
});

it('should return bfs list', () => {
expect(bst.traverse('levelOrder')).toEqual([11, 6, 19, 4, 8, 17, 43, 5, 10, 31, 49]);
it("should return inorder list", () => {
expect(bst.traverse("inOrder")).toEqual([
4,
5,
6,
8,
10,
11,
17,
19,
31,
43,
49
]);
});

it("should return preorder list", () => {
expect(bst.traverse("preOrder")).toEqual([
11,
6,
4,
5,
8,
10,
19,
17,
43,
31,
49
]);
});

it("should return postorder list", () => {
expect(bst.traverse("postOrder")).toEqual([
5,
4,
10,
8,
6,
17,
31,
49,
43,
19,
11
]);
});

it("should return bfs list", () => {
expect(bst.traverse("levelOrder")).toEqual([
11,
6,
19,
4,
8,
17,
43,
5,
10,
31,
49
]);
});
});
});
});
Loading