diff --git a/CHANGELOG.md b/CHANGELOG.md
index a6c4d84..798f63e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
-## [v1.41.8](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
+## [v1.41.9](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
### Changes
diff --git a/README.md b/README.md
index 1efbc38..1af0d62 100644
--- a/README.md
+++ b/README.md
@@ -658,40 +658,40 @@ optimal approach to data structure design.
[//]: # (Start of Replace Section)
avl-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 30.29 | 33.01 | 3.64e-4 |
10,000 add & delete randomly | 68.10 | 14.68 | 0.00 |
10,000 addMany | 39.54 | 25.29 | 4.07e-4 |
10,000 get | 26.72 | 37.42 | 3.77e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 30.18 | 33.13 | 2.75e-4 |
10,000 add & delete randomly | 65.65 | 15.23 | 0.00 |
10,000 addMany | 39.56 | 25.28 | 3.25e-4 |
10,000 get | 26.57 | 37.63 | 1.92e-4 |
binary-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 add randomly | 12.88 | 77.63 | 1.02e-4 |
1,000 add & delete randomly | 15.90 | 62.88 | 1.08e-4 |
1,000 addMany | 10.59 | 94.41 | 8.39e-5 |
1,000 get | 18.01 | 55.53 | 1.95e-4 |
1,000 dfs | 69.11 | 14.47 | 6.47e-4 |
1,000 bfs | 54.42 | 18.38 | 4.20e-4 |
1,000 morris | 37.14 | 26.92 | 2.27e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 add randomly | 13.00 | 76.93 | 3.84e-4 |
1,000 add & delete randomly | 16.11 | 62.06 | 3.76e-4 |
1,000 addMany | 10.63 | 94.04 | 1.30e-4 |
1,000 get | 18.24 | 54.84 | 5.04e-4 |
1,000 dfs | 69.77 | 14.33 | 4.07e-4 |
1,000 bfs | 54.49 | 18.35 | 4.96e-4 |
1,000 morris | 37.10 | 26.96 | 2.35e-4 |
bst
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 28.48 | 35.11 | 2.29e-4 |
10,000 add & delete randomly | 64.82 | 15.43 | 0.01 |
10,000 addMany | 28.74 | 34.80 | 9.06e-4 |
10,000 get | 27.38 | 36.52 | 1.82e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 31.88 | 31.37 | 3.10e-4 |
10,000 add & delete randomly | 73.08 | 13.68 | 0.01 |
10,000 addMany | 28.80 | 34.73 | 0.00 |
10,000 get | 27.59 | 36.25 | 2.19e-4 |
rb-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add randomly | 72.30 | 13.83 | 0.00 |
100,000 add & 1000 delete randomly | 81.37 | 12.29 | 0.01 |
100,000 getNode | 59.48 | 16.81 | 9.29e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add randomly | 70.97 | 14.09 | 0.00 |
100,000 add & 1000 delete randomly | 82.03 | 12.19 | 0.01 |
100,000 getNode | 59.75 | 16.74 | 0.00 |
directed-graph
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 addVertex | 0.10 | 9786.77 | 5.56e-7 |
1,000 addEdge | 6.02 | 166.04 | 1.27e-4 |
1,000 getVertex | 0.05 | 2.18e+4 | 3.02e-7 |
1,000 getEdge | 23.41 | 42.71 | 0.00 |
tarjan | 223.51 | 4.47 | 0.01 |
tarjan all | 224.89 | 4.45 | 0.00 |
topologicalSort | 181.90 | 5.50 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 addVertex | 0.10 | 9894.62 | 1.57e-6 |
1,000 addEdge | 6.17 | 162.15 | 0.00 |
1,000 getVertex | 0.05 | 2.17e+4 | 4.06e-7 |
1,000 getEdge | 23.50 | 42.56 | 0.00 |
tarjan | 223.18 | 4.48 | 0.01 |
tarjan all | 226.10 | 4.42 | 0.01 |
topologicalSort | 186.20 | 5.37 | 0.02 |
heap
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add & pop | 4.62 | 216.33 | 3.06e-5 |
10,000 fib add & pop | 351.41 | 2.85 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add & pop | 4.64 | 215.30 | 4.51e-5 |
10,000 fib add & pop | 351.83 | 2.84 | 0.00 |
doubly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 unshift | 214.84 | 4.65 | 0.04 |
1,000,000 unshift & shift | 167.11 | 5.98 | 0.04 |
1,000,000 insertBefore | 335.78 | 2.98 | 0.07 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 unshift | 216.19 | 4.63 | 0.04 |
1,000,000 unshift & shift | 164.84 | 6.07 | 0.02 |
1,000,000 insertBefore | 325.14 | 3.08 | 0.07 |
singly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 push & pop | 212.53 | 4.71 | 0.01 |
10,000 insertBefore | 243.94 | 4.10 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 push & pop | 213.24 | 4.69 | 0.01 |
10,000 insertBefore | 247.69 | 4.04 | 0.01 |
max-priority-queue
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 11.43 | 87.50 | 1.84e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 11.50 | 86.97 | 1.99e-4 |
deque
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 216.07 | 4.63 | 0.05 |
1,000,000 shift | 24.97 | 40.05 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 223.30 | 4.48 | 0.08 |
1,000,000 shift | 24.86 | 40.23 | 0.00 |
queue
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 42.57 | 23.49 | 0.01 |
1,000,000 push & shift | 79.94 | 12.51 | 9.99e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 42.26 | 23.66 | 0.01 |
1,000,000 push & shift | 79.22 | 12.62 | 0.00 |
trie
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 push | 54.02 | 18.51 | 0.00 |
100,000 getWords | 82.83 | 12.07 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 push | 52.92 | 18.90 | 0.00 |
100,000 getWords | 83.37 | 11.99 | 0.00 |
[//]: # (End of Replace Section)
\ No newline at end of file
diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts
index 6a9378d..68416a1 100644
--- a/src/data-structures/binary-tree/binary-tree.ts
+++ b/src/data-structures/binary-tree/binary-tree.ts
@@ -108,8 +108,7 @@ export class BinaryTreeNode = BinaryTree
* @template N - The type of the binary tree's nodes.
*/
export class BinaryTree = BinaryTreeNode>>
- implements IBinaryTree
-{
+ implements IBinaryTree {
iterationType: IterationType = IterationType.ITERATIVE;
/**
@@ -391,7 +390,7 @@ export class BinaryTree = BinaryTreeNode
return -1;
}
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
let maxHeight = 0;
while (stack.length > 0) {
@@ -846,6 +845,27 @@ export class BinaryTree = BinaryTreeNode
return this.isSubtreeBST(this.root, iterationType);
}
+ subTreeTraverse>(
+ callback?: C,
+ beginRoot?: BTNKey | N | null,
+ iterationType?: IterationType,
+ includeNull?: false
+ ): ReturnType[]
+
+ subTreeTraverse>(
+ callback?: C,
+ beginRoot?: BTNKey | N | null,
+ iterationType?: IterationType,
+ includeNull?: undefined
+ ): ReturnType[]
+
+ subTreeTraverse>(
+ callback?: C,
+ beginRoot?: BTNKey | N | null,
+ iterationType?: IterationType,
+ includeNull?: true
+ ): ReturnType[]
+
/**
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
* node, either recursively or iteratively.
@@ -858,40 +878,79 @@ export class BinaryTree = BinaryTreeNode
* start from the root of the tree.
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
* performed on the binary tree. It can have two possible values:
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `subTreeTraverse` returns an array of `ReturnType>`.
*/
- subTreeTraverse>(
+ subTreeTraverse>(
callback: C = this.defaultOneParamCallback as C,
beginRoot: BTNKey | N | null = this.root,
- iterationType = this.iterationType
+ iterationType = this.iterationType,
+ includeNull = false
): ReturnType[] {
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
- const ans: ReturnType>[] = [];
+ const ans: (ReturnType> | null)[] = [];
if (!beginRoot) return ans;
if (iterationType === IterationType.RECURSIVE) {
- const _traverse = (cur: N) => {
- ans.push(callback(cur));
- cur.left && _traverse(cur.left);
- cur.right && _traverse(cur.right);
+ const _traverse = (cur: N | null) => {
+ if (cur !== undefined) {
+ ans.push(callback(cur));
+ if (includeNull) {
+ cur !== null && cur.left !== undefined && _traverse(cur.left);
+ cur !== null && cur.right !== undefined && _traverse(cur.right);
+ } else {
+ cur !== null && cur.left && _traverse(cur.left);
+ cur !== null && cur.right && _traverse(cur.right);
+ }
+ }
};
_traverse(beginRoot);
} else {
- const stack: N[] = [beginRoot];
+ const stack: (N| null)[] = [beginRoot];
while (stack.length > 0) {
- const cur = stack.pop()!;
-
- ans.push(callback(cur));
- cur.right && stack.push(cur.right);
- cur.left && stack.push(cur.left);
+ const cur = stack.pop();
+ if (cur !== undefined) {
+ ans.push(callback(cur));
+ if (includeNull) {
+ cur !== null && cur.right !== undefined && stack.push(cur.right);
+ cur !== null && cur.left !== undefined && stack.push(cur.left);
+ } else {
+ cur !== null && cur.right && stack.push(cur.right);
+ cur !== null && cur.left && stack.push(cur.left);
+ }
+ }
}
}
return ans;
}
+ dfs>(
+ callback?: C,
+ pattern?: DFSOrderPattern,
+ beginRoot?: N | null,
+ iterationType?: IterationType,
+ includeNull?: false
+ ): ReturnType[]
+
+ dfs>(
+ callback?: C,
+ pattern?: DFSOrderPattern,
+ beginRoot?: N | null,
+ iterationType?: IterationType,
+ includeNull?: undefined
+ ): ReturnType[]
+
+ dfs>(
+ callback?: C,
+ pattern?: DFSOrderPattern,
+ beginRoot?: N | null,
+ iterationType?: IterationType,
+ includeNull?: true
+ ): ReturnType[]
+
/**
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
* function on each node according to a specified order pattern.
@@ -905,34 +964,53 @@ export class BinaryTree = BinaryTreeNode
* is `null`, an empty array will be returned.
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
* iteration used in the depth-first search algorithm. It can have two possible values:
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `dfs` returns an array of `ReturnType>` values.
*/
- dfs>(
+ dfs>(
callback: C = this.defaultOneParamCallback as C,
pattern: DFSOrderPattern = 'in',
beginRoot: N | null = this.root,
- iterationType: IterationType = IterationType.ITERATIVE
+ iterationType: IterationType = IterationType.ITERATIVE,
+ includeNull = false
): ReturnType[] {
if (!beginRoot) return [];
- const ans: ReturnType>[] = [];
+ const ans: ReturnType[] = [];
if (iterationType === IterationType.RECURSIVE) {
- const _traverse = (node: N) => {
+ const _traverse = (node: N | null) => {
switch (pattern) {
case 'in':
- if (node.left) _traverse(node.left);
- ans.push(callback(node));
- if (node.right) _traverse(node.right);
+ if (includeNull) {
+ if (node !== null && node.left !== undefined) _traverse(node.left);
+ ans.push(callback(node));
+ if (node !== null && node.right !== undefined) _traverse(node.right);
+ } else {
+ if (node !== null && node.left) _traverse(node.left);
+ ans.push(callback(node));
+ if (node !== null && node.right) _traverse(node.right);
+ }
break;
case 'pre':
- ans.push(callback(node));
-
- if (node.left) _traverse(node.left);
- if (node.right) _traverse(node.right);
+ if (includeNull) {
+ ans.push(callback(node));
+ if (node !== null && node.left !== undefined) _traverse(node.left);
+ if (node !== null && node.right !== undefined) _traverse(node.right);
+ } else {
+ ans.push(callback(node));
+ if (node !== null && node.left) _traverse(node.left);
+ if (node !== null && node.right) _traverse(node.right);
+ }
break;
case 'post':
- if (node.left) _traverse(node.left);
- if (node.right) _traverse(node.right);
- ans.push(callback(node));
+ if (includeNull) {
+ if (node !== null && node.left !== undefined) _traverse(node.left);
+ if (node !== null && node.right !== undefined) _traverse(node.right);
+ ans.push(callback(node));
+ } else {
+ if (node !== null && node.left) _traverse(node.left);
+ if (node !== null && node.right) _traverse(node.right);
+ ans.push(callback(node));
+ }
break;
}
@@ -941,34 +1019,39 @@ export class BinaryTree = BinaryTreeNode
_traverse(beginRoot);
} else {
// 0: visit, 1: print
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
+ const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
while (stack.length > 0) {
const cur = stack.pop();
- if (!cur || !cur.node) continue;
+ if (cur === undefined) continue;
+ if (includeNull) {
+ if (cur.node === undefined) continue;
+ } else {
+ if (cur.node === null || cur.node === undefined) continue;
+ }
if (cur.opt === 1) {
ans.push(callback(cur.node));
} else {
switch (pattern) {
case 'in':
- stack.push({opt: 0, node: cur.node.right});
+ cur.node && stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 1, node: cur.node});
- stack.push({opt: 0, node: cur.node.left});
+ cur.node && stack.push({opt: 0, node: cur.node.left});
break;
case 'pre':
- stack.push({opt: 0, node: cur.node.right});
- stack.push({opt: 0, node: cur.node.left});
+ cur.node && stack.push({opt: 0, node: cur.node.right});
+ cur.node && stack.push({opt: 0, node: cur.node.left});
stack.push({opt: 1, node: cur.node});
break;
case 'post':
stack.push({opt: 1, node: cur.node});
- stack.push({opt: 0, node: cur.node.right});
- stack.push({opt: 0, node: cur.node.left});
+ cur.node && stack.push({opt: 0, node: cur.node.right});
+ cur.node && stack.push({opt: 0, node: cur.node.left});
break;
default:
- stack.push({opt: 0, node: cur.node.right});
+ cur.node && stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 1, node: cur.node});
- stack.push({opt: 0, node: cur.node.left});
+ cur.node && stack.push({opt: 0, node: cur.node.left});
break;
}
}
@@ -978,6 +1061,27 @@ export class BinaryTree = BinaryTreeNode
return ans;
}
+ bfs>(
+ callback?: C,
+ beginRoot?: N | null,
+ iterationType?: IterationType,
+ includeNull?: false
+ ): ReturnType[]
+
+ bfs>(
+ callback?: C,
+ beginRoot?: N | null,
+ iterationType?: IterationType,
+ includeNull?: undefined
+ ): ReturnType[]
+
+ bfs>(
+ callback?: C,
+ beginRoot?: N | null,
+ iterationType?: IterationType,
+ includeNull?: true
+ ): ReturnType[]
+
/**
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
* function on each node.
@@ -989,19 +1093,21 @@ export class BinaryTree = BinaryTreeNode
* will not be performed and an empty array will be returned.
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
* in the breadth-first search (BFS) algorithm. It can have two possible values:
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `bfs` returns an array of `ReturnType>[]`.
*/
- bfs>(
+ bfs>(
callback: C = this.defaultOneParamCallback as C,
beginRoot: N | null = this.root,
- iterationType = this.iterationType
+ iterationType = this.iterationType,
+ includeNull = false
): ReturnType[] {
if (!beginRoot) return [];
const ans: ReturnType>[] = [];
if (iterationType === IterationType.RECURSIVE) {
- const queue = new Queue([beginRoot]);
+ const queue: Queue = new Queue([beginRoot]);
const traverse = (level: number) => {
if (queue.size === 0) return;
@@ -1009,15 +1115,21 @@ export class BinaryTree = BinaryTreeNode
const current = queue.shift()!;
ans.push(callback(current));
- if (current.left) queue.push(current.left);
- if (current.right) queue.push(current.right);
+ if (includeNull) {
+ if (current && current.left !== undefined) queue.push(current.left);
+ if (current && current.right !== undefined) queue.push(current.right);
+ } else {
+ if (current.left) queue.push(current.left);
+ if (current.right) queue.push(current.right);
+ }
+
traverse(level + 1);
};
traverse(0);
} else {
- const queue = new Queue([beginRoot]);
+ const queue = new Queue([beginRoot]);
while (queue.size > 0) {
const levelSize = queue.size;
@@ -1025,14 +1137,41 @@ export class BinaryTree = BinaryTreeNode
const current = queue.shift()!;
ans.push(callback(current));
- if (current.left) queue.push(current.left);
- if (current.right) queue.push(current.right);
+ if (includeNull) {
+ if (current !== null && current.left !== undefined) queue.push(current.left);
+ if (current !== null && current.right !== undefined) queue.push(current.right);
+ } else {
+ if (current.left) queue.push(current.left);
+ if (current.right) queue.push(current.right);
+ }
+
}
}
}
return ans;
}
+ listLevels>(
+ callback?: C ,
+ beginRoot?: N | null ,
+ iterationType?: IterationType,
+ includeNull?: false
+ ): ReturnType[][]
+
+ listLevels>(
+ callback?: C ,
+ beginRoot?: N | null ,
+ iterationType?: IterationType,
+ includeNull?: undefined
+ ): ReturnType[][]
+
+ listLevels>(
+ callback?: C ,
+ beginRoot?: N | null ,
+ iterationType?: IterationType,
+ includeNull?: true
+ ): ReturnType[][]
+
/**
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
* of arrays representing the levels of the tree.
@@ -1044,29 +1183,36 @@ export class BinaryTree = BinaryTreeNode
* from the root node of the binary tree.
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
* recursively or iteratively. It can have two possible values:
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `listLevels` returns an array of arrays, where each inner array represents a
* level in a binary tree. Each inner array contains the return type of the provided callback
* function `C` applied to the nodes at that level.
*/
- listLevels>(
+ listLevels>(
callback: C = this.defaultOneParamCallback as C,
beginRoot: N | null = this.root,
- iterationType = this.iterationType
+ iterationType = this.iterationType,
+ includeNull = false
): ReturnType[][] {
if (!beginRoot) return [];
const levelsNodes: ReturnType[][] = [];
if (iterationType === IterationType.RECURSIVE) {
- const _recursive = (node: N, level: number) => {
+ const _recursive = (node: N | null, level: number) => {
if (!levelsNodes[level]) levelsNodes[level] = [];
levelsNodes[level].push(callback(node));
- if (node.left) _recursive(node.left, level + 1);
- if (node.right) _recursive(node.right, level + 1);
+ if (includeNull) {
+ if (node && node.left !== undefined) _recursive(node.left, level + 1);
+ if (node && node.right !== undefined) _recursive(node.right, level + 1);
+ } else {
+ if (node && node.left) _recursive(node.left, level + 1);
+ if (node && node.right) _recursive(node.right, level + 1);
+ }
};
_recursive(beginRoot, 0);
} else {
- const stack: [N, number][] = [[beginRoot, 0]];
+ const stack: [N | null, number][] = [[beginRoot, 0]];
while (stack.length > 0) {
const head = stack.pop()!;
@@ -1074,8 +1220,14 @@ export class BinaryTree = BinaryTreeNode
if (!levelsNodes[level]) levelsNodes[level] = [];
levelsNodes[level].push(callback(node));
- if (node.right) stack.push([node.right, level + 1]);
- if (node.left) stack.push([node.left, level + 1]);
+
+ if (includeNull) {
+ if (node && node.right !== undefined) stack.push([node.right, level + 1]);
+ if (node && node.left !== undefined) stack.push([node.left, level + 1]);
+ } else {
+ if (node && node.right) stack.push([node.right, level + 1]);
+ if (node && node.left) stack.push([node.left, level + 1]);
+ }
}
}
@@ -1231,7 +1383,7 @@ export class BinaryTree = BinaryTreeNode
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
* binary tree nodes in a specific order.
*/
- *[Symbol.iterator](node = this.root): Generator {
+ * [Symbol.iterator](node = this.root): Generator {
if (!node) {
return;
}
diff --git a/test/unit/data-structures/binary-tree/binary-tree.test.ts b/test/unit/data-structures/binary-tree/binary-tree.test.ts
index 5970b72..e70a3f8 100644
--- a/test/unit/data-structures/binary-tree/binary-tree.test.ts
+++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts
@@ -174,8 +174,11 @@ describe('BinaryTree', () => {
});
it('should subTreeTraverse', () => {
- tree.addMany([4, 2, 6, 1, 3, 5, 7]);
- expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6, 5, 7]);
+ tree.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
+ expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.ITERATIVE)).toEqual([6,3, 7]);
+ expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6,3, 7]);
+ expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.ITERATIVE, true)).toEqual([6, 3, 7, null]);
+ expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.RECURSIVE, true)).toEqual([6, 3, 7, null]);
});
it('should clear the tree', () => {
@@ -266,10 +269,18 @@ describe('BinaryTree traversals', () => {
const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
tree.refill(arr);
+ expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
+ expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
+ expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
+ expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
+
expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
expect(tree.dfs(node => node.key, 'pre', tree.root, IterationType.RECURSIVE)).toEqual([
35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55
]);
+ expect(tree.dfs(node => node, 'pre', tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
+ expect(tree.dfs(node => node, 'pre', tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
+
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
expect(tree.dfs(node => node.key, 'post', tree.root, IterationType.RECURSIVE)).toEqual([
@@ -282,9 +293,7 @@ describe('BinaryTree traversals', () => {
35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
]);
- const levels = tree.listLevels(node => node.key);
- expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
- isDebug && console.log(levels);
+ expect(tree.listLevels(node => node.key)).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
expect(tree.listLevels(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
[35],
@@ -292,9 +301,21 @@ describe('BinaryTree traversals', () => {
[15, 29, 50],
[16, 28, 30, 45, 55]
]);
- isDebug && console.log(levels);
+ expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.ITERATIVE, true)).toEqual([
+ [35],
+ [20, 40],
+ [15, 29, null,50],
+ [null, 16, 28, 30, 45, 55]
+ ]);
+ expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.RECURSIVE, true)).toEqual([
+ [35],
+ [20, 40],
+ [15, 29, null,50],
+ [null, 16, 28, 30, 45, 55]
+ ]);
});
+
describe('BinaryTree', () => {
let tree: BinaryTree;