ts-structures
Advanced tools
Comparing version 0.1.3 to 0.3.0
function guardOverflow(throwError, returnValue) { | ||
return (target, methodName, descriptor) => { | ||
const method = descriptor.value; | ||
descriptor.value = function (...args) { | ||
return function (target, methodName, descriptor) { | ||
var method = descriptor.value; | ||
descriptor.value = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
if (!this.capacity || | ||
@@ -10,3 +14,3 @@ this.capacity < 0 || | ||
if (throwError) | ||
throw new Error(`${target}.${methodName}: insertion aborted (limit of ${this.length} reached)`); | ||
throw new Error(target + "." + methodName + ": insertion aborted (limit of " + this.length + " reached)"); | ||
return returnValue; | ||
@@ -17,1 +21,2 @@ }; | ||
export { guardOverflow, }; | ||
//# sourceMappingURL=capped-structure.js.map |
@@ -1,5 +0,7 @@ | ||
import type { CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback } from './types'; | ||
import type { Comparer, CompareFunction } from './comparer'; | ||
import { compareMethods } from './comparer'; | ||
import type { FilterFunction, MapFunction, ReduceFunction, TraverseCallback } from './types'; | ||
import { TraverseMethod } from './types'; | ||
import CappedStructure, { guardOverflow } from './capped-structure'; | ||
export type { CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback, }; | ||
export { CappedStructure, guardOverflow, TraverseMethod, }; | ||
export type { Comparer, CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback, }; | ||
export { compareMethods, CappedStructure, guardOverflow, TraverseMethod, }; |
@@ -0,3 +1,5 @@ | ||
import { compareMethods, } from './comparer'; | ||
import { TraverseMethod, } from './types'; | ||
import { guardOverflow, } from './capped-structure'; | ||
export { guardOverflow, TraverseMethod, }; | ||
export { compareMethods, guardOverflow, TraverseMethod, }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,1 @@ | ||
declare type CompareFunction<T> = (a: T, b: T) => -1 | 0 | 1; | ||
declare type FilterFunction<T> = (currentValue: T) => boolean; | ||
@@ -12,3 +11,3 @@ declare type MapFunction<T, U> = (currentValue: T) => U; | ||
} | ||
export type { CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback, }; | ||
export type { FilterFunction, MapFunction, ReduceFunction, TraverseCallback, }; | ||
export { TraverseMethod, }; |
@@ -9,1 +9,2 @@ var TraverseMethod; | ||
export { TraverseMethod, }; | ||
//# sourceMappingURL=types.js.map |
import ListGraph from './list-graph'; | ||
export { ListGraph, }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,17 +0,18 @@ | ||
class Edge { | ||
constructor(vertex, weight) { | ||
var Edge = (function () { | ||
function Edge(vertex, weight) { | ||
this.vertex = vertex; | ||
this.weight = weight; | ||
} | ||
} | ||
class ListGraph { | ||
constructor() { | ||
return Edge; | ||
}()); | ||
var ListGraph = (function () { | ||
function ListGraph() { | ||
this.data = new Map(); | ||
} | ||
addVertex(vertex) { | ||
ListGraph.prototype.addVertex = function (vertex) { | ||
if (!this.data.has(vertex)) | ||
this.data.set(vertex, new Set()); | ||
return this; | ||
} | ||
removeVertex(vertex) { | ||
}; | ||
ListGraph.prototype.removeVertex = function (vertex) { | ||
if (!this.data.has(vertex)) | ||
@@ -21,6 +22,6 @@ return this; | ||
return this; | ||
} | ||
addEdge(vertexA, vertexB, weight) { | ||
const edgesA = this.data.get(vertexA); | ||
const edgesB = this.data.get(vertexB); | ||
}; | ||
ListGraph.prototype.addEdge = function (vertexA, vertexB, weight) { | ||
var edgesA = this.data.get(vertexA); | ||
var edgesB = this.data.get(vertexB); | ||
if (!edgesA || !edgesB) | ||
@@ -31,11 +32,13 @@ return this; | ||
return this; | ||
} | ||
removeEdge(vertexA, vertexB) { | ||
const edgesA = this.data.get(vertexA); | ||
const edgesB = this.data.get(vertexB); | ||
}; | ||
ListGraph.prototype.removeEdge = function (vertexA, vertexB) { | ||
var edgesA = this.data.get(vertexA); | ||
var edgesB = this.data.get(vertexB); | ||
if (!edgesA || !edgesB) | ||
return this; | ||
return this; | ||
} | ||
} | ||
}; | ||
return ListGraph; | ||
}()); | ||
export default ListGraph; | ||
//# sourceMappingURL=list-graph.js.map |
import type { CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback } from './_shared'; | ||
import { TraverseMethod } from './_shared'; | ||
import { DoublyLinkedList } from './list'; | ||
import { Queue } from './queue'; | ||
import { Queue, PriorityQueue } from './queue'; | ||
import { Stack } from './stack'; | ||
import { BinarySearchTree } from './tree'; | ||
import { BinaryHeap } from './heap'; | ||
export type { CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback, }; | ||
export { DoublyLinkedList, Queue, Stack, BinarySearchTree, TraverseMethod, }; | ||
export { DoublyLinkedList, Queue, PriorityQueue, Stack, BinarySearchTree, TraverseMethod, BinaryHeap, }; |
import { TraverseMethod } from './_shared'; | ||
import { DoublyLinkedList } from './list'; | ||
import { Queue } from './queue'; | ||
import { Queue, PriorityQueue, } from './queue'; | ||
import { Stack } from './stack'; | ||
import { BinarySearchTree } from './tree'; | ||
export { DoublyLinkedList, Queue, Stack, BinarySearchTree, TraverseMethod, }; | ||
import { BinaryHeap } from './heap'; | ||
export { DoublyLinkedList, Queue, PriorityQueue, Stack, BinarySearchTree, TraverseMethod, BinaryHeap, }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
class DoublyLinkedListNode { | ||
constructor(list, value, prev, next) { | ||
var DoublyLinkedListNode = (function () { | ||
function DoublyLinkedListNode(list, value, prev, next) { | ||
this.list = list; | ||
@@ -8,47 +8,48 @@ this.value = value; | ||
} | ||
} | ||
class DoublyLinkedList { | ||
constructor() { | ||
return DoublyLinkedListNode; | ||
}()); | ||
var DoublyLinkedList = (function () { | ||
function DoublyLinkedList() { | ||
this.length = 0; | ||
} | ||
push(value) { | ||
DoublyLinkedList.prototype.push = function (value) { | ||
return this.insertValue(value, this.tail, undefined); | ||
} | ||
pop() { | ||
}; | ||
DoublyLinkedList.prototype.pop = function () { | ||
return this.removeNode(this.tail); | ||
} | ||
unshift(value) { | ||
}; | ||
DoublyLinkedList.prototype.unshift = function (value) { | ||
return this.insertValue(value, undefined, this.head); | ||
} | ||
shift() { | ||
}; | ||
DoublyLinkedList.prototype.shift = function () { | ||
return this.removeNode(this.head); | ||
} | ||
insertBefore(value, before) { | ||
}; | ||
DoublyLinkedList.prototype.insertBefore = function (value, before) { | ||
if (before.list !== this) | ||
return undefined; | ||
return this.insertValue(value, before.prev, before); | ||
} | ||
insertAfter(value, after) { | ||
}; | ||
DoublyLinkedList.prototype.insertAfter = function (value, after) { | ||
if (after.list !== this) | ||
return undefined; | ||
return this.insertValue(value, after, after.next); | ||
} | ||
has(value) { | ||
}; | ||
DoublyLinkedList.prototype.has = function (value) { | ||
return !!this.get(value); | ||
} | ||
get(value) { | ||
for (let curr = this.head; curr; curr = curr.next) | ||
}; | ||
DoublyLinkedList.prototype.get = function (value) { | ||
for (var curr = this.head; curr; curr = curr.next) | ||
if (curr.value === value) | ||
return curr; | ||
return undefined; | ||
} | ||
getAll(value) { | ||
const results = []; | ||
for (let curr = this.head; curr; curr = curr.next) | ||
}; | ||
DoublyLinkedList.prototype.getAll = function (value) { | ||
var results = []; | ||
for (var curr = this.head; curr; curr = curr.next) | ||
if (curr.value === value) | ||
results.push(curr); | ||
return results; | ||
} | ||
insertValue(value, prev, next) { | ||
const node = new DoublyLinkedListNode(this, value, prev, next); | ||
}; | ||
DoublyLinkedList.prototype.insertValue = function (value, prev, next) { | ||
var node = new DoublyLinkedListNode(this, value, prev, next); | ||
!prev ? this.head = node : prev.next = node; | ||
@@ -58,4 +59,4 @@ !next ? this.tail = node : next.prev = node; | ||
return node; | ||
} | ||
removeNode(node) { | ||
}; | ||
DoublyLinkedList.prototype.removeNode = function (node) { | ||
if (!node || !this.head) | ||
@@ -73,12 +74,12 @@ return undefined; | ||
return node; | ||
} | ||
map(callback) { | ||
const newList = new DoublyLinkedList(); | ||
for (let curr = this.head; curr; curr = curr.next) | ||
}; | ||
DoublyLinkedList.prototype.map = function (callback) { | ||
var newList = new DoublyLinkedList(); | ||
for (var curr = this.head; curr; curr = curr.next) | ||
newList.push(callback(curr.value, curr, this)); | ||
return newList; | ||
} | ||
filter(callback) { | ||
const newList = new DoublyLinkedList(); | ||
for (let curr = this.head; curr; curr = curr.next) { | ||
}; | ||
DoublyLinkedList.prototype.filter = function (callback) { | ||
var newList = new DoublyLinkedList(); | ||
for (var curr = this.head; curr; curr = curr.next) { | ||
if (callback(curr.value, curr, this)) | ||
@@ -88,16 +89,18 @@ newList.push(curr.value); | ||
return newList; | ||
} | ||
reduce(callback, initialValue) { | ||
let acc = initialValue; | ||
for (let curr = this.head; curr; curr = curr.next) | ||
}; | ||
DoublyLinkedList.prototype.reduce = function (callback, initialValue) { | ||
var acc = initialValue; | ||
for (var curr = this.head; curr; curr = curr.next) | ||
acc = callback(acc, curr.value); | ||
return acc; | ||
} | ||
toArray() { | ||
const arr = []; | ||
for (let curr = this.head; curr; curr = curr.next) | ||
}; | ||
DoublyLinkedList.prototype.toArray = function () { | ||
var arr = []; | ||
for (var curr = this.head; curr; curr = curr.next) | ||
arr.push(curr.value); | ||
return arr; | ||
} | ||
} | ||
}; | ||
return DoublyLinkedList; | ||
}()); | ||
export default DoublyLinkedList; | ||
//# sourceMappingURL=doubly-linked-list.js.map |
import DoublyLinkedList from './doubly-linked-list'; | ||
export { DoublyLinkedList, }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "ts-structures", | ||
"version": "0.1.3", | ||
"version": "0.3.0", | ||
"description": "TypeScript implementation of common data structures", | ||
@@ -8,9 +8,9 @@ "main": "index.js", | ||
"typescript", | ||
"datastructures", | ||
"data", | ||
"structures", | ||
"list", | ||
"tree", | ||
"queue", | ||
"stack", | ||
"tree", | ||
"heap", | ||
"graph" | ||
@@ -27,20 +27,3 @@ ], | ||
"url": "https://github.com/gregoryalbouy/ts-datastructures/issues" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.14", | ||
"@types/reflect-metadata": "^0.1.0", | ||
"@typescript-eslint/eslint-plugin": "^4.1.1", | ||
"@typescript-eslint/parser": "^4.1.1", | ||
"coveralls": "^3.1.0", | ||
"eslint": "^7.9.0", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"jest": "^26.4.2", | ||
"ts-jest": "^26.3.0", | ||
"typedoc": "^0.19.1", | ||
"typescript": "^4.0.3" | ||
} | ||
} |
import Queue from './queue'; | ||
export { Queue, }; | ||
import PriorityQueue from './priority-queue'; | ||
export { Queue, PriorityQueue, }; |
import Queue from './queue'; | ||
export { Queue, }; | ||
import PriorityQueue from './priority-queue'; | ||
export { Queue, PriorityQueue, }; | ||
//# sourceMappingURL=index.js.map |
@@ -11,9 +11,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { guardOverflow, } from '../_shared'; | ||
class QueueNode { | ||
constructor(value) { | ||
var QueueNode = (function () { | ||
function QueueNode(value) { | ||
this.value = value; | ||
} | ||
} | ||
class Queue { | ||
constructor(capacity) { | ||
return QueueNode; | ||
}()); | ||
var Queue = (function () { | ||
function Queue(capacity) { | ||
this.length = 0; | ||
@@ -24,10 +25,10 @@ this.capacity = -1; | ||
} | ||
first() { | ||
Queue.prototype.first = function () { | ||
return this._first; | ||
} | ||
last() { | ||
}; | ||
Queue.prototype.last = function () { | ||
return this._last; | ||
} | ||
enqueue(value) { | ||
const node = new QueueNode(value); | ||
}; | ||
Queue.prototype.enqueue = function (value) { | ||
var node = new QueueNode(value); | ||
if (!this._first) | ||
@@ -39,4 +40,4 @@ this._first = node; | ||
return ++this.length; | ||
} | ||
dequeue() { | ||
}; | ||
Queue.prototype.dequeue = function () { | ||
if (!this._first) | ||
@@ -46,14 +47,16 @@ return undefined; | ||
this._last = undefined; | ||
const value = this._first.value; | ||
var value = this._first.value; | ||
this._first = this._first.next; | ||
this.length--; | ||
return value; | ||
} | ||
} | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Queue.prototype, "enqueue", null); | ||
}; | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Queue.prototype, "enqueue", null); | ||
return Queue; | ||
}()); | ||
export default Queue; | ||
//# sourceMappingURL=queue.js.map |
@@ -5,2 +5,4 @@ # TypeScript Datastructures | ||
[![Coverage Status](https://coveralls.io/repos/github/GregoryAlbouy/ts-datastructures/badge.svg?branch=master)](https://coveralls.io/github/GregoryAlbouy/ts-datastructures?branch=master) | ||
[![NPM version](https://img.shields.io/npm/v/ts-structures)](https://www.npmjs.org/package/ts-structures) | ||
[![NPM downloads](https://img.shields.io/npm/dt/ts-structures)](https://www.npmjs.org/package/ts-structures) | ||
@@ -10,8 +12,18 @@ _Work in progress_ | ||
Implementation of common data structures, TypeScript pendant of my [go-datastructures repo](https://github.com/gregoryalbouy/go-datastructures). | ||
(As a huge Go enthusiast, I must admit that working with a language that support Generics is quite a relief.) | ||
(As a huge Go enthusiast, I must admit that working with a language that support Generics is quite a relief for this kind of work) | ||
## About | ||
This package provides ready-to-use and functionnal data structures. It includes linked lists, queues, stack, binary heaps and I intent to implement a lot more. | ||
The package provides ready-to-use and functionnal data structures. It includes linked lists, queues, stack, binary heaps and I intent to implement a lot more. | ||
This project is also a pretext for the student developer I am to learn or practice many aspects of the development process: | ||
- :office: Understanding data structures | ||
- :vertical_traffic_light: Keeping clean code and good coding practices | ||
- :white_check_mark: Making relevant tests with high coverage rate | ||
- :arrows_counterclockwise: Using Continuous Integration tools | ||
- :blue_book: Maintaining a fully documented codebase | ||
Feedback of any kind is always appreciated! | ||
## Usage | ||
@@ -24,9 +36,3 @@ | ||
```typescript | ||
const { | ||
DoublyLinkedList, | ||
Queue, | ||
Stack, | ||
BinarySearchTree, | ||
//Graph, | ||
} = require('ts-structures') | ||
const { BinarySearchTree } = require('ts-structures') | ||
@@ -45,4 +51,6 @@ const tree = new BinarySearchTree() | ||
- [Queue](https://gregoryalbouy-ts-datastructures.netlify.app/classes/_queue_queue_.queue.html) | ||
- [Priority Queue](https://gregoryalbouy-ts-datastructures.netlify.app/classes/_queue_priority_queue_.priorityqueue.html) | ||
- [Stack](https://gregoryalbouy-ts-datastructures.netlify.app/classes/_stack_stack_.stack.html) | ||
- [Binary Search Tree](https://gregoryalbouy-ts-datastructures.netlify.app/classes/_tree_binary_search_tree_.binarysearchtree.html) | ||
- [Binary Heap](https://gregoryalbouy-ts-datastructures.netlify.app/classes/_heap_binary_heap_.binaryheap.html) | ||
- Graph *in progress* | ||
@@ -52,4 +60,3 @@ | ||
- Build npm package | ||
- More data structures | ||
- Benchmarks |
import Stack from './stack'; | ||
export { Stack, }; | ||
//# sourceMappingURL=index.js.map |
@@ -11,10 +11,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { guardOverflow, } from '../_shared'; | ||
class StackNode { | ||
constructor(value, next) { | ||
var StackNode = (function () { | ||
function StackNode(value, next) { | ||
this.value = value; | ||
this.next = next; | ||
} | ||
} | ||
class Stack { | ||
constructor(capacity) { | ||
return StackNode; | ||
}()); | ||
var Stack = (function () { | ||
function Stack(capacity) { | ||
this.length = 0; | ||
@@ -25,22 +26,24 @@ this.capacity = -1; | ||
} | ||
push(value) { | ||
const node = new StackNode(value, this.front); | ||
Stack.prototype.push = function (value) { | ||
var node = new StackNode(value, this.front); | ||
this.front = node; | ||
return ++this.length; | ||
} | ||
pop() { | ||
}; | ||
Stack.prototype.pop = function () { | ||
if (!this.front) | ||
return undefined; | ||
const value = this.front.value; | ||
var value = this.front.value; | ||
this.front = this.front.next; | ||
this.length--; | ||
return value; | ||
} | ||
} | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Stack.prototype, "push", null); | ||
}; | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Stack.prototype, "push", null); | ||
return Stack; | ||
}()); | ||
export default Stack; | ||
//# sourceMappingURL=stack.js.map |
@@ -1,3 +0,3 @@ | ||
import type { CompareFunction, FilterFunction, MapFunction, ReduceFunction } from '../_shared/types'; | ||
import { TraverseMethod } from '../_shared/types'; | ||
import type { Comparer, CompareFunction, FilterFunction, MapFunction, ReduceFunction } from '../_shared'; | ||
import { TraverseMethod } from '../_shared'; | ||
declare class BinarySearchTreeNode<T> { | ||
@@ -12,5 +12,10 @@ value: T; | ||
} | ||
declare class BinarySearchTree<T> { | ||
declare class BinarySearchTree<T> implements Comparer<T> { | ||
private _root?; | ||
private compare; | ||
compare: CompareFunction<T>; | ||
inf: <T_1>(this: any, a: T_1, b: T_1) => boolean; | ||
sup: <T_1>(this: any, a: T_1, b: T_1) => boolean; | ||
equal: <T_1>(this: any, a: T_1, b: T_1) => boolean; | ||
infOrEqual: <T_1>(this: any, a: T_1, b: T_1) => boolean; | ||
supOrEqual: <T_1>(this: any, a: T_1, b: T_1) => boolean; | ||
private traverseMethods; | ||
@@ -17,0 +22,0 @@ defaultTraverseMethod: TraverseMethod; |
@@ -1,20 +0,26 @@ | ||
import { TraverseMethod } from '../_shared/types'; | ||
import { compareMethods, TraverseMethod, } from '../_shared'; | ||
import { Queue } from '../queue'; | ||
class BinarySearchTreeNode { | ||
constructor(value) { | ||
var BinarySearchTreeNode = (function () { | ||
function BinarySearchTreeNode(value) { | ||
this.value = value; | ||
} | ||
hasChild(direction) { | ||
BinarySearchTreeNode.prototype.hasChild = function (direction) { | ||
return !!this.getChild(direction); | ||
} | ||
getChild(direction) { | ||
}; | ||
BinarySearchTreeNode.prototype.getChild = function (direction) { | ||
return direction === -1 ? this.left : this.right; | ||
} | ||
setChild(direction, newNode) { | ||
}; | ||
BinarySearchTreeNode.prototype.setChild = function (direction, newNode) { | ||
direction === -1 ? this.left = newNode : this.right = newNode; | ||
} | ||
} | ||
class BinarySearchTree { | ||
constructor(compareFunction) { | ||
this.compare = (a, b) => a < b ? -1 : a > b ? 1 : 0; | ||
}; | ||
return BinarySearchTreeNode; | ||
}()); | ||
var BinarySearchTree = (function () { | ||
function BinarySearchTree(compareFunction) { | ||
this.compare = compareMethods.compare.bind(this); | ||
this.inf = compareMethods.inf.bind(this); | ||
this.sup = compareMethods.sup.bind(this); | ||
this.equal = compareMethods.equal.bind(this); | ||
this.infOrEqual = compareMethods.infOrEqual.bind(this); | ||
this.supOrEqual = compareMethods.supOrEqual.bind(this); | ||
this.traverseMethods = [ | ||
@@ -30,17 +36,17 @@ this.traverseBFS.bind(this), | ||
} | ||
root() { | ||
BinarySearchTree.prototype.root = function () { | ||
return this._root; | ||
} | ||
setCompareFunction(compareFunction) { | ||
}; | ||
BinarySearchTree.prototype.setCompareFunction = function (compareFunction) { | ||
this.compare = compareFunction; | ||
const values = this.toArray(TraverseMethod.DFSPreOrder); | ||
var values = this.toArray(TraverseMethod.DFSPreOrder); | ||
this.clear(); | ||
values.forEach(this.insert.bind(this)); | ||
return this; | ||
} | ||
setDefaultTraverseMethod(traverseMethod) { | ||
}; | ||
BinarySearchTree.prototype.setDefaultTraverseMethod = function (traverseMethod) { | ||
this.defaultTraverseMethod = traverseMethod; | ||
} | ||
insert(value) { | ||
const newNode = new BinarySearchTreeNode(value); | ||
}; | ||
BinarySearchTree.prototype.insert = function (value) { | ||
var newNode = new BinarySearchTreeNode(value); | ||
if (!this._root) { | ||
@@ -50,4 +56,4 @@ this._root = newNode; | ||
} | ||
for (let currentNode = this._root; currentNode;) { | ||
const direction = this.compare(value, currentNode.value); | ||
for (var currentNode = this._root; currentNode;) { | ||
var direction = this.compare(value, currentNode.value); | ||
if (direction === 0) | ||
@@ -62,29 +68,32 @@ break; | ||
return false; | ||
} | ||
clear() { | ||
}; | ||
BinarySearchTree.prototype.clear = function () { | ||
this._root = undefined; | ||
} | ||
toArray(traverseMethod = this.defaultTraverseMethod) { | ||
const reduceFunction = (values, value) => { | ||
}; | ||
BinarySearchTree.prototype.toArray = function (traverseMethod) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
var reduceFunction = function (values, value) { | ||
values.push(value); | ||
return values; | ||
}; | ||
const initialValue = []; | ||
var initialValue = []; | ||
return this.reduce(reduceFunction, initialValue, traverseMethod); | ||
} | ||
map(mapFunction, traverseMethod = this.defaultTraverseMethod, newCompareFunction) { | ||
const newTree = new BinarySearchTree(newCompareFunction); | ||
}; | ||
BinarySearchTree.prototype.map = function (mapFunction, traverseMethod, newCompareFunction) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
var newTree = new BinarySearchTree(newCompareFunction); | ||
if (!this._root) | ||
return newTree; | ||
const traverse = this.traverseMethods[traverseMethod]; | ||
const callback = (value) => newTree.insert(mapFunction(value)); | ||
var traverse = this.traverseMethods[traverseMethod]; | ||
var callback = function (value) { return newTree.insert(mapFunction(value)); }; | ||
traverse(this._root, callback); | ||
return newTree; | ||
} | ||
filter(filterFunction, traverseMethod = this.defaultTraverseMethod) { | ||
const newTree = new BinarySearchTree(this.compare); | ||
}; | ||
BinarySearchTree.prototype.filter = function (filterFunction, traverseMethod) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
var newTree = new BinarySearchTree(this.compare); | ||
if (!this._root) | ||
return newTree; | ||
const traverse = this.traverseMethods[traverseMethod]; | ||
const callback = (value) => { | ||
var traverse = this.traverseMethods[traverseMethod]; | ||
var callback = function (value) { | ||
if (filterFunction(value)) | ||
@@ -95,21 +104,22 @@ newTree.insert(value); | ||
return newTree; | ||
} | ||
reduce(reduceFunction, initialValue, traverseMethod = this.defaultTraverseMethod) { | ||
}; | ||
BinarySearchTree.prototype.reduce = function (reduceFunction, initialValue, traverseMethod) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
if (!this._root) | ||
return initialValue; | ||
let accumulator = initialValue; | ||
const callback = (value) => { | ||
var accumulator = initialValue; | ||
var callback = function (value) { | ||
accumulator = reduceFunction(accumulator, value); | ||
}; | ||
const traverse = this.traverseMethods[traverseMethod]; | ||
var traverse = this.traverseMethods[traverseMethod]; | ||
traverse(this._root, callback); | ||
return accumulator; | ||
} | ||
traverseBFS(root, callback) { | ||
}; | ||
BinarySearchTree.prototype.traverseBFS = function (root, callback) { | ||
if (!root) | ||
return; | ||
const queue = new Queue(); | ||
var queue = new Queue(); | ||
queue.enqueue(root); | ||
while (queue.first()) { | ||
const currentTreeNode = queue.dequeue(), left = currentTreeNode.left, right = currentTreeNode.right, value = currentTreeNode.value; | ||
var currentTreeNode = queue.dequeue(), left = currentTreeNode.left, right = currentTreeNode.right, value = currentTreeNode.value; | ||
if (left) | ||
@@ -121,4 +131,4 @@ queue.enqueue(left); | ||
} | ||
} | ||
traverseDFS(order, currentNode, callback) { | ||
}; | ||
BinarySearchTree.prototype.traverseDFS = function (order, currentNode, callback) { | ||
if (order === TraverseMethod.DFSPreOrder) | ||
@@ -134,14 +144,16 @@ callback(currentNode.value); | ||
callback(currentNode.value); | ||
} | ||
traversePreOrder(root, callback) { | ||
}; | ||
BinarySearchTree.prototype.traversePreOrder = function (root, callback) { | ||
this.traverseDFS(TraverseMethod.DFSPreOrder, root, callback); | ||
} | ||
traverseInOrder(root, callback) { | ||
}; | ||
BinarySearchTree.prototype.traverseInOrder = function (root, callback) { | ||
this.traverseDFS(TraverseMethod.DFSInOrder, root, callback); | ||
} | ||
traversePostOrder(root, callback) { | ||
}; | ||
BinarySearchTree.prototype.traversePostOrder = function (root, callback) { | ||
this.traverseDFS(TraverseMethod.DFSPostOrder, root, callback); | ||
} | ||
} | ||
}; | ||
return BinarySearchTree; | ||
}()); | ||
export default BinarySearchTree; | ||
export { TraverseMethod }; | ||
//# sourceMappingURL=binary-search-tree.js.map |
import BinarySearchTree from './binary-search-tree'; | ||
export { BinarySearchTree, }; | ||
//# sourceMappingURL=index.js.map |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59223
0
56
898
57