Refactor: The default callback function now supports returning values for null and undefined nodes with undefined keys. The deleteVertex method in AbstractGraph has been made abstract. Unnecessary magnitude variables have been removed from the magnitude function. fix: In directed and undirected graphs, the deleteVertex method now removes edges from adjacent nodes when deleting a vertex.

This commit is contained in:
Revone 2023-12-21 10:01:04 +08:00
parent bbfa64fc64
commit c3db558607
21 changed files with 286 additions and 227 deletions

View file

@ -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.49.2](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.49.3](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
### Changes

135
README.md
View file

@ -196,10 +196,54 @@ our [visual tool](https://github.com/zrwusa/vivid-algorithm)
## Code Snippets
### Binary Search Tree (BST) snippet
### RedBlackTree snippet
#### TS
```ts
import {RedBlackTree} from 'data-structure-typed';
const rbTree = new RedBlackTree<number>();
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
rbTree.isAVLBalanced(); // true
rbTree.delete(10);
rbTree.isAVLBalanced(); // true
rbTree.print()
// ___6________
// / \
// ___4_ ___11________
// / \ / \
// _2_ 5 _8_ ____14__
// / \ / \ / \
// 1 3 7 9 12__ 15__
// \ \
// 13 16
```
#### JS
```js
import {RedBlackTree} from 'data-structure-typed';
const rbTree = new RedBlackTree();
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
rbTree.isAVLBalanced(); // true
rbTree.delete(10);
rbTree.isAVLBalanced(); // true
rbTree.print()
// ___6________
// / \
// ___4_ ___11________
// / \ / \
// _2_ 5 _8_ ____14__
// / \ / \ / \
// 1 3 7 9 12__ 15__
// \ \
// 13 16
```
### Binary Search Tree (BST) snippet
```ts
import {BST, BSTNode} from 'data-structure-typed';
@ -259,31 +303,6 @@ objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
objBST.delete(11);
```
#### JS
```js
const {BST, BSTNode} = require('data-structure-typed');
const bst = new BST();
bst.add(11);
bst.add(3);
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
bst.size === 16; // true
bst.has(6); // true
const node6 = bst.getNode(6);
bst.getHeight(6) === 2; // true
bst.getHeight() === 5; // true
bst.getDepth(6) === 3; // true
const leftMost = bst.getLeftMost();
leftMost?.key === 1; // true
bst.delete(6);
bst.get(6); // undefined
bst.isAVLBalanced(); // true or false
const bfsIDs = bst.bfs();
bfsIDs[0] === 11; // true
```
### AVLTree snippet
```ts
@ -296,28 +315,6 @@ avlTree.delete(10);
avlTree.isAVLBalanced(); // true
```
### RedBlackTree snippet
```ts
import {RedBlackTree} from 'data-structure-typed';
const rbTree = new RedBlackTree<number>();
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
rbTree.isAVLBalanced(); // true
rbTree.delete(10);
rbTree.isAVLBalanced(); // true
rbTree.print()
// ___6________
// / \
// ___4_ ___11________
// / \ / \
// _2_ 5 _8_ ____14__
// / \ / \ / \
// 1 3 7 9 12__ 15__
// \ \
// 13 16
```
### Directed Graph simple snippet
```ts
@ -672,7 +669,7 @@ avl2.print();
</tbody>
</table>
## Standard library data structure comparison
## The corresponding relationships between data structures in different language standard libraries.
<table style="display: table; width:100%; table-layout: fixed;">
<thead>
@ -686,9 +683,15 @@ avl2.print();
<tbody>
<tr>
<td>Heap&lt;E&gt;</td>
<td>-</td>
<td>-</td>
<td>heapq</td>
</tr>
<tr>
<td>PriorityQueue&lt;E&gt;</td>
<td>priority_queue&lt;T&gt;</td>
<td>PriorityQueue&lt;E&gt;</td>
<td>heapq</td>
<td>-</td>
</tr>
<tr>
<td>Deque&lt;E&gt;</td>
@ -981,52 +984,52 @@ avl2.print();
[//]: # (No deletion!!! Start of Replace Section)
<div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>avl-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>50.74</td><td>19.71</td><td>0.00</td></tr><tr><td>10,000 add & delete randomly</td><td>127.76</td><td>7.83</td><td>0.02</td></tr><tr><td>10,000 addMany</td><td>57.14</td><td>17.50</td><td>0.00</td></tr><tr><td>10,000 get</td><td>52.22</td><td>19.15</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>48.42</td><td>20.65</td><td>0.00</td></tr><tr><td>10,000 add & delete randomly</td><td>107.72</td><td>9.28</td><td>0.02</td></tr><tr><td>10,000 addMany</td><td>55.40</td><td>18.05</td><td>6.22e-4</td></tr><tr><td>10,000 get</td><td>53.89</td><td>18.56</td><td>0.02</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>binary-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 add randomly</td><td>18.32</td><td>54.59</td><td>0.00</td></tr><tr><td>1,000 add & delete randomly</td><td>26.54</td><td>37.69</td><td>0.01</td></tr><tr><td>1,000 addMany</td><td>18.79</td><td>53.22</td><td>0.00</td></tr><tr><td>1,000 get</td><td>18.95</td><td>52.78</td><td>0.00</td></tr><tr><td>1,000 has</td><td>19.76</td><td>50.60</td><td>0.01</td></tr><tr><td>1,000 dfs</td><td>159.96</td><td>6.25</td><td>0.01</td></tr><tr><td>1,000 bfs</td><td>73.63</td><td>13.58</td><td>0.08</td></tr><tr><td>1,000 morris</td><td>225.93</td><td>4.43</td><td>0.05</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 add randomly</td><td>17.75</td><td>56.35</td><td>2.23e-4</td></tr><tr><td>1,000 add & delete randomly</td><td>25.58</td><td>39.10</td><td>0.01</td></tr><tr><td>1,000 addMany</td><td>19.65</td><td>50.89</td><td>0.00</td></tr><tr><td>1,000 get</td><td>21.03</td><td>47.55</td><td>0.01</td></tr><tr><td>1,000 has</td><td>19.81</td><td>50.48</td><td>0.01</td></tr><tr><td>1,000 dfs</td><td>183.37</td><td>5.45</td><td>0.03</td></tr><tr><td>1,000 bfs</td><td>65.61</td><td>15.24</td><td>0.02</td></tr><tr><td>1,000 morris</td><td>231.00</td><td>4.33</td><td>0.06</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>bst</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>48.80</td><td>20.49</td><td>2.79e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>110.72</td><td>9.03</td><td>0.00</td></tr><tr><td>10,000 addMany</td><td>46.19</td><td>21.65</td><td>0.00</td></tr><tr><td>10,000 get</td><td>49.28</td><td>20.29</td><td>7.92e-4</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>49.96</td><td>20.02</td><td>7.65e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>116.77</td><td>8.56</td><td>0.02</td></tr><tr><td>10,000 addMany</td><td>49.06</td><td>20.38</td><td>0.01</td></tr><tr><td>10,000 get</td><td>49.06</td><td>20.38</td><td>7.13e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>rb-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add</td><td>80.84</td><td>12.37</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>206.65</td><td>4.84</td><td>0.01</td></tr><tr><td>100,000 getNode</td><td>57.42</td><td>17.42</td><td>0.00</td></tr><tr><td>100,000 add & iterator</td><td>109.59</td><td>9.12</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add</td><td>83.42</td><td>11.99</td><td>0.01</td></tr><tr><td>100,000 add & delete randomly</td><td>243.05</td><td>4.11</td><td>0.07</td></tr><tr><td>100,000 getNode</td><td>218.87</td><td>4.57</td><td>0.05</td></tr><tr><td>100,000 add & iterator</td><td>124.22</td><td>8.05</td><td>0.01</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>comparison</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>SRC PQ 10,000 add</td><td>0.14</td><td>6917.74</td><td>1.81e-6</td></tr><tr><td>CJS PQ 10,000 add</td><td>0.15</td><td>6883.53</td><td>3.84e-6</td></tr><tr><td>MJS PQ 10,000 add</td><td>0.57</td><td>1761.70</td><td>5.07e-6</td></tr><tr><td>SRC PQ 10,000 add & poll</td><td>3.45</td><td>289.74</td><td>3.63e-4</td></tr><tr><td>CJS PQ 10,000 add & poll</td><td>3.53</td><td>283.14</td><td>4.73e-5</td></tr><tr><td>MJS PQ 10,000 add & poll</td><td>3.31</td><td>302.38</td><td>3.64e-5</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>SRC PQ 10,000 add</td><td>0.15</td><td>6710.40</td><td>1.90e-5</td></tr><tr><td>CJS PQ 10,000 add</td><td>0.16</td><td>6407.42</td><td>4.25e-5</td></tr><tr><td>MJS PQ 10,000 add</td><td>0.62</td><td>1602.37</td><td>1.51e-4</td></tr><tr><td>SRC PQ 10,000 add & poll</td><td>3.67</td><td>272.42</td><td>0.00</td></tr><tr><td>CJS PQ 10,000 add & poll</td><td>3.95</td><td>252.90</td><td>0.00</td></tr><tr><td>MJS PQ 10,000 add & poll</td><td>3.33</td><td>300.28</td><td>8.65e-5</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>directed-graph</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.10</td><td>9860.53</td><td>9.32e-7</td></tr><tr><td>1,000 addEdge</td><td>6.34</td><td>157.71</td><td>8.55e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.16e+4</td><td>4.61e-7</td></tr><tr><td>1,000 getEdge</td><td>22.67</td><td>44.12</td><td>0.00</td></tr><tr><td>tarjan</td><td>217.59</td><td>4.60</td><td>0.01</td></tr><tr><td>tarjan all</td><td>6489.86</td><td>0.15</td><td>0.09</td></tr><tr><td>topologicalSort</td><td>179.22</td><td>5.58</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.11</td><td>8958.10</td><td>3.30e-5</td></tr><tr><td>1,000 addEdge</td><td>6.37</td><td>156.98</td><td>1.96e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.04e+4</td><td>8.22e-6</td></tr><tr><td>1,000 getEdge</td><td>24.49</td><td>40.83</td><td>0.00</td></tr><tr><td>tarjan</td><td>235.54</td><td>4.25</td><td>0.04</td></tr><tr><td>tarjan all</td><td>6766.74</td><td>0.15</td><td>0.32</td></tr><tr><td>topologicalSort</td><td>197.52</td><td>5.06</td><td>0.04</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>hash-map</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 set</td><td>108.75</td><td>9.20</td><td>0.04</td></tr><tr><td>Native Map 1,000,000 set</td><td>217.55</td><td>4.60</td><td>0.02</td></tr><tr><td>Native Set 1,000,000 add</td><td>179.67</td><td>5.57</td><td>0.03</td></tr><tr><td>1,000,000 set & get</td><td>122.66</td><td>8.15</td><td>0.03</td></tr><tr><td>Native Map 1,000,000 set & get</td><td>282.47</td><td>3.54</td><td>0.04</td></tr><tr><td>Native Set 1,000,000 add & has</td><td>174.48</td><td>5.73</td><td>0.02</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>336.83</td><td>2.97</td><td>0.06</td></tr><tr><td>Native Map 1,000,000 ObjKey set & get</td><td>314.00</td><td>3.18</td><td>0.06</td></tr><tr><td>Native Set 1,000,000 ObjKey add & has</td><td>267.84</td><td>3.73</td><td>0.03</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 set</td><td>117.82</td><td>8.49</td><td>0.05</td></tr><tr><td>Native Map 1,000,000 set</td><td>220.92</td><td>4.53</td><td>0.03</td></tr><tr><td>Native Set 1,000,000 add</td><td>187.44</td><td>5.34</td><td>0.02</td></tr><tr><td>1,000,000 set & get</td><td>125.44</td><td>7.97</td><td>0.04</td></tr><tr><td>Native Map 1,000,000 set & get</td><td>300.10</td><td>3.33</td><td>0.06</td></tr><tr><td>Native Set 1,000,000 add & has</td><td>200.88</td><td>4.98</td><td>0.02</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>361.00</td><td>2.77</td><td>0.06</td></tr><tr><td>Native Map 1,000,000 ObjKey set & get</td><td>335.34</td><td>2.98</td><td>0.09</td></tr><tr><td>Native Set 1,000,000 ObjKey add & has</td><td>261.28</td><td>3.83</td><td>0.07</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>heap</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>80.49</td><td>12.42</td><td>0.00</td></tr><tr><td>100,000 add & dfs</td><td>34.01</td><td>29.40</td><td>3.88e-4</td></tr><tr><td>10,000 fib add & pop</td><td>359.70</td><td>2.78</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>80.43</td><td>12.43</td><td>0.00</td></tr><tr><td>100,000 add & dfs</td><td>36.95</td><td>27.07</td><td>0.00</td></tr><tr><td>10,000 fib add & pop</td><td>386.63</td><td>2.59</td><td>0.05</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>doubly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>229.17</td><td>4.36</td><td>0.06</td></tr><tr><td>1,000,000 unshift</td><td>220.53</td><td>4.53</td><td>0.06</td></tr><tr><td>1,000,000 unshift & shift</td><td>172.12</td><td>5.81</td><td>0.03</td></tr><tr><td>1,000,000 addBefore</td><td>309.58</td><td>3.23</td><td>0.06</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>235.15</td><td>4.25</td><td>0.07</td></tr><tr><td>1,000,000 unshift</td><td>245.36</td><td>4.08</td><td>0.08</td></tr><tr><td>1,000,000 unshift & shift</td><td>175.53</td><td>5.70</td><td>0.03</td></tr><tr><td>1,000,000 addBefore</td><td>319.21</td><td>3.13</td><td>0.06</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>singly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push & shift</td><td>211.62</td><td>4.73</td><td>0.06</td></tr><tr><td>10,000 push & pop</td><td>219.72</td><td>4.55</td><td>0.03</td></tr><tr><td>10,000 addBefore</td><td>249.09</td><td>4.01</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push & shift</td><td>202.02</td><td>4.95</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>228.77</td><td>4.37</td><td>0.04</td></tr><tr><td>10,000 addBefore</td><td>274.25</td><td>3.65</td><td>0.05</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>max-priority-queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 refill & poll</td><td>8.96</td><td>111.61</td><td>1.80e-4</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 refill & poll</td><td>9.39</td><td>106.51</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>priority-queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>106.14</td><td>9.42</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add & poll</td><td>114.36</td><td>8.74</td><td>0.02</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>deque</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>13.91</td><td>71.89</td><td>4.15e-4</td></tr><tr><td>1,000,000 push & pop</td><td>22.82</td><td>43.83</td><td>2.45e-4</td></tr><tr><td>100,000 push & shift</td><td>2.38</td><td>420.49</td><td>3.61e-5</td></tr><tr><td>Native Array 100,000 push & shift</td><td>2718.62</td><td>0.37</td><td>0.35</td></tr><tr><td>100,000 unshift & shift</td><td>2.28</td><td>438.78</td><td>4.18e-4</td></tr><tr><td>Native Array 100,000 unshift & shift</td><td>4065.01</td><td>0.25</td><td>0.21</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>14.81</td><td>67.51</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>25.34</td><td>39.47</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>2.49</td><td>400.86</td><td>7.97e-4</td></tr><tr><td>Native Array 100,000 push & shift</td><td>2390.92</td><td>0.42</td><td>0.17</td></tr><tr><td>100,000 unshift & shift</td><td>2.48</td><td>403.14</td><td>6.46e-4</td></tr><tr><td>Native Array 100,000 unshift & shift</td><td>4462.41</td><td>0.22</td><td>0.34</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>44.46</td><td>22.49</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>5.16</td><td>193.83</td><td>0.00</td></tr><tr><td>Native Array 100,000 push & shift</td><td>2195.56</td><td>0.46</td><td>0.29</td></tr><tr><td>Native Array 100,000 push & pop</td><td>4.40</td><td>227.04</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>51.99</td><td>19.24</td><td>0.03</td></tr><tr><td>100,000 push & shift</td><td>5.23</td><td>191.34</td><td>7.00e-4</td></tr><tr><td>Native Array 100,000 push & shift</td><td>2400.96</td><td>0.42</td><td>0.28</td></tr><tr><td>Native Array 100,000 push & pop</td><td>4.36</td><td>229.52</td><td>1.14e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>stack</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>44.05</td><td>22.70</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>49.72</td><td>20.11</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>43.55</td><td>22.96</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>55.29</td><td>18.09</td><td>0.01</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>trie</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>44.33</td><td>22.56</td><td>0.00</td></tr><tr><td>100,000 getWords</td><td>88.47</td><td>11.30</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>48.66</td><td>20.55</td><td>0.00</td></tr><tr><td>100,000 getWords</td><td>95.09</td><td>10.52</td><td>0.01</td></tr></table></div>
</div>
[//]: # (No deletion!!! End of Replace Section)

View file

@ -199,9 +199,51 @@ const {
## 代码片段
### 二叉搜索树 (BST) 代码示例
### 红黑树 代码示例
#### TS
```ts
import {RedBlackTree} from 'data-structure-typed';
const rbTree = new RedBlackTree<number>();
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
rbTree.isAVLBalanced(); // true
rbTree.delete(10);
rbTree.isAVLBalanced(); // true
rbTree.print()
// ___6________
// / \
// ___4_ ___11________
// / \ / \
// _2_ 5 _8_ ____14__
// / \ / \ / \
// 1 3 7 9 12__ 15__
// \ \
// 13 16
```
#### JS
```js
import {RedBlackTree} from 'data-structure-typed';
const rbTree = new RedBlackTree();
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
rbTree.isAVLBalanced(); // true
rbTree.delete(10);
rbTree.isAVLBalanced(); // true
rbTree.print()
// ___6________
// / \
// ___4_ ___11________
// / \ / \
// _2_ 5 _8_ ____14__
// / \ / \ / \
// 1 3 7 9 12__ 15__
// \ \
// 13 16
```
### 二叉搜索树 (BST) 代码示例
```ts
import {BST, BSTNode} from 'data-structure-typed';
@ -262,31 +304,6 @@ objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
objBST.delete(11);
```
#### JS
```js
const {BST, BSTNode} = require('data-structure-typed');
const bst = new BST();
bst.add(11);
bst.add(3);
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
bst.size === 16; // true
bst.has(6); // true
const node6 = bst.getNode(6);
bst.getHeight(6) === 2; // true
bst.getHeight() === 5; // true
bst.getDepth(6) === 3; // true
const leftMost = bst.getLeftMost();
leftMost?.key === 1; // true
bst.delete(6);
bst.get(6); // undefined
bst.isAVLBalanced(); // true or false
const bfsIDs = bst.bfs();
bfsIDs[0] === 11; // true
```
### AVL树 代码示例
```ts
@ -299,28 +316,6 @@ avlTree.delete(10);
avlTree.isAVLBalanced(); // true
```
### 红黑树 代码示例
```ts
import {RedBlackTree} from 'data-structure-typed';
const rbTree = new RedBlackTree<number>();
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
rbTree.isAVLBalanced(); // true
rbTree.delete(10);
rbTree.isAVLBalanced(); // true
rbTree.print()
// ___6________
// / \
// ___4_ ___11________
// / \ / \
// _2_ 5 _8_ ____14__
// / \ / \ / \
// 1 3 7 9 12__ 15__
// \ \
// 13 16
```
### 有向图代码示例
```ts

View file

@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.49.2",
"version": "1.49.3",
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",

View file

@ -1957,7 +1957,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
}
}
protected _defaultOneParamCallback = (node: N) => node.key;
protected _defaultOneParamCallback = (node: N | null | undefined) => node ? node.key : undefined;
/**
* Swap the data of two nodes in the binary tree.

View file

@ -173,19 +173,7 @@ export abstract class AbstractGraph<
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
*/
/**
* Time Complexity: O(1) - Constant time for Map operations.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
*
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
* (`VertexKey`).
* @returns The method is returning a boolean value.
*/
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
const vertexKey = this._getVertexKey(vertexOrKey);
return this._vertexMap.delete(vertexKey);
}
abstract deleteVertex(vertexOrKey: VO | VertexKey): boolean;
/**
* Time Complexity: O(K), where K is the number of vertexMap to be removed.

View file

@ -240,7 +240,7 @@ export class DirectedGraph<
* (`VertexKey`).
* @returns The method is returning a boolean value.
*/
override deleteVertex(vertexOrKey: VO | VertexKey): boolean {
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
let vertexKey: VertexKey;
let vertex: VO | undefined;
if (this.isVertexKey(vertexOrKey)) {
@ -252,8 +252,12 @@ export class DirectedGraph<
}
if (vertex) {
this._outEdgeMap.delete(vertex)
this._inEdgeMap.delete(vertex)
const neighbors = this.getNeighbors(vertex);
for (const neighbor of neighbors) {
this._inEdgeMap.delete(neighbor);
}
this._outEdgeMap.delete(vertex);
this._inEdgeMap.delete(vertex);
}
return this._vertexMap.delete(vertexKey);

View file

@ -212,7 +212,7 @@ export class UndirectedGraph<
* (`VertexKey`).
* @returns The method is returning a boolean value.
*/
override deleteVertex(vertexOrKey: VO | VertexKey): boolean {
deleteVertex(vertexOrKey: VO | VertexKey): boolean {
let vertexKey: VertexKey;
let vertex: VO | undefined;
if (this.isVertexKey(vertexOrKey)) {

View file

@ -33,8 +33,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
this._tail = undefined;
this._size = 0;
if (elements) {
for (const el of elements)
this.push(el);
for (const el of elements) this.push(el);
}
}

View file

@ -16,7 +16,7 @@ import { getRandomIntArray, magnitude } from '../../../utils';
import { isCompetitor } from '../../../config';
const suite = new Benchmark.Suite();
const { TEN_THOUSAND, HUNDRED_THOUSAND, LINEAR } = magnitude;
const { TEN_THOUSAND, HUNDRED_THOUSAND, MILLION } = magnitude;
const cOrderedMap = new OrderedMap<number, number>();
const arrHundredThousand = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSAND, true);
@ -96,10 +96,10 @@ if (isCompetitor) {
hm.getElementByKey(i);
}
})
.add(`CPT LL ${LINEAR.toLocaleString()} unshift`, () => {
.add(`CPT LL ${MILLION.toLocaleString()} unshift`, () => {
const list = new CLinkedList<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
list.pushFront(i);
}
})
@ -114,33 +114,33 @@ if (isCompetitor) {
pq.pop();
}
})
.add(`CPT DQ ${LINEAR.toLocaleString()} push`, () => {
.add(`CPT DQ ${MILLION.toLocaleString()} push`, () => {
const deque = new CDeque<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
deque.pushBack(i);
}
})
.add(`CPT Q ${LINEAR.toLocaleString()} push`, () => {
.add(`CPT Q ${MILLION.toLocaleString()} push`, () => {
const queue = new CQueue<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
})
.add(`CPT ST ${LINEAR.toLocaleString()} push`, () => {
.add(`CPT ST ${MILLION.toLocaleString()} push`, () => {
const queue = new CStack<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
})
.add(`CPT ST ${LINEAR.toLocaleString()} push & pop`, () => {
.add(`CPT ST ${MILLION.toLocaleString()} push & pop`, () => {
const queue = new CStack<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.pop();
}
});

View file

@ -5,61 +5,50 @@ import { magnitude } from '../../../utils';
import { isCompetitor } from '../../../config';
const suite = new Benchmark.Suite();
const { LINEAR } = magnitude;
const { MILLION } = magnitude;
suite.add(`${LINEAR.toLocaleString()} push`, () => {
suite.add(`${MILLION.toLocaleString()} push`, () => {
const list = new DoublyLinkedList<number>();
for (let i = 0; i < LINEAR; i++) {
list.push(i);
}
for (let i = 0; i < MILLION; i++) list.push(i);
});
if (isCompetitor) {
suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
const list = new CLinkedList<number>();
for (let i = 0; i < LINEAR; i++) {
list.pushBack(i);
}
for (let i = 0; i < MILLION; i++) list.pushBack(i);
});
}
suite.add(`${LINEAR.toLocaleString()} unshift`, () => {
suite.add(`${MILLION.toLocaleString()} unshift`, () => {
const list = new DoublyLinkedList<number>();
for (let i = 0; i < LINEAR; i++) {
list.unshift(i);
}
for (let i = 0; i < MILLION; i++) list.unshift(i);
});
if (isCompetitor) {
suite.add(`CPT ${LINEAR.toLocaleString()} unshift`, () => {
suite.add(`CPT ${MILLION.toLocaleString()} unshift`, () => {
const list = new CLinkedList<number>();
for (let i = 0; i < LINEAR; i++) {
list.pushFront(i);
}
for (let i = 0; i < MILLION; i++) list.pushFront(i);
});
}
suite
.add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
.add(`${MILLION.toLocaleString()} unshift & shift`, () => {
const list = new DoublyLinkedList<number>();
for (let i = 0; i < LINEAR; i++) {
list.unshift(i);
}
for (let i = 0; i < LINEAR; i++) {
list.shift();
}
for (let i = 0; i < MILLION; i++) list.unshift(i);
for (let i = 0; i < MILLION; i++) list.shift();
})
.add(`${LINEAR.toLocaleString()} addBefore`, () => {
.add(`${MILLION.toLocaleString()} addBefore`, () => {
const doublyList = new DoublyLinkedList<number>();
let midNode: DoublyLinkedListNode | undefined;
const midIndex = Math.floor(LINEAR / 2);
for (let i = 0; i < LINEAR; i++) {
const midIndex = Math.floor(MILLION / 2);
for (let i = 0; i < MILLION; i++) {
doublyList.push(i);
if (i === midIndex) {
midNode = doublyList.getNode(i);

View file

@ -9,24 +9,16 @@ suite
.add(`${MILLION.toLocaleString()} push & shift`, () => {
const list = new SinglyLinkedList<number>();
for (let i = 0; i < MILLION; i++) {
list.push(i);
}
for (let i = 0; i < MILLION; i++) list.push(i);
for (let i = 0; i < MILLION; i++) {
list.shift();
}
for (let i = 0; i < MILLION; i++) list.shift();
})
.add(`${TEN_THOUSAND.toLocaleString()} push & pop`, () => {
const list = new SinglyLinkedList<number>();
for (let i = 0; i < TEN_THOUSAND; i++) {
list.push(i);
}
for (let i = 0; i < TEN_THOUSAND; i++) list.push(i);
for (let i = 0; i < TEN_THOUSAND; i++) {
list.pop();
}
for (let i = 0; i < TEN_THOUSAND; i++) list.pop();
})
.add(`${TEN_THOUSAND.toLocaleString()} addBefore`, () => {
const singlyList = new SinglyLinkedList<number>();

View file

@ -5,26 +5,26 @@ import { magnitude } from '../../../utils';
import { isCompetitor } from '../../../config';
export const suite = new Benchmark.Suite();
const { LINEAR, HUNDRED_THOUSAND } = magnitude;
const { MILLION, HUNDRED_THOUSAND } = magnitude;
suite.add(`${LINEAR.toLocaleString()} push`, () => {
suite.add(`${MILLION.toLocaleString()} push`, () => {
const deque = new Deque<number>();
for (let i = 0; i < LINEAR; i++) deque.push(i);
for (let i = 0; i < MILLION; i++) deque.push(i);
});
if (isCompetitor) {
suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
const _deque = new CDeque<number>();
for (let i = 0; i < LINEAR; i++) _deque.pushBack(i);
for (let i = 0; i < MILLION; i++) _deque.pushBack(i);
});
}
suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
suite.add(`${MILLION.toLocaleString()} push & pop`, () => {
const _deque = new Deque<number>();
for (let i = 0; i < LINEAR; i++) _deque.push(i);
for (let i = 0; i < LINEAR; i++) _deque.pop();
for (let i = 0; i < MILLION; i++) _deque.push(i);
for (let i = 0; i < MILLION; i++) _deque.pop();
})
.add(`${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {

View file

@ -5,20 +5,20 @@ import { magnitude } from '../../../utils';
import { isCompetitor } from '../../../config';
const suite = new Benchmark.Suite();
const { LINEAR, HUNDRED_THOUSAND } = magnitude;
const { MILLION, HUNDRED_THOUSAND } = magnitude;
suite.add(`${LINEAR.toLocaleString()} push`, () => {
suite.add(`${MILLION.toLocaleString()} push`, () => {
const queue = new Queue<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
});
if (isCompetitor) {
suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
const queue = new CQueue<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
});

View file

@ -5,42 +5,42 @@ import { magnitude } from '../../../utils';
import { isCompetitor } from '../../../config';
const suite = new Benchmark.Suite();
const { LINEAR } = magnitude;
const { MILLION } = magnitude;
suite.add(`${LINEAR.toLocaleString()} push`, () => {
suite.add(`${MILLION.toLocaleString()} push`, () => {
const stack = new Stack<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
stack.push(i);
}
});
if (isCompetitor) {
suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
const queue = new CStack<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
});
}
suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
suite.add(`${MILLION.toLocaleString()} push & pop`, () => {
const queue = new Stack<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.pop();
}
});
if (isCompetitor) {
suite.add(`CPT ${LINEAR.toLocaleString()} push & pop`, () => {
suite.add(`CPT ${MILLION.toLocaleString()} push & pop`, () => {
const queue = new CStack<number>();
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.push(i);
}
for (let i = 0; i < LINEAR; i++) {
for (let i = 0; i < MILLION; i++) {
queue.pop();
}
});

View file

@ -639,4 +639,19 @@ describe('BinaryTree iterative methods test', () => {
const values = binaryTree.values();
expect([...values]).toEqual(['b', 'a', 'c']);
});
test('should iterative method return undefined when the node is null', () => {
const tree = new BinaryTree()
tree.addMany([-10, -10, -10, 9, 9, 20, null, null, 15, 7, 8, null, 2, null, 6, null, null, 8, 8, 8]);
const bfsResult = tree.bfs(undefined, undefined, undefined, true);
expect(bfsResult).toEqual([
-10, 9,
20, undefined,
undefined, 15,
7, 8,
undefined, 2,
undefined, 6,
undefined, undefined
]);
})
});

View file

@ -635,7 +635,7 @@ describe('DirectedGraph iterative Methods', () => {
expect(concatenated).toBe(vertexMap.join(''));
});
test('Removing an edge of a DirectedGraph should not delete additional edges', () => {
test('Removing an edge of a DirectedGraph should delete additional edges', () => {
const dg = new DirectedGraph();
dg.addVertex('hello')
dg.addVertex('hi')
@ -649,9 +649,22 @@ describe('DirectedGraph iterative Methods', () => {
dg.deleteEdge('hello', 'hi')
expect(dg.getEdge('hello', 'hi')).toBe(undefined)
expect(dg.getEdge('hello', 'hey')).toBeInstanceOf(DirectedEdge)
expect(dg.incomingEdgesOf("Hi")).toEqual([])
});
test('Removing a vertex from a UndirectedGraph should remove its edges', () => {
test('Removing a vertex of a DirectedGraph should delete additional edges', () => {
const graph = new DirectedGraph();
graph.addVertex("Hello");
graph.addVertex("Hi");
graph.addEdge("Hello", "Hi");
graph.deleteVertex("Hello");
expect(graph.incomingEdgesOf("Hi")).toEqual([]);
})
test('Removing a vertex from a DirectedGraph should remove its edges', () => {
const dg = new DirectedGraph();
dg.addVertex('hello')
dg.addVertex('world')

View file

@ -190,6 +190,19 @@ describe('UndirectedGraph', () => {
expect(dg.getEdge('hello', 'hey')).toBeInstanceOf(UndirectedEdge)
});
test('Removing a vertex of a DirectedGraph should delete additional edges', () => {
const graph = new UndirectedGraph();
graph.addVertex("Hello");
graph.addVertex("Hi");
graph.addEdge("Hello", "Hi");
graph.deleteVertex("Hello");
expect(graph.edgesOf("Hi")).toEqual([]);
})
test('Removing a vertex from a UndirectedGraph should remove its edges', () => {
const dg = new UndirectedGraph();
dg.addVertex('hello')

View file

@ -554,4 +554,25 @@ describe('LinkedHashMap setMany, keys, values', () => {
test('values', () => {
expect([...hm.values()]).toEqual([2, 3, 4, 5, 6])
});
test('entries', () => {
expect([...hm.entries()]).toEqual([[2, 2], [3, 3], [4, 4], [5, 5], [6, 6]])
});
test('every', () => {
expect(hm.every(value => value > 4)).toBe(false)
});
test('some', () => {
expect(hm.some(value => value > 6)).toBe(false)
});
test('hasValue', () => {
expect(hm.hasValue(3)).toBe(true)
expect(hm.hasValue(7)).toBe(false)
});
test('print', () => {
hm.print();
});
});

View file

@ -424,4 +424,31 @@ describe('iterable methods', () => {
expect([...dl.map(element => element * 2)]).toEqual([2, 4, 6]);
expect(dl.reduce((accumulator, element) => accumulator + element, 0)).toEqual(6);
});
test('values', () => {
const dl = new DoublyLinkedList<number>()
dl.push(1);
dl.push(2);
dl.push(3);
dl.delete(2);
dl.unshift(0);
dl.shift();
dl.pop();
dl.unshift(3);
expect([...dl.values()]).toEqual([3, 1])
})
test('some', () => {
const dl = new DoublyLinkedList<number>()
dl.push(1);
dl.push(2);
dl.push(3);
dl.delete(2);
dl.unshift(0);
dl.shift();
dl.pop();
dl.unshift(3);
expect(dl.some(value => value > 1)).toBe(true)
expect(dl.some(value => value > 100)).toBe(false)
})
});

View file

@ -2,16 +2,16 @@ import { AnyFunction } from '../types';
import { isDebugTest } from '../config';
const isDebug = isDebugTest;
const orderReducedBy = 1; // reduction of bigO's order compared to the baseline bigO
// const orderReducedBy = 1; // reduction of bigO's order compared to the baseline bigO
export const magnitude = {
CONSTANT: Math.pow(10, 9),
LOG_N: Math.pow(10, 8 - orderReducedBy),
LINEAR: Math.pow(10, 7 - orderReducedBy),
N_LOG_N: Math.pow(10, 4 - orderReducedBy),
SQUARED: Math.pow(10, 3 - orderReducedBy),
CUBED: Math.pow(10, 2 - orderReducedBy),
FACTORIAL: 20 - orderReducedBy,
// CONSTANT: Math.pow(10, 9),
// LOG_N: Math.pow(10, 8 - orderReducedBy),
// LINEAR: Math.pow(10, 7 - orderReducedBy),
// N_LOG_N: Math.pow(10, 4 - orderReducedBy),
// SQUARED: Math.pow(10, 3 - orderReducedBy),
// CUBED: Math.pow(10, 2 - orderReducedBy),
// FACTORIAL: 20 - orderReducedBy,
THOUSAND: 1000,
TEN_THOUSAND: 10000,
HUNDRED_THOUSAND: 100000,
@ -21,12 +21,12 @@ export const magnitude = {
};
export const bigO = {
CONSTANT: magnitude.CONSTANT / 100000,
LOG_N: Math.log2(magnitude.LOG_N) / 1000,
LINEAR: magnitude.LINEAR / 1000,
N_LOG_N: (magnitude.N_LOG_N * Math.log2(magnitude.LOG_N)) / 1000,
SQUARED: Math.pow(magnitude.SQUARED, 2) / 1000,
CUBED: Math.pow(magnitude.SQUARED, 3) / 1000,
// CONSTANT: magnitude.CONSTANT / 100000,
// LOG_N: Math.log2(magnitude.LOG_N) / 1000,
// LINEAR: magnitude.LINEAR / 1000,
// N_LOG_N: (magnitude.N_LOG_N * Math.log2(magnitude.LOG_N)) / 1000,
// SQUARED: Math.pow(magnitude.SQUARED, 2) / 1000,
// CUBED: Math.pow(magnitude.SQUARED, 3) / 1000,
FACTORIAL: 10000
};