Skip to content

Commit eb0a212

Browse files
Convert code.
1 parent 0556df2 commit eb0a212

File tree

16 files changed

+3552
-5357
lines changed

16 files changed

+3552
-5357
lines changed

dist/btree.d.ts

Lines changed: 399 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,399 @@
1+
import { BTreeNode } from "./btreenode";
2+
export declare class ExtendedWindow extends Window {
3+
DSinJS: {
4+
BTreeNode: typeof BTreeNode;
5+
BTree: typeof BTree;
6+
};
7+
BTreeNode: typeof BTreeNode;
8+
BTree: typeof BTree;
9+
}
10+
export declare class BTreeNodeStruct<T> {
11+
value?: T | null;
12+
lNode?: BTreeNodeStruct<T> | null;
13+
rNode?: BTreeNodeStruct<T> | null;
14+
}
15+
declare class BTreeRootAttrStruct<T> {
16+
root?: T;
17+
}
18+
declare class BTreeValueAttrStruct<T> {
19+
value?: T;
20+
}
21+
/**
22+
* BTree main class
23+
* @class
24+
* @public
25+
* @example
26+
* new BTree(10);
27+
* new BTree({ root: 10 });
28+
* new BTree({ value: 10 });
29+
*/
30+
export declare class BTree<T = any> {
31+
/**
32+
* Root node of the binary tree.
33+
* @type {BTreeNode}
34+
* @property root
35+
*/
36+
root: BTreeNode<T>;
37+
/**
38+
* Depth of the binary tree.
39+
* @type {number}
40+
* @property depth
41+
*/
42+
depth: number;
43+
/**
44+
* Constructor for Binary Tree.
45+
* @param {BTreeRootAttrStruct|BTreeValueAttrStruct|T} attr Can be of type object, string, number. In case of object root/value property is expected to be value of root node.
46+
* @constructor
47+
*/
48+
constructor(attr: BTreeRootAttrStruct<T> | BTreeValueAttrStruct<T> | T);
49+
/**
50+
* Returns string value of given tree.
51+
* @method toString
52+
* @member
53+
* @public
54+
* @example
55+
* var tree = new BTree(10);
56+
* tree.insert(10);
57+
* tree.insert(20);
58+
* tree.insert(30);
59+
* tree.toString(); // "10102030"
60+
*/
61+
toString(): string;
62+
/**
63+
* Returns JSON Form.
64+
* @method toJSON
65+
* @member
66+
* @public
67+
* @returns {BTreeNodeStruct} Returns json form of a given tree.
68+
* @example
69+
* var tree = new BTree(10);
70+
* tree.insert(20);
71+
* tree.toJSON(); // {value:10,lNode:{value:20,lNode:null,rNode:null},rNode:null}
72+
*/
73+
toJSON(): import("./btreenode").BTreeNodeStruct<T>;
74+
/**
75+
* Returns array value.
76+
* @method toArray
77+
* @member
78+
* @public
79+
* @returns {Array<BTreeNode>} Returns array form of given tree.
80+
* @example
81+
* var tree = new BTree(10);
82+
* tree.insert(20);
83+
* tree.toArray(); // => [{value:10,...},{value:20,...}]
84+
*/
85+
toArray(): BTreeNode<T>[];
86+
/**
87+
* Returns array of values of the tree.
88+
* @method toFlatArray
89+
* @member
90+
* @public
91+
* @returns {Array<T>} Returns array form of given tree.
92+
* @example
93+
* var tree = new BTree(10);
94+
* tree.insert(20);
95+
* tree.toFlatArray(); // => [10,20]
96+
*/
97+
toFlatArray(): T[];
98+
/**
99+
* Inserts the given value to the tree where first free left child node is found.
100+
* @param {any} val any type of value to be added to tree node.
101+
* @returns {BTreeNode} Returns newly created BTreeNode.
102+
* @method insert
103+
* @member
104+
* @example
105+
* var tree = new BTree(10);
106+
* tree.insert(10);
107+
* tree.insert(20);
108+
* tree.insert(30);
109+
* tree.toString(); // "10102030"
110+
*/
111+
insert(val: T): BTreeNode<T>;
112+
/**
113+
* Inserts the given value to the tree where first free left child node is found.
114+
* @param {T} val any type of value to be added to tree node.
115+
* @method insertLeftMost
116+
* @member
117+
* @returns {BTreeNode<T>} Returns newly created BTreeNode.
118+
*/
119+
insertLeftMost(val: T): BTreeNode<T>;
120+
/**
121+
* Inserts the given value to the tree where first free right child node is found.
122+
* @param {T} val any type of value to be added to tree node.
123+
* @method insertRightMost
124+
* @member
125+
* @public
126+
* @returns {BTreeNode<T>} Returns newly created BTreeNode.
127+
*/
128+
insertRightMost(val: T): BTreeNode<T>;
129+
/**
130+
* Deletes given value from tree.
131+
* Travarsal = Root -> L -> R.
132+
* @param {T} val Value to be removed.
133+
* @returns {BTreeNode<T>} Returns removed BTreeNode.
134+
* @method delete
135+
* @member
136+
* @public
137+
*/
138+
delete(val: T): BTreeNode<T>;
139+
/**
140+
* Inserts given element at given location. If location is already taken then it does not insert any value.
141+
* @param {T} val value to insert.
142+
* @param {number} index index at which to append new element to.
143+
* @method insertAt
144+
* @member
145+
* @public
146+
* @throws UnreachableError
147+
* @example
148+
* tree.insertAt(20,2);
149+
*/
150+
insertAt(val: T, index: number): void;
151+
/**
152+
* Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
153+
* @param {{(node: BTreeNode<T>, index: number) => any}} callback A callback function for execution of each node.
154+
* @method traverseBFS
155+
* @member
156+
* @public
157+
* @returns {void} no value.
158+
*/
159+
traverseBFS(callback: (node: BTreeNode<T>, index: number) => any): void;
160+
/**
161+
* Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
162+
* @param {{(node: BTreeNode<T>, index: number) => any}} callback A callback function for execution of each node.
163+
* @method traverseDFS
164+
* @member
165+
* @public
166+
* @returns {void} no value.
167+
*/
168+
traverseDFS(callback: (node: BTreeNode<T>, index: number) => any): void;
169+
/**
170+
* Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
171+
* @param {{(node: BTreeNode<T>, index: number) => any}} callback A callback function for execution of each node.
172+
* @method each
173+
* @member
174+
* @public
175+
* @returns {void} no value.
176+
*/
177+
each(callback: (node: BTreeNode<T>, index: number) => any): void;
178+
/**
179+
* Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
180+
* @param {{(node: BTreeNode<T>, index: number) => any}} callback A callback function for execution of each node.
181+
* @method forEach
182+
* @member
183+
* @public
184+
* @returns {void} no value.
185+
*/
186+
forEach(callback: (node: BTreeNode<T>, index: number) => any): void;
187+
/**
188+
* Returns an iterable of key, value pairs for every entry in the tree.
189+
* @method [Symbol.iterator]
190+
* @member
191+
* @public
192+
* @example
193+
* var tree = new BTree(10);
194+
* for (const node of tree) {
195+
* console.log(node.value); // 10
196+
* }
197+
*/
198+
[Symbol.iterator](): {
199+
/**
200+
* @returns { {value: BTreeNode<T>, done: boolean} }
201+
* @private
202+
*/
203+
next(): {
204+
value: BTreeNode<T>;
205+
done: boolean;
206+
};
207+
};
208+
/**
209+
* Returns an iterable of key, value pairs for every entry in the tree.
210+
* @method entries
211+
* @member
212+
* @public
213+
* @returns {IterableIterator<[number, BTreeNode<T>]>} Iterable for iterations.
214+
* @example
215+
* var tree = new BTree(10);
216+
* for (const [index, node] of tree.entries()) {
217+
* console.log(index, node.value); // 0, 10
218+
* }
219+
*/
220+
entries(): IterableIterator<[number, BTreeNode<T>]>;
221+
/**
222+
* Maps current tree values to a new tree with modifying the values using given callback function.
223+
* Uses BFS.
224+
* @param {{(value: T) => T}} callback callback function for value modifier.
225+
* @method map
226+
* @member
227+
* @public
228+
* @returns {BTree<T>} A new BTree
229+
* @example
230+
* var tree = BTree.fromArray([10, 20, 30, 40]);
231+
* var tree2 = tree.map(n => n * 2);
232+
* var arr2 = tree2.toArray(); // [{value:20,...},{value:40,...},{value:60,...},{value:80,...}]
233+
*/
234+
map(callback: (value: T) => T): BTree<T>;
235+
/**
236+
* Filters each item based on given filter function
237+
* @param {{(value: T) => boolean}} filterFnc callback function for filtering purpose.
238+
* @method filter
239+
* @member
240+
* @public
241+
* @throws FilteredRootError, Error when root node gets filtered out.
242+
* @returns {BTree<T>} New filtered instance of tree.
243+
* @example
244+
* var tree = BTree.fromArray([10, 20, 30, 40]);
245+
* var tree2 = tree.filter(n => !!(n % 4 === 0 || n === 10));
246+
* var arr2 = tree2.toArray(); // [{value:10,...},{value:20,...},{value:40,...}]
247+
*/
248+
filter(filterFnc: (value: T) => boolean): BTree<T>;
249+
/**
250+
* Reduces each node values using reduceFunction and returns final value.
251+
* @param {(next: T2, value: T, index: number, tree: BTree<T>) => T2} reduceFunction callback function for reducing each node value to a final value.
252+
* @param {T2} initialValue Optional, Accumulator/Initial value.
253+
* @method reduce<T2>
254+
* @member
255+
* @public
256+
* @returns {T2} Returns reduceed value
257+
* @example
258+
* var tree = BTree.fromArray([10, 20, 30, 40]);
259+
* var sum = tree.reduce((acc, node) => acc + node); // => 100
260+
*/
261+
reduce<T2 = any>(reduceFunction: (next: T2, value: T, index: number, tree: BTree<T>) => T2, initialValue?: T2): T2;
262+
/**
263+
* Reverses the current Binary Tree, Left Node becomes Right node and vise versa.
264+
* Does not return new instance, returns current tree instance.
265+
* @method reverse
266+
* @member
267+
* @public
268+
* @returns {BTree<T>} Returns current tree instance.
269+
* @example
270+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
271+
* tree.reverse().toArray(); // => [10, 30, 20, 70, 60, 50, 40, 80]
272+
*/
273+
reverse(): BTree<T>;
274+
/**
275+
* Returns first index of a value matched, if it is not present, it returns -1.
276+
* @param {T} value Any value to find.
277+
* @method indexOf
278+
* @member
279+
* @public
280+
* @returns {number} Returns index of given item.
281+
* @example
282+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
283+
* tree.indexOf(30); // => 3
284+
* tree.indexOf(51); // => -1
285+
*/
286+
indexOf(value: T): number;
287+
/**
288+
* Checks if given item exists or not, returns boolean.
289+
* @param {T} value Any value to check if it exists or not.
290+
* @method includes
291+
* @member
292+
* @public
293+
* @returns {boolean} Returns true if it is present, otherwise false.
294+
* @example
295+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
296+
* tree.includes(30); // true
297+
* tree.includes(51); // false
298+
*/
299+
includes(value: T): boolean;
300+
/**
301+
* Checks if given item exists or not, returns boolean.
302+
* @param {T} value Any value to check if it exists or not.
303+
* @method exists
304+
* @member
305+
* @public
306+
* @returns {boolean} Returns true if it is present, otherwise false.
307+
* @example
308+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
309+
* tree.exists(30); // true
310+
* tree.exists(51); // false
311+
*/
312+
exists(value: T): boolean;
313+
/**
314+
* Checks if given item exists or not, returns boolean.
315+
* @param {T} value Any value to check if it exists or not.
316+
* @method has
317+
* @member
318+
* @public
319+
* @returns {boolean} Returns true if it is present, otherwise false.
320+
* @example
321+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
322+
* tree.has(30); // true
323+
* tree.has(51); // false
324+
*/
325+
has(value: T): boolean;
326+
/**
327+
* Sorts the tree based on compare function, Has option to sort only at children level.
328+
* @param {Function} compareFnc Function used to determine the order of the elements. It is expected to return
329+
* a negative value if first argument is less than second argument, zero if they're equal and a positive
330+
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
331+
* ```ts
332+
* (a, b) => a - b)
333+
* ```
334+
* @param {boolean} atOnlyFirstChildLevel Optiona, Flag to specify if first child of each node should sorted. Default is `false`.
335+
* @method sort
336+
* @member
337+
* @public
338+
* @returns {void} Returns undefined.
339+
* @example
340+
* var tree = BTree.fromArray([10, 200, 100, 50, 60, 90, 5, 3]);
341+
* tree.sort().toFlatArray(); // => [3,5,10,50,60,90,100,200]
342+
*/
343+
sort(compareFnc?: <T2>(a: T2, b: T2) => number, atOnlyFirstChildLevel?: boolean): void;
344+
/**
345+
* Prints entire tree on the console, useful for logging and checking status.
346+
* @method print
347+
* @member
348+
* @public
349+
* @returns {void} Returns undefined.
350+
* @example
351+
* var tree = BTree.fromArray([1, 2, 3]);
352+
* tree.print();
353+
* 1 (1)
354+
* |- 2 (2)
355+
* |- 3 (3)
356+
*/
357+
print(): void;
358+
/**
359+
* Returns the first matched tree node. Traverses using BFS.
360+
* @param {T} item any value to find inside the tree.
361+
* @method find
362+
* @member
363+
* @public
364+
* @returns {BTreeNode<T> | null} Returns the first matched tree node, if not found, returns null.
365+
* @example
366+
*/
367+
find(item: T): BTreeNode<T> | null;
368+
/**
369+
* Returns index value from given path.
370+
* @param {Array<'U'|'L'|'R'>} path Array for U L or R, which represents the quickest path to get to a node.
371+
* @returns {number} Returns index value.
372+
* @method getIndexFromPath
373+
* @public
374+
* @static
375+
* @member
376+
*/
377+
static getIndexFromPath(path: Array<'U' | 'L' | 'R'>): number;
378+
/**
379+
* Returns Path equivalent to the given index.
380+
* @param {number} index Index number from which path to be calculated.
381+
* @returns {Array<'U'|'L'|'R'>} Path equivalent to the given index.
382+
* @method getPathFromIndex
383+
* @public
384+
* @static
385+
*/
386+
static getPathFromIndex(index: number): Array<'U' | 'L' | 'R'>;
387+
/**
388+
* Converts given values into a Binary Tree.
389+
* @param {T2[]} arr Any array of values.
390+
* @returns {BTree<T2>} Newly generated tree.
391+
* @method fromArray
392+
* @static
393+
* @public
394+
* @example
395+
* var tree = BTree.fromArray([10,20,30,40]);
396+
*/
397+
static fromArray<T2>(arr: T2[]): BTree<T2>;
398+
}
399+
export {};

0 commit comments

Comments
 (0)