Compare commits

..

No commits in common. "9f197d48b06232074de3d1d3f63be27fbaac480a" and "ea0d4272dfba400ad6b65e9efdc1ac633b31fa2e" have entirely different histories.

27 changed files with 1622 additions and 645 deletions

View file

@ -823,43 +823,43 @@ Version 11.7.9
[//]: # (No deletion!!! Start of Replace Section)
<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</td><td>7.49</td><td>133.43</td><td>2.10e-4</td></tr><tr><td>100,000 add & poll</td><td>44.08</td><td>22.69</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>8.07</td><td>123.93</td><td>0.00</td></tr><tr><td>100,000 add & poll</td><td>45.32</td><td>22.07</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'>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>79.16</td><td>12.63</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>84.35</td><td>11.85</td><td>0.00</td></tr><tr><td>100,000 get</td><td>111.74</td><td>8.95</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>26.61</td><td>37.58</td><td>0.00</td></tr><tr><td>100,000 add & delete orderly</td><td>160.05</td><td>6.25</td><td>0.02</td></tr><tr><td>100,000 add & delete randomly</td><td>234.55</td><td>4.26</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 add</td><td>77.03</td><td>12.98</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>83.26</td><td>12.01</td><td>0.00</td></tr><tr><td>100,000 get</td><td>120.25</td><td>8.32</td><td>0.01</td></tr><tr><td>100,000 iterator</td><td>26.32</td><td>37.99</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>161.68</td><td>6.19</td><td>0.01</td></tr><tr><td>100,000 add & delete randomly</td><td>268.63</td><td>3.72</td><td>0.03</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>40.70</td><td>24.57</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>5.15</td><td>194.25</td><td>6.57e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2143.37</td><td>0.47</td><td>0.15</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>45.55</td><td>21.96</td><td>0.02</td></tr><tr><td>100,000 push & shift</td><td>5.54</td><td>180.36</td><td>0.00</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2467.71</td><td>0.41</td><td>0.20</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>24.74</td><td>40.43</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>31.38</td><td>31.87</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>32.12</td><td>31.13</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.39</td><td>295.24</td><td>3.62e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2348.52</td><td>0.43</td><td>0.21</td></tr><tr><td>100,000 unshift & shift</td><td>3.28</td><td>304.60</td><td>2.76e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4062.43</td><td>0.25</td><td>0.12</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>25.36</td><td>39.43</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>32.13</td><td>31.12</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>33.42</td><td>29.92</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.59</td><td>278.92</td><td>3.21e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2427.03</td><td>0.41</td><td>0.21</td></tr><tr><td>100,000 unshift & shift</td><td>3.56</td><td>280.81</td><td>3.81e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4464.18</td><td>0.22</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'>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>106.65</td><td>9.38</td><td>0.02</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>201.70</td><td>4.96</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>163.55</td><td>6.11</td><td>0.01</td></tr><tr><td>1,000,000 set & get</td><td>115.56</td><td>8.65</td><td>0.02</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>264.29</td><td>3.78</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>171.96</td><td>5.82</td><td>0.01</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>326.27</td><td>3.06</td><td>0.05</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>322.17</td><td>3.10</td><td>0.06</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>241.34</td><td>4.14</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>313.05</td><td>3.19</td><td>0.07</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>224.48</td><td>4.45</td><td>0.05</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>203.46</td><td>4.91</td><td>0.05</td></tr><tr><td>1,000,000 set & get</td><td>281.79</td><td>3.55</td><td>0.07</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>289.57</td><td>3.45</td><td>0.03</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>195.73</td><td>5.11</td><td>0.02</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>348.39</td><td>2.87</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>315.23</td><td>3.17</td><td>0.04</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>335.83</td><td>2.98</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'>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.21</td><td>22.62</td><td>0.00</td></tr><tr><td>100,000 getWords</td><td>85.22</td><td>11.73</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 push</td><td>48.09</td><td>20.79</td><td>0.00</td></tr><tr><td>100,000 getWords</td><td>93.17</td><td>10.73</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'>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>100,000 add</td><td>269.92</td><td>3.70</td><td>0.01</td></tr><tr><td>100,000 add randomly</td><td>317.13</td><td>3.15</td><td>0.00</td></tr><tr><td>100,000 get</td><td>127.74</td><td>7.83</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>29.99</td><td>33.34</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>431.27</td><td>2.32</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>580.91</td><td>1.72</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>278.91</td><td>3.59</td><td>0.01</td></tr><tr><td>100,000 add randomly</td><td>350.65</td><td>2.85</td><td>0.02</td></tr><tr><td>100,000 get</td><td>154.97</td><td>6.45</td><td>0.03</td></tr><tr><td>100,000 iterator</td><td>34.78</td><td>28.75</td><td>0.02</td></tr><tr><td>100,000 add & delete orderly</td><td>445.45</td><td>2.24</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>616.61</td><td>1.62</td><td>0.03</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-overall</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 RBTree add randomly</td><td>6.73</td><td>148.59</td><td>1.04e-4</td></tr><tr><td>10,000 RBTree get randomly</td><td>9.48</td><td>105.50</td><td>1.07e-4</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>18.40</td><td>54.33</td><td>2.70e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>23.57</td><td>42.43</td><td>1.73e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>9.70</td><td>103.06</td><td>7.92e-5</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>44.43</td><td>22.51</td><td>3.33e-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 RBTree add randomly</td><td>7.07</td><td>141.45</td><td>7.19e-4</td></tr><tr><td>10,000 RBTree get randomly</td><td>9.33</td><td>107.21</td><td>1.48e-4</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>18.60</td><td>53.77</td><td>3.93e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>30.39</td><td>32.91</td><td>0.01</td></tr><tr><td>10,000 AVLTree get randomly</td><td>10.70</td><td>93.49</td><td>0.00</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>46.04</td><td>21.72</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'>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>9723.27</td><td>2.46e-6</td></tr><tr><td>1,000 addEdge</td><td>6.26</td><td>159.78</td><td>7.28e-4</td></tr><tr><td>1,000 getVertex</td><td>0.04</td><td>2.54e+4</td><td>4.17e-7</td></tr><tr><td>1,000 getEdge</td><td>22.64</td><td>44.16</td><td>0.00</td></tr><tr><td>tarjan</td><td>200.14</td><td>5.00</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>175.91</td><td>5.68</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 addVertex</td><td>0.13</td><td>7502.53</td><td>6.69e-5</td></tr><tr><td>1,000 addEdge</td><td>7.93</td><td>126.13</td><td>0.00</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.19e+4</td><td>1.17e-5</td></tr><tr><td>1,000 getEdge</td><td>18.65</td><td>53.61</td><td>0.00</td></tr><tr><td>tarjan</td><td>158.86</td><td>6.30</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>141.91</td><td>7.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'>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>196.17</td><td>5.10</td><td>0.03</td></tr><tr><td>1,000,000 unshift</td><td>203.73</td><td>4.91</td><td>0.04</td></tr><tr><td>1,000,000 unshift & shift</td><td>186.32</td><td>5.37</td><td>0.04</td></tr><tr><td>1,000,000 addBefore</td><td>298.35</td><td>3.35</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,000 push</td><td>211.26</td><td>4.73</td><td>0.03</td></tr><tr><td>1,000,000 unshift</td><td>216.90</td><td>4.61</td><td>0.07</td></tr><tr><td>1,000,000 unshift & shift</td><td>208.45</td><td>4.80</td><td>0.05</td></tr><tr><td>1,000,000 addBefore</td><td>316.55</td><td>3.16</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>210.62</td><td>4.75</td><td>0.09</td></tr><tr><td>10,000 push & pop</td><td>226.02</td><td>4.42</td><td>0.02</td></tr><tr><td>10,000 addBefore</td><td>249.35</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>222.81</td><td>4.49</td><td>0.08</td></tr><tr><td>10,000 push & pop</td><td>235.91</td><td>4.24</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>250.57</td><td>3.99</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'>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</td><td>27.23</td><td>36.72</td><td>7.64e-4</td></tr><tr><td>100,000 add & poll</td><td>76.66</td><td>13.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>100,000 add</td><td>27.64</td><td>36.17</td><td>8.81e-4</td></tr><tr><td>100,000 add & poll</td><td>75.52</td><td>13.24</td><td>9.91e-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>37.10</td><td>26.96</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>44.72</td><td>22.36</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>40.47</td><td>24.71</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>47.47</td><td>21.07</td><td>0.01</td></tr></table></div>
</div>
[//]: # (No deletion!!! End of Replace Section)

View file

@ -29,6 +29,10 @@ export abstract class IterableElementBase<E, R, C> {
return this._toElementFn;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -42,6 +46,10 @@ export abstract class IterableElementBase<E, R, C> {
yield* this._getIterator(...args);
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -54,6 +62,10 @@ export abstract class IterableElementBase<E, R, C> {
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -78,6 +90,15 @@ export abstract class IterableElementBase<E, R, C> {
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -102,6 +123,11 @@ export abstract class IterableElementBase<E, R, C> {
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -122,6 +148,11 @@ export abstract class IterableElementBase<E, R, C> {
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -164,6 +195,10 @@ export abstract class IterableElementBase<E, R, C> {
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -186,6 +221,11 @@ export abstract class IterableElementBase<E, R, C> {
return accumulator;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)

View file

@ -9,6 +9,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
// }
// }
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
abstract get size(): number;
// protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
@ -34,6 +39,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
yield* this._getIterator(...args);
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -47,6 +56,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -59,6 +72,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -71,6 +88,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -95,6 +116,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -120,6 +145,15 @@ export abstract class IterableEntryBase<K = any, V = any> {
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -141,6 +175,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -167,6 +206,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
return;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -185,6 +229,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -201,6 +250,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -245,6 +299,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
return accumulator;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)

View file

@ -105,6 +105,11 @@ export class AVLTreeMultiMap<
return this._count;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -198,6 +203,11 @@ export class AVLTreeMultiMap<
return;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -227,6 +237,11 @@ export class AVLTreeMultiMap<
return true;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -307,6 +322,11 @@ export class AVLTreeMultiMap<
return deletedResult;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -319,6 +339,11 @@ export class AVLTreeMultiMap<
this._count = 0;
}
/**
* Time Complexity: O(n log n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n log n)
* Space Complexity: O(log n)
@ -369,6 +394,11 @@ export class AVLTreeMultiMap<
}
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -382,6 +412,11 @@ export class AVLTreeMultiMap<
return cloned;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -423,6 +458,11 @@ export class AVLTreeMultiMap<
return undefined;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -135,6 +135,12 @@ export class AVLTree<
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -155,6 +161,11 @@ export class AVLTree<
return inserted;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -181,6 +192,11 @@ export class AVLTree<
return deletedResults;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -222,6 +238,11 @@ export class AVLTree<
return undefined;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -242,6 +263,11 @@ export class AVLTree<
else return node.right.height - node.left.height;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -259,6 +285,11 @@ export class AVLTree<
else node.height = 1 + Math.max(node.right.height, node.left.height);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -292,6 +323,11 @@ export class AVLTree<
if (B) this._updateHeight(B);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -343,6 +379,11 @@ export class AVLTree<
C && this._updateHeight(C);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -381,6 +422,11 @@ export class AVLTree<
B && this._updateHeight(B);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -431,6 +477,12 @@ export class AVLTree<
C && this._updateHeight(C);
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -480,6 +532,11 @@ export class AVLTree<
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -22,7 +22,6 @@ import type {
NodeDisplayLayout,
OptBTNOrNull
} from '../../types';
import { DFSOperation, DFSStackItem } from '../../types';
import { IBinaryTree } from '../../interfaces';
import { trampoline } from '../../utils';
import { Queue } from '../queue';
@ -267,6 +266,11 @@ export class BinaryTree<
return;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
@ -283,7 +287,7 @@ export class BinaryTree<
*/
ensureNode(
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType: IterationType = this.iterationType
iterationType: IterationType = 'ITERATIVE'
): OptBTNOrNull<NODE> {
if (keyOrNodeOrEntryOrRawElement === null) return null;
if (keyOrNodeOrEntryOrRawElement === undefined) return;
@ -334,7 +338,7 @@ export class BinaryTree<
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value.
*/
isRealNodeOrNull(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
isNodeOrNull(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
return this.isRealNode(node) || node === null;
}
@ -344,26 +348,10 @@ export class BinaryTree<
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value.
*/
isNIL(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): boolean {
isNIL(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>) {
return node === this.NIL;
}
/**
* The function `isLeaf` determines whether a given node is a leaf node in a binary tree structure.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter in the `isLeaf` function
* can be either a regular node (`R`) or a `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns The `isLeaf` function is checking if the provided node is a leaf node in a binary tree.
* If the node is `undefined`, it returns `false`. If the node is `null`, it returns `true`.
* Otherwise, it checks if both the left and right children of the node are not real nodes, and
* returns `true` if they are not, indicating that the node is a
*/
isLeaf(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): boolean {
node = this.ensureNode(node);
if (node === undefined) return false;
if (node === null) return true;
return !this.isRealNode(node.left) && !this.isRealNode(node.right);
}
/**
* The function checks if the input is an array with two elements, indicating it is a binary tree
* node entry.
@ -405,6 +393,11 @@ export class BinaryTree<
return false;
}
/**
* Time Complexity O(n)
* Space Complexity O(1)
*/
/**
* Time Complexity O(n)
* Space Complexity O(1)
@ -474,6 +467,12 @@ export class BinaryTree<
return false; // If the insertion position cannot be found, return undefined
}
/**
* Time Complexity: O(k * n)
* Space Complexity: O(1)
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
*/
/**
* Time Complexity: O(k * n)
* Space Complexity: O(1)
@ -516,6 +515,12 @@ export class BinaryTree<
return inserted;
}
/**
* Time Complexity: O(k * n)
* Space Complexity: O(1)
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
*/
/**
* Time Complexity: O(k * n)
* Space Complexity: O(1)
@ -544,6 +549,11 @@ export class BinaryTree<
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeleteResult<NODE>[];
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -631,6 +641,11 @@ export class BinaryTree<
iterationType?: IterationType
): NODE[];
/**
* Time Complexity: O(n)
* Space Complexity: O(k + log n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(k + log n)
@ -719,6 +734,11 @@ export class BinaryTree<
iterationType?: IterationType
): OptBTNOrNull<NODE>;
/**
* Time Complexity: O(n)
* Space Complexity: O(log n).
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n).
@ -746,6 +766,11 @@ export class BinaryTree<
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
@ -758,7 +783,7 @@ export class BinaryTree<
* It has a default value of `'ITERATIVE'`.
* @returns a value of type NODE, null, or undefined.
*/
getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBTNOrNull<NODE> {
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBTNOrNull<NODE> {
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
}
@ -783,6 +808,11 @@ export class BinaryTree<
iterationType?: IterationType
): V | undefined;
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
@ -834,6 +864,11 @@ export class BinaryTree<
iterationType?: IterationType
): boolean;
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
@ -865,6 +900,11 @@ export class BinaryTree<
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -876,6 +916,11 @@ export class BinaryTree<
this._size = 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -887,6 +932,11 @@ export class BinaryTree<
return this.size === 0;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
@ -903,6 +953,11 @@ export class BinaryTree<
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -961,6 +1016,11 @@ export class BinaryTree<
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -992,6 +1052,11 @@ export class BinaryTree<
return depth;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -1038,6 +1103,11 @@ export class BinaryTree<
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
@ -1100,6 +1170,11 @@ export class BinaryTree<
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
@ -1128,6 +1203,11 @@ export class BinaryTree<
return isReverse ? result.reverse() : result;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -1168,6 +1248,11 @@ export class BinaryTree<
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -1209,6 +1294,11 @@ export class BinaryTree<
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -1232,6 +1322,11 @@ export class BinaryTree<
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -1261,7 +1356,8 @@ export class BinaryTree<
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[];
dfs<C extends BTNCallback<NODE | null>>(
@ -1269,9 +1365,14 @@ export class BinaryTree<
pattern?: DFSOrderPattern,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: boolean
includeNull?: true
): ReturnType<C>[];
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -1299,12 +1400,95 @@ export class BinaryTree<
callback: C = this._DEFAULT_CALLBACK as C,
pattern: DFSOrderPattern = 'IN',
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType,
iterationType: IterationType = 'ITERATIVE',
includeNull = false
): ReturnType<C>[] {
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return [];
return this._dfs(callback, pattern, beginRoot, iterationType, includeNull);
const ans: ReturnType<C>[] = [];
if (iterationType === 'RECURSIVE') {
const dfs = (node: OptBTNOrNull<NODE>) => {
switch (pattern) {
case 'IN':
if (includeNull) {
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
this.isNodeOrNull(node) && ans.push(callback(node));
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
} else {
if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
this.isRealNode(node) && ans.push(callback(node));
if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
}
break;
case 'PRE':
if (includeNull) {
this.isNodeOrNull(node) && ans.push(callback(node));
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
} else {
this.isRealNode(node) && ans.push(callback(node));
if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
}
break;
case 'POST':
if (includeNull) {
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
this.isNodeOrNull(node) && ans.push(callback(node));
} else {
if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
this.isRealNode(node) && ans.push(callback(node));
}
break;
}
};
dfs(beginRoot);
} else {
// 0: visit, 1: print
const stack: { opt: 0 | 1; node: OptBTNOrNull<NODE> }[] = [{ opt: 0, node: beginRoot }];
while (stack.length > 0) {
const cur = stack.pop();
if (cur === undefined || this.isNIL(cur.node)) 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':
cur.node && stack.push({ opt: 0, node: cur.node.right });
stack.push({ opt: 1, node: cur.node });
cur.node && stack.push({ opt: 0, node: cur.node.left });
break;
case 'PRE':
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 });
cur.node && stack.push({ opt: 0, node: cur.node.right });
cur.node && stack.push({ opt: 0, node: cur.node.left });
break;
default:
cur.node && stack.push({ opt: 0, node: cur.node.right });
stack.push({ opt: 1, node: cur.node });
cur.node && stack.push({ opt: 0, node: cur.node.left });
break;
}
}
}
}
return ans;
}
bfs<C extends BTNCallback<NODE>>(
@ -1321,6 +1505,11 @@ export class BinaryTree<
includeNull?: true
): ReturnType<C>[];
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -1364,8 +1553,8 @@ export class BinaryTree<
ans.push(callback(current));
if (includeNull) {
if (current && this.isRealNodeOrNull(current.left)) queue.push(current.left);
if (current && this.isRealNodeOrNull(current.right)) queue.push(current.right);
if (current && this.isNodeOrNull(current.left)) queue.push(current.left);
if (current && this.isNodeOrNull(current.right)) queue.push(current.right);
} else {
if (this.isRealNode(current.left)) queue.push(current.left);
if (this.isRealNode(current.right)) queue.push(current.right);
@ -1385,8 +1574,8 @@ export class BinaryTree<
ans.push(callback(current));
if (includeNull) {
if (current && this.isRealNodeOrNull(current.left)) queue.push(current.left);
if (current && this.isRealNodeOrNull(current.right)) queue.push(current.right);
if (current && this.isNodeOrNull(current.left)) queue.push(current.left);
if (current && this.isNodeOrNull(current.right)) queue.push(current.right);
} else {
if (this.isRealNode(current.left)) queue.push(current.left);
if (this.isRealNode(current.right)) queue.push(current.right);
@ -1397,63 +1586,6 @@ export class BinaryTree<
return ans;
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*
* The `leaves` function in TypeScript iterates through a binary tree to find and return the leaf
* nodes based on a specified callback and iteration type.
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
* in the binary tree. It is a generic type `C` that extends `BTNCallback<NODE | null>`, where `NODE`
* represents a node in the binary tree. The default value for `callback` is
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `leaves`
* method is used to specify the starting point for finding and processing the leaves of a binary
* tree. It represents the root node of the binary tree or a specific key, node, or entry within the
* tree from which the search for leaves should begin
* @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
* specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
* can have two possible values:
* @returns The `leaves` method returns an array of values that are the result of applying the
* provided callback function to the leaf nodes in the binary tree structure.
*/
leaves<C extends BTNCallback<NODE | null>>(
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): ReturnType<C>[] {
beginRoot = this.ensureNode(beginRoot);
const leaves: ReturnType<BTNCallback<NODE>>[] = [];
if (!this.isRealNode(beginRoot)) {
return [];
}
if (iterationType === 'RECURSIVE') {
const dfs = (cur: NODE) => {
if (this.isLeaf(cur)) {
leaves.push(callback(cur));
}
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
this.isRealNode(cur.left) && dfs(cur.left);
this.isRealNode(cur.right) && dfs(cur.right);
};
dfs(beginRoot);
} else {
const queue = new Queue([beginRoot]);
while (queue.size > 0) {
const cur = queue.shift();
if (this.isRealNode(cur)) {
if (this.isLeaf(cur)) {
leaves.push(callback(cur));
}
this.isRealNode(cur.left) && queue.push(cur.left);
this.isRealNode(cur.right) && queue.push(cur.right);
}
}
}
return leaves;
}
listLevels<C extends BTNCallback<NODE>>(
callback?: C,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
@ -1468,6 +1600,11 @@ export class BinaryTree<
includeNull?: true
): ReturnType<C>[][];
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -1504,8 +1641,8 @@ export class BinaryTree<
if (!levelsNodes[level]) levelsNodes[level] = [];
levelsNodes[level].push(callback(node));
if (includeNull) {
if (node && this.isRealNodeOrNull(node.left)) _recursive(node.left, level + 1);
if (node && this.isRealNodeOrNull(node.right)) _recursive(node.right, level + 1);
if (node && this.isNodeOrNull(node.left)) _recursive(node.left, level + 1);
if (node && this.isNodeOrNull(node.right)) _recursive(node.right, level + 1);
} else {
if (node && node.left) _recursive(node.left, level + 1);
if (node && node.right) _recursive(node.right, level + 1);
@ -1524,8 +1661,8 @@ export class BinaryTree<
levelsNodes[level].push(callback(node));
if (includeNull) {
if (node && this.isRealNodeOrNull(node.right)) stack.push([node.right, level + 1]);
if (node && this.isRealNodeOrNull(node.left)) stack.push([node.left, level + 1]);
if (node && this.isNodeOrNull(node.right)) stack.push([node.right, level + 1]);
if (node && this.isNodeOrNull(node.left)) 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]);
@ -1536,6 +1673,11 @@ export class BinaryTree<
return levelsNodes;
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -1641,6 +1783,11 @@ export class BinaryTree<
return ans;
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -1662,6 +1809,11 @@ export class BinaryTree<
return cloned;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -1688,6 +1840,11 @@ export class BinaryTree<
return newTree;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -1722,6 +1879,11 @@ export class BinaryTree<
// // }
//
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -1753,166 +1915,18 @@ export class BinaryTree<
const display = (root: OptBTNOrNull<NODE>): void => {
const [lines, , ,] = this._displayAux(root, opts);
let paragraph = '';
for (const line of lines) {
paragraph += line + '\n';
console.log(line);
}
console.log(paragraph);
};
display(beginRoot);
}
protected _dfs<C extends BTNCallback<NODE>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): ReturnType<C>[];
protected _dfs<C extends BTNCallback<NODE | null>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: boolean
): ReturnType<C>[];
/**
* Time complexity: O(n)
* Space complexity: O(n)
*
* The function `_dfs` performs a depth-first search traversal on a binary tree structure based on
* the specified order pattern and callback function.
* @param {C} callback - The `callback` parameter is a function that will be called on each node
* visited during the depth-first search. It is of type `C`, which extends
* `BTNCallback<OptBTNOrNull<NODE>>`. The default value is set to `this._DEFAULT_CALLBACK` if not
* provided.
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in a binary tree.
* It can have one of the following values:
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `_dfs`
* method is used to specify the starting point for the depth-first search traversal in a binary
* tree. It can be provided as either the root node of the tree or a key, node, or entry that exists
* in the tree. If no specific `
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal. It
* can have two possible values:
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
* that determines whether null nodes should be included in the depth-first search traversal. If
* `includeNull` is set to `true`, the traversal will consider null nodes as valid nodes to visit and
* process. If set to `
* @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
* and returns a boolean value. It is used to determine whether the left child of a node should be
* visited during the depth-first search traversal. By default, it checks if the node is truthy (not
* null or undefined
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as
* input and returns a boolean value. It is used to determine whether the right child of a node
* should be visited during the depth-first search traversal. The default implementation checks if
* the node is truthy before visiting the right child.
* @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
* argument and returns a boolean value. It is used to determine whether a given node should be
* visited during the depth-first search traversal based on certain conditions. The default
* implementation checks if the node is a real node or null based
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as
* input and returns a boolean value indicating whether the node should be processed during the
* depth-first search traversal. The default implementation of this function simply returns `true`,
* meaning that by default all nodes will be processed. However, you can
* @returns The `_dfs` method returns an array of the return type of the callback function provided
* as input.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
protected _dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
callback: C = this._DEFAULT_CALLBACK as C,
pattern: DFSOrderPattern = 'IN',
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType,
includeNull = false,
shouldVisitLeft: (node: OptBTNOrNull<NODE>) => boolean = node => !!node,
shouldVisitRight: (node: OptBTNOrNull<NODE>) => boolean = node => !!node,
shouldVisitRoot: (node: OptBTNOrNull<NODE>) => boolean = node => {
if (includeNull) return this.isRealNodeOrNull(node);
return this.isRealNode(node);
},
shouldProcessRoot: (node: OptBTNOrNull<NODE>) => boolean = node => true
): ReturnType<C>[] {
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return [];
const ans: ReturnType<C>[] = [];
if (iterationType === 'RECURSIVE') {
const dfs = (node: OptBTNOrNull<NODE>) => {
if (!shouldVisitRoot(node)) return;
const visitLeft = () => {
if (shouldVisitLeft(node)) dfs(node?.left);
};
const visitRight = () => {
if (shouldVisitRight(node)) dfs(node?.right);
};
switch (pattern) {
case 'IN':
visitLeft();
if (shouldProcessRoot(node)) ans.push(callback(node));
visitRight();
break;
case 'PRE':
if (shouldProcessRoot(node)) ans.push(callback(node));
visitLeft();
visitRight();
break;
case 'POST':
visitLeft();
visitRight();
if (shouldProcessRoot(node)) ans.push(callback(node));
break;
}
};
dfs(beginRoot);
} else {
const stack: DFSStackItem<NODE>[] = [{ opt: DFSOperation.VISIT, node: beginRoot }];
const pushLeft = (cur: DFSStackItem<NODE>) => {
if (shouldVisitLeft(cur.node)) stack.push({ opt: DFSOperation.VISIT, node: cur.node?.left });
};
const pushRight = (cur: DFSStackItem<NODE>) => {
if (shouldVisitRight(cur.node)) stack.push({ opt: DFSOperation.VISIT, node: cur.node?.right });
};
const pushRoot = (cur: DFSStackItem<NODE>) => {
if (shouldVisitRoot(cur.node)) stack.push({ opt: DFSOperation.PROCESS, node: cur.node });
};
while (stack.length > 0) {
const cur = stack.pop();
if (cur === undefined) continue;
if (!shouldVisitRoot(cur.node)) continue;
if (cur.opt === DFSOperation.PROCESS) {
if (shouldProcessRoot(cur.node)) ans.push(callback(cur.node));
} else {
switch (pattern) {
case 'IN':
pushRight(cur);
pushRoot(cur);
pushLeft(cur);
break;
case 'PRE':
pushRight(cur);
pushLeft(cur);
pushRoot(cur);
break;
case 'POST':
pushRoot(cur);
pushRight(cur);
pushLeft(cur);
break;
}
}
}
}
return ans;
}
/**
* Time Complexity: O(1)
@ -1955,6 +1969,11 @@ export class BinaryTree<
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -2042,6 +2061,11 @@ export class BinaryTree<
protected _DEFAULT_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -2079,6 +2103,11 @@ export class BinaryTree<
return undefined;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -2109,6 +2138,11 @@ export class BinaryTree<
return newNode;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -2125,6 +2159,11 @@ export class BinaryTree<
this._root = v;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -195,7 +195,7 @@ export class BST<
*/
override ensureNode(
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType: IterationType = this.iterationType
iterationType: IterationType = 'ITERATIVE'
): OptBSTN<NODE> {
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
}
@ -259,6 +259,11 @@ export class BST<
return false;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(k log n)
* Space Complexity: O(k + log n)
@ -464,6 +469,11 @@ export class BST<
return ans;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -494,6 +504,11 @@ export class BST<
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
}
/**
* Time Complexity: O(k log n)
* Space Complexity: O(k + log n)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -507,10 +522,15 @@ export class BST<
* It has a default value of `'ITERATIVE'`.
* @returns The method is returning a NODE object or undefined.
*/
override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBSTN<NODE> {
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBSTN<NODE> {
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(k + log n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -535,11 +555,16 @@ export class BST<
callback: C = this._DEFAULT_CALLBACK as C,
pattern: DFSOrderPattern = 'IN',
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
iterationType: IterationType = 'ITERATIVE'
): ReturnType<C>[] {
return super.dfs(callback, pattern, beginRoot, iterationType);
return super.dfs(callback, pattern, beginRoot, iterationType, false);
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -565,6 +590,11 @@ export class BST<
return super.bfs(callback, beginRoot, iterationType, false);
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -591,6 +621,11 @@ export class BST<
return super.listLevels(callback, beginRoot, iterationType, false);
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -652,6 +687,11 @@ export class BST<
}
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -701,6 +741,11 @@ export class BST<
}
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
@ -770,6 +815,11 @@ export class BST<
protected _comparator: Comparator<K> = this._DEFAULT_COMPARATOR;
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*/
/**
* The function returns the value of the _comparator property.
* @returns The `_comparator` property is being returned.

View file

@ -126,6 +126,11 @@ export class RedBlackTree<
}) as TREE;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -178,6 +183,11 @@ export class RedBlackTree<
// return ;
// }
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -190,6 +200,11 @@ export class RedBlackTree<
this._root = this.NIL;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -223,6 +238,11 @@ export class RedBlackTree<
} else return insertStatus === 'UPDATED';
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -299,6 +319,11 @@ export class RedBlackTree<
return results;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -314,6 +339,11 @@ export class RedBlackTree<
this._root = v;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -332,6 +362,11 @@ export class RedBlackTree<
return super._replaceNode(oldNode, newNode);
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -379,6 +414,11 @@ export class RedBlackTree<
return 'CREATED';
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -402,6 +442,11 @@ export class RedBlackTree<
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -468,6 +513,11 @@ export class RedBlackTree<
if (this.isRealNode(this._root)) this._root.color = 'BLACK';
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -551,6 +601,11 @@ export class RedBlackTree<
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -586,6 +641,11 @@ export class RedBlackTree<
x.parent = y;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -100,6 +100,11 @@ export class TreeMultiMap<
return this._count;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -197,6 +202,11 @@ export class TreeMultiMap<
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -226,6 +236,11 @@ export class TreeMultiMap<
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -337,6 +352,11 @@ export class TreeMultiMap<
return results;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -349,6 +369,11 @@ export class TreeMultiMap<
this._count = 0;
}
/**
* Time Complexity: O(n log n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(n log n)
* Space Complexity: O(log n)
@ -400,6 +425,11 @@ export class TreeMultiMap<
}
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
@ -413,6 +443,11 @@ export class TreeMultiMap<
return cloned;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -455,6 +490,11 @@ export class TreeMultiMap<
return undefined;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -119,6 +119,11 @@ export abstract class AbstractGraph<
abstract getEndsOfEdge(edge: EO): [VO, VO] | undefined;
/**
* Time Complexity: O(1) - Constant time for Map lookup.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
*/
/**
* Time Complexity: O(1) - Constant time for Map lookup.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
@ -133,6 +138,11 @@ export abstract class AbstractGraph<
return this._vertexMap.get(vertexKey) || undefined;
}
/**
* Time Complexity: O(1) - Constant time for Map lookup.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
*/
/**
* Time Complexity: O(1) - Constant time for Map lookup.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
@ -176,6 +186,11 @@ export abstract class AbstractGraph<
abstract deleteVertex(vertexOrKey: VO | VertexKey): boolean;
/**
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
*/
/**
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
@ -194,6 +209,11 @@ export abstract class AbstractGraph<
return removed.length > 0;
}
/**
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
*/
/**
* Time Complexity: O(1) - Depends on the implementation in the concrete class.
* Space Complexity: O(1) - Depends on the implementation in the concrete class.
@ -235,6 +255,11 @@ export abstract class AbstractGraph<
}
}
/**
* Time Complexity: O(1) - Constant time for Map and Edge operations.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
*/
/**
* Time Complexity: O(1) - Constant time for Map and Edge operations.
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
@ -259,6 +284,11 @@ export abstract class AbstractGraph<
}
}
/**
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
*/
/**
* Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
* Space Complexity: O(P) - Linear space, where P is the number of paths found.
@ -301,6 +331,11 @@ export abstract class AbstractGraph<
return paths;
}
/**
* Time Complexity: O(L), where L is the length of the path.
* Space Complexity: O(1) - Constant space.
*/
/**
* Time Complexity: O(L), where L is the length of the path.
* Space Complexity: O(1) - Constant space.
@ -317,6 +352,11 @@ export abstract class AbstractGraph<
return sum;
}
/**
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
*/
/**
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
@ -379,6 +419,11 @@ export abstract class AbstractGraph<
}
}
/**
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
*/
/**
* Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
@ -450,6 +495,15 @@ export abstract class AbstractGraph<
}
}
/**
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
*/
/**
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
*/
/**
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
@ -579,6 +633,20 @@ export abstract class AbstractGraph<
return { distMap, preMap, seen, paths, minDist, minPath };
}
/**
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
*
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edgeMap.
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
*
*/
/**
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
*/
/**
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
@ -706,6 +774,12 @@ export abstract class AbstractGraph<
return { distMap, preMap, seen, paths, minDist, minPath };
}
/**
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
* one to rest pairs
*/
/**
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
@ -828,6 +902,14 @@ export abstract class AbstractGraph<
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
*/
/**
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
* Not support graph with negative weight cycle
* all pairs
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
*/
/**
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
@ -927,6 +1009,11 @@ export abstract class AbstractGraph<
return [...uniqueCycles].map(cycleString => cycleString[1]);
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -955,6 +1042,11 @@ export abstract class AbstractGraph<
return filtered;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)

View file

@ -81,6 +81,11 @@ export class DirectedGraph<
this._inEdgeMap = v;
}
/**
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
*/
/**
* The function creates a new vertex with an optional value and returns it.
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
@ -94,6 +99,11 @@ export class DirectedGraph<
return new DirectedVertex(key, value) as VO;
}
/**
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
*/
/**
* The function creates a directed edge between two vertexMap with an optional weight and value.
* @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
@ -108,6 +118,11 @@ export class DirectedGraph<
return new DirectedEdge(src, dest, weight ?? 1, value) as EO;
}
/**
* Time Complexity: O(|V|) where |V| is the number of vertexMap
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(|V|) where |V| is the number of vertexMap
* Space Complexity: O(1)
@ -137,6 +152,11 @@ export class DirectedGraph<
return edgeMap[0] || undefined;
}
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
@ -166,6 +186,11 @@ export class DirectedGraph<
return removed;
}
/**
* Time Complexity: O(E) where E is the number of edgeMap
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(E) where E is the number of edgeMap
* Space Complexity: O(1)
@ -209,6 +234,11 @@ export class DirectedGraph<
return removed;
}
/**
* Time Complexity: O(1) - Constant time for Map operations.
* 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.
@ -242,6 +272,11 @@ export class DirectedGraph<
return this._vertexMap.delete(vertexKey);
}
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
@ -267,6 +302,11 @@ export class DirectedGraph<
return removed;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -284,6 +324,11 @@ export class DirectedGraph<
return [];
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -301,6 +346,11 @@ export class DirectedGraph<
return [];
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -313,6 +363,11 @@ export class DirectedGraph<
return this.outDegreeOf(vertexOrKey) + this.inDegreeOf(vertexOrKey);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -325,6 +380,11 @@ export class DirectedGraph<
return this.incomingEdgesOf(vertexOrKey).length;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -337,6 +397,11 @@ export class DirectedGraph<
return this.outgoingEdgesOf(vertexOrKey).length;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -349,6 +414,11 @@ export class DirectedGraph<
return [...this.outgoingEdgesOf(vertexOrKey), ...this.incomingEdgesOf(vertexOrKey)];
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -361,6 +431,11 @@ export class DirectedGraph<
return this._getVertex(e.src);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -373,6 +448,11 @@ export class DirectedGraph<
return this._getVertex(e.dest);
}
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
@ -397,6 +477,11 @@ export class DirectedGraph<
return destinations;
}
/**
* Time Complexity: O(|V| + |E|) where |V| is the number of vertexMap and |E| is the number of edgeMap
* Space Complexity: O(|V|)
*/
/**
* Time Complexity: O(|V| + |E|) where |V| is the number of vertexMap and |E| is the number of edgeMap
* Space Complexity: O(|V|)
@ -446,6 +531,11 @@ export class DirectedGraph<
return sorted.reverse();
}
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(|E|)
*/
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(|E|)
@ -461,6 +551,11 @@ export class DirectedGraph<
return edgeMap;
}
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(|E|) where |E| is the number of edgeMap
* Space Complexity: O(1)
@ -486,6 +581,11 @@ export class DirectedGraph<
return neighbors;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -518,6 +618,11 @@ export class DirectedGraph<
return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -543,6 +648,13 @@ export class DirectedGraph<
return cloned;
}
/**
* Time Complexity: O(V + E)
* Space Complexity: O(V)
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
*/
/**
* Time Complexity: O(V + E)
* Space Complexity: O(V)
@ -605,6 +717,11 @@ export class DirectedGraph<
return { dfnMap, lowMap, SCCs };
}
/**
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
*/
/**
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
@ -636,6 +753,11 @@ export class DirectedGraph<
return this.tarjan().SCCs;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -96,6 +96,11 @@ export class UndirectedGraph<
return new UndirectedEdge(v1, v2, weight ?? 1, value) as EO;
}
/**
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
* Space Complexity: O(1)
@ -122,6 +127,11 @@ export class UndirectedGraph<
return edgeMap ? edgeMap[0] || undefined : undefined;
}
/**
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
* Space Complexity: O(1)
@ -152,6 +162,11 @@ export class UndirectedGraph<
return removed;
}
/**
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
* Space Complexity: O(1)
@ -186,6 +201,11 @@ export class UndirectedGraph<
}
}
/**
* Time Complexity: O(1) - Constant time for Map operations.
* 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.
@ -224,6 +244,11 @@ export class UndirectedGraph<
return this._vertexMap.delete(vertexKey);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -243,6 +268,11 @@ export class UndirectedGraph<
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -261,6 +291,11 @@ export class UndirectedGraph<
}
}
/**
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
* Space Complexity: O(|E|)
*/
/**
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
* Space Complexity: O(|E|)
@ -278,6 +313,11 @@ export class UndirectedGraph<
return [...edgeSet];
}
/**
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
* Space Complexity: O(|E|)
*/
/**
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
* Space Complexity: O(|E|)
@ -302,6 +342,11 @@ export class UndirectedGraph<
return neighbors;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -333,6 +378,11 @@ export class UndirectedGraph<
return this.vertexMap.size === 0 && this.edgeMap.size === 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -360,6 +410,11 @@ export class UndirectedGraph<
return cloned;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(V + E)
* Space Complexity: O(V)
@ -429,6 +484,13 @@ export class UndirectedGraph<
};
}
/**
* Time Complexity: O(V + E)
* Space Complexity: O(V)
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
*/
/**
* The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
* @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.

View file

@ -234,7 +234,9 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
*/
/**
* The clone function creates a new HashMap with the same key-value pairs as
* this one. The clone function is useful for creating a copy of an existing
* HashMap, and then modifying that copy without affecting the original.
@ -245,6 +247,11 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
return new HashMap<K, V, R>(this, { hashFn: this._hashFn, toEntryFn: this._toEntryFn });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -472,6 +479,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return this._size;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -485,6 +497,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return <[K, V]>[this.head.key, this.head.value];
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -521,6 +538,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -620,6 +642,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -645,6 +672,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -665,6 +697,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return node.value;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -707,6 +744,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -725,6 +767,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return this._deleteNode(node);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -747,6 +794,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return Array.isArray(rawElement) && rawElement.length === 2;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -759,6 +811,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -777,6 +834,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return cloned;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -804,6 +866,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return filteredMap;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -832,6 +899,12 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return mappedMap;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
* where n is the number of entries in the LinkedHashMap.
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -847,6 +920,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -560,6 +560,11 @@ export class FibonacciHeap<E> {
return this.min ? this.min.element : undefined;
}
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Space Complexity: O(1)
@ -607,6 +612,11 @@ export class FibonacciHeap<E> {
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -618,6 +628,11 @@ export class FibonacciHeap<E> {
return this.pop();
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -652,6 +667,11 @@ export class FibonacciHeap<E> {
return z.element;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -712,6 +732,11 @@ export class FibonacciHeap<E> {
return 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -730,6 +755,11 @@ export class FibonacciHeap<E> {
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -744,6 +774,11 @@ export class FibonacciHeap<E> {
if (node.right) node.right.left = node.left;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -762,6 +797,11 @@ export class FibonacciHeap<E> {
y.parent = x;
}
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)

View file

@ -133,6 +133,12 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return this._size;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
* where n is the number of elements in the linked list.
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -144,6 +150,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return this.head?.value;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -155,6 +166,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return this.tail?.value;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -171,7 +187,9 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
*/
/**
* The push function adds a new element to the end of a doubly linked list.
* @param {E} element - The "element" parameter represents the value that you want to add to the
* doubly linked list.
@ -194,7 +212,9 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
*/
/**
* The `pop()` function removes and returns the value of the last element in a linked list.
* @returns The method is returning the value of the removed node.
*/
@ -215,7 +235,9 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
*/
/**
* The `shift()` function removes and returns the value of the first element in a doubly linked list.
* @returns The value of the removed node.
*/
@ -236,7 +258,9 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
*/
/**
* The unshift function adds a new element to the beginning of a doubly linked list.
* @param {E} element - The "element" parameter represents the value of the element that you want to
* add to the beginning of the doubly linked list.
@ -256,6 +280,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -275,6 +304,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return current!.value;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -295,6 +329,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return current;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -318,6 +357,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return undefined;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -352,6 +396,12 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return true;
}
/**
* Time Complexity: O(1) or O(n)
* Space Complexity: O(1)
* where n is the number of elements in the linked list.
*/
/**
* Time Complexity: O(1) or O(n)
* Space Complexity: O(1)
@ -392,6 +442,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return false;
}
/**
* Time Complexity: O(1) or O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) or O(n)
* Space Complexity: O(1)
@ -461,6 +516,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return true;
}
/**
* Time Complexity: O(1) or O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1) or O(n)
* Space Complexity: O(1)
@ -497,6 +557,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return false;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -508,6 +573,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return this.size === 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -520,6 +590,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
this._size = 0;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -543,6 +618,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return -1;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -565,6 +645,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return undefined;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -582,6 +667,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return this;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -599,6 +689,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return array;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -616,6 +711,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return array;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -629,6 +729,11 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return new DoublyLinkedList<E, R>(this);
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -661,7 +766,9 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
*/
/**
* The `map` function takes a callback function and returns a new DoublyLinkedList with the results
* of applying the callback to each element in the original list.
* @param callback - The callback parameter is a function that will be called for each element in the

View file

@ -121,6 +121,13 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return this._size;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
* Linear space, as it creates a new node for each element in the array.
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -138,6 +145,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return singlyLinkedList;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -160,6 +172,12 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
* Linear time in the worst case, as it may need to traverse the list to find the last element.
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -189,6 +207,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return value;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -204,6 +227,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return removedNode.value;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -226,6 +254,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -245,6 +278,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return current!.value;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -263,6 +301,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return current;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -291,6 +334,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -335,6 +383,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -384,6 +437,13 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
this._size = 0;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
* Linear space, as it creates an array with the same length as the list.
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -401,6 +461,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return array;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -426,6 +491,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return this;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -450,6 +520,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return -1;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -473,6 +548,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return undefined;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -513,6 +593,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -547,6 +632,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return false;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -569,6 +659,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return count;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -582,6 +677,11 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return new SinglyLinkedList<E, R>(this, { toElementFn: this.toElementFn });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -614,7 +714,9 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
*/
/**
* The `map` function takes a callback function and returns a new SinglyLinkedList with the results
* of applying the callback to each element in the original list.
* @param callback - The `callback` parameter is a function that will be called for each element in

View file

@ -80,6 +80,11 @@ export class SkipList<K, V> {
return this._probability;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -92,6 +97,11 @@ export class SkipList<K, V> {
return firstNode ? firstNode.value : undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -109,6 +119,11 @@ export class SkipList<K, V> {
return current.value;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -140,6 +155,11 @@ export class SkipList<K, V> {
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -169,7 +189,9 @@ export class SkipList<K, V> {
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
*/
/**
* The function checks if a key exists in a data structure.
* @param {K} key - The parameter "key" is of type K, which represents the type of the key being
* checked.
@ -179,6 +201,11 @@ export class SkipList<K, V> {
return this.get(key) !== undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -217,6 +244,11 @@ export class SkipList<K, V> {
return false;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -236,6 +268,11 @@ export class SkipList<K, V> {
return nextNode ? nextNode.value : undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -260,6 +297,12 @@ export class SkipList<K, V> {
return lastLess ? lastLess.value : undefined;
}
/**
* Time Complexity: O(maxLevel)
* Space Complexity: O(1)
* where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
*/
/**
* Time Complexity: O(maxLevel)
* Space Complexity: O(1)

View file

@ -176,6 +176,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return this._buckets[this._bucketLast][this._lastInBucket];
}
/**
* Time Complexity - Amortized O(1) (possible reallocation)
* Space Complexity - O(n) (due to potential resizing).
*/
/**
* Time Complexity - Amortized O(1) (possible reallocation),
* Space Complexity - O(n) (due to potential resizing).
@ -204,6 +209,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return true;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -230,6 +240,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return element;
}
/**
* Time Complexity: Amortized O(1)
* Space Complexity: O(n)
*/
/**
* Time Complexity: Amortized O(1)
* Space Complexity: O(n)
@ -259,6 +274,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return true;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -286,6 +306,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return element;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -297,6 +322,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return this.size === 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -334,6 +364,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -350,6 +385,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return this._buckets[bucketIndex][indexInBucket]!;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -367,6 +407,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -401,6 +446,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return true;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -434,6 +484,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1) or O(n)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1) or O(n)
@ -470,6 +525,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1) or O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1) or O(n)
@ -499,6 +559,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -526,6 +591,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -547,6 +617,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return this;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -572,6 +647,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return this;
}
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n log n)
* Space Complexity: O(n)
@ -594,6 +674,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return this;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -624,6 +709,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
this._buckets = newBuckets;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -644,6 +734,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return -1;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -655,6 +750,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return [...this];
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -668,6 +768,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return new Deque<E, R>(this, { bucketSize: this.bucketSize, toElementFn: this.toElementFn });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -699,7 +804,9 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
*/
/**
* The `map` function takes a callback function and applies it to each element in the deque,
* returning a new deque with the results.
* @param callback - The callback parameter is a function that will be called for each element in the
@ -728,6 +835,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
return newDeque;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
@ -741,6 +853,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -772,6 +889,11 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
this._bucketCount = newBuckets.length;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)

View file

@ -61,6 +61,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return this.elements.length - this.offset;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -73,6 +78,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return this.size > 0 ? this.elements[this.offset] : undefined;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -104,6 +114,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
this._autoCompactRatio = v;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -118,6 +133,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return new Queue(elements);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -131,6 +151,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return true;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -169,6 +194,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return spliced.length === 1;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -179,6 +209,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return this.elements[index + this._offset];
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -190,6 +225,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return this.size === 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(n)
@ -201,6 +241,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return this.elements.slice(this.offset);
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -223,6 +268,12 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return true;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* where n is the number of elements in the queue. It creates a shallow copy of the internal array. the space required is proportional to the number of elements in the queue.
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -234,6 +285,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -266,6 +322,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
map<EM, RM>(
callback: ElementCallback<E, R, EM, Queue<E, R>>,
toElementFn?: (rawElement: RM) => EM,
@ -280,6 +337,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
return newDeque;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -300,6 +362,11 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
*/
export class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)

View file

@ -74,6 +74,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
return this.elements.length === 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -87,6 +92,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
return this.elements[this.elements.length - 1];
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -100,6 +110,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
return true;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -134,6 +149,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
return spliced.length === 1;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -145,6 +165,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
return this.elements.slice();
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -155,6 +180,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
this._elements = [];
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -166,6 +196,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
return new Stack<E, R>(this, { toElementFn: this.toElementFn });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -197,7 +232,9 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
*/
/**
* The `map` function takes a callback function and applies it to each element in the stack,
* returning a new stack with the results.
* @param callback - The callback parameter is a function that will be called for each element in the
@ -225,6 +262,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
return newStack;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)

View file

@ -147,6 +147,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return this._root;
}
/**
* Time Complexity: O(l), where l is the length of the word being added.
* Space Complexity: O(l) - Each character in the word adds a TrieNode.
*/
/**
* Time Complexity: O(l), where l is the length of the word being added.
* Space Complexity: O(l) - Each character in the word adds a TrieNode.
@ -175,6 +180,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return isNewWord;
}
/**
* Time Complexity: O(l), where l is the length of the input word.
* Space Complexity: O(1) - Constant space.
*/
/**
* Time Complexity: O(l), where l is the length of the input word.
* Space Complexity: O(1) - Constant space.
@ -194,6 +204,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return cur.isEnd;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -205,6 +220,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return this.size === 0;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -216,6 +236,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
this._root = new TrieNode('');
}
/**
* Time Complexity: O(l), where l is the length of the word being deleted.
* Space Complexity: O(n) - Due to the recursive DFS approach.
*/
/**
* Time Complexity: O(l), where l is the length of the word being deleted.
* Space Complexity: O(n) - Due to the recursive DFS approach.
@ -260,6 +285,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return isDeleted;
}
/**
* Time Complexity: O(n), where n is the total number of nodes in the trie.
* Space Complexity: O(1) - Constant space.
*/
/**
* Time Complexity: O(n), where n is the total number of nodes in the trie.
* Space Complexity: O(1) - Constant space.
@ -285,6 +315,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return maxDepth;
}
/**
* Time Complexity: O(l), where l is the length of the input prefix.
* Space Complexity: O(1) - Constant space.
*/
/**
* Time Complexity: O(l), where l is the length of the input prefix.
* Space Complexity: O(1) - Constant space.
@ -304,6 +339,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return !cur.isEnd;
}
/**
* Time Complexity: O(l), where l is the length of the input prefix.
* Space Complexity: O(1) - Constant space.
*/
/**
* Time Complexity: O(l), where l is the length of the input prefix.
* Space Complexity: O(1) - Constant space.
@ -323,6 +363,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return true;
}
/**
* Time Complexity: O(n), where n is the total number of nodes in the trie.
* Space Complexity: O(l), where l is the length of the input prefix.
*/
/**
* Time Complexity: O(n), where n is the total number of nodes in the trie.
* Space Complexity: O(l), where l is the length of the input prefix.
@ -345,6 +390,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return commonPre === input;
}
/**
* Time Complexity: O(n), where n is the total number of nodes in the trie.
* Space Complexity: O(l), where l is the length of the longest common prefix.
*/
/**
* Time Complexity: O(n), where n is the total number of nodes in the trie.
* Space Complexity: O(l), where l is the length of the longest common prefix.
@ -364,6 +414,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return commonPre;
}
/**
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
* Space Complexity: O(w * l) - The space required for the output array.
*/
/**
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
* Space Complexity: O(w * l) - The space required for the output array.
@ -413,6 +468,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return words;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -425,6 +485,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return new Trie<R>(this, { caseSensitive: this.caseSensitive, toElementFn: this.toElementFn });
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -451,6 +516,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return results;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -483,6 +553,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
return newTrie;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -503,6 +578,11 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
yield* _dfs(this.root, '');
}
/**
* Time Complexity: O(l), where l is the length of the input string.
* Space Complexity: O(1) - Constant space.
*/
/**
* Time Complexity: O(l), where l is the length of the input string.
* Space Complexity: O(1) - Constant space.

View file

@ -28,11 +28,4 @@ export type BTNPureKeyOrNodeOrEntry<K, V, NODE> = [K, OptValue<V>] | BTNPureKeyO
export type BinaryTreeDeleteResult<NODE> = { deleted: OptBTNOrNull<NODE>; needBalanced: OptBTNOrNull<NODE> };
export type BTNCallback<NODE, D = any> = (node: NODE) => D;
export enum DFSOperation {
VISIT = 0,
PROCESS = 1,
}
export type DFSStackItem<NODE> = { opt: DFSOperation; node: OptBTNOrNull<NODE> }
export type BTNCallback<NODE, D = any> = (node: NODE) => D;

View file

@ -411,9 +411,4 @@ describe('AVLTree iterative methods test', () => {
const values = avl.values();
expect([...values]).toEqual(['a', 'b', 'c']);
});
it('should leaves', () => {
const leaves = avl.leaves();
expect(leaves).toEqual([1, 3]);
});
});

View file

@ -292,178 +292,29 @@ describe('BinaryTree', () => {
expect(tree.getNodes(tree.getNodeByKey(2), undefined, false, tree.root)).toEqual([tree.getNodeByKey(2)]);
});
it('should tree traverse', () => {
tree.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
expect(tree.dfs(node => node.key, 'PRE', undefined, 'ITERATIVE')).toEqual([4, 2, 1, 5, 6, 3, 7]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', undefined, 'ITERATIVE', false)).toEqual([
4, 2, 1, 5, 6, 3, 7
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', undefined, 'ITERATIVE', true)).toEqual([
4,
2,
null,
1,
5,
null,
6,
3,
7,
null
]);
expect(tree.dfs(node => node.key, 'PRE', undefined, 'RECURSIVE')).toEqual([4, 2, 1, 5, 6, 3, 7]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', undefined, 'RECURSIVE', false)).toEqual([
4, 2, 1, 5, 6, 3, 7
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', undefined, 'RECURSIVE', true)).toEqual([
4,
2,
null,
1,
5,
null,
6,
3,
7,
null
]);
expect(tree.dfs(node => node.key, 'IN', undefined, 'ITERATIVE')).toEqual([2, 5, 1, 4, 7, 3, 6]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', undefined, 'ITERATIVE', false)).toEqual([
2, 5, 1, 4, 7, 3, 6
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', undefined, 'ITERATIVE', true)).toEqual([
null,
2,
5,
1,
null,
4,
7,
3,
6,
null
]);
expect(tree.dfs(node => node.key, 'IN', undefined, 'RECURSIVE')).toEqual([2, 5, 1, 4, 7, 3, 6]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', undefined, 'RECURSIVE', false)).toEqual([
2, 5, 1, 4, 7, 3, 6
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', undefined, 'RECURSIVE', true)).toEqual([
null,
2,
5,
1,
null,
4,
7,
3,
6,
null
]);
expect(tree.dfs(node => node.key, 'POST', undefined, 'ITERATIVE')).toEqual([5, 1, 2, 7, 3, 6, 4]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', undefined, 'ITERATIVE', false)).toEqual([
5, 1, 2, 7, 3, 6, 4
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', undefined, 'ITERATIVE', true)).toEqual([
null,
5,
null,
1,
2,
7,
3,
null,
6,
4
]);
expect(tree.dfs(node => node.key, 'POST', undefined, 'RECURSIVE')).toEqual([5, 1, 2, 7, 3, 6, 4]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', undefined, 'RECURSIVE', false)).toEqual([
5, 1, 2, 7, 3, 6, 4
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', undefined, 'RECURSIVE', true)).toEqual([
null,
5,
null,
1,
2,
7,
3,
null,
6,
4
]);
});
it('should sub tree traverse', () => {
tree.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
expect(tree.dfs(node => node.key, 'PRE', tree.getNode(6), 'ITERATIVE')).toEqual([6, 3, 7]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', tree.getNode(6), 'ITERATIVE', false)).toEqual([
6, 3, 7
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', tree.getNode(6), 'ITERATIVE', true)).toEqual([
6,
3,
7,
null
]);
expect(tree.dfs(node => node.key, 'PRE', tree.getNode(6), 'ITERATIVE', false)).toEqual([6, 3, 7]);
expect(tree.dfs(node => node.key, 'PRE', tree.getNode(6), 'RECURSIVE')).toEqual([6, 3, 7]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', tree.getNode(6), 'RECURSIVE', false)).toEqual([
6, 3, 7
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'PRE', tree.getNode(6), 'RECURSIVE', true)).toEqual([
expect(tree.dfs(node => (node ? node.key : null), 'PRE', tree.getNode(6), 'ITERATIVE', true)).toEqual([
6,
3,
7,
null
]);
expect(tree.dfs(node => node.key, 'IN', tree.getNode(6), 'ITERATIVE')).toEqual([7, 3, 6]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', tree.getNode(6), 'ITERATIVE', false)).toEqual([
7, 3, 6
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', tree.getNode(6), 'ITERATIVE', true)).toEqual([
7,
3,
expect(tree.dfs(node => (node ? node.key : node), 'PRE', tree.getNode(6), 'ITERATIVE', true)).toEqual([
6,
3,
7,
null
]);
expect(tree.dfs(node => node.key, 'IN', tree.getNode(6), 'RECURSIVE')).toEqual([7, 3, 6]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', tree.getNode(6), 'RECURSIVE', false)).toEqual([
7, 3, 6
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'IN', tree.getNode(6), 'RECURSIVE', true)).toEqual([
7,
3,
expect(tree.dfs(node => (node ? node.key : null), 'PRE', tree.getNode(6), 'RECURSIVE', true)).toEqual([
6,
3,
7,
null
]);
expect(tree.dfs(node => node.key, 'POST', tree.getNode(6), 'ITERATIVE')).toEqual([7, 3, 6]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', tree.getNode(6), 'ITERATIVE', false)).toEqual([
7, 3, 6
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', tree.getNode(6), 'ITERATIVE', true)).toEqual([
7,
3,
null,
6
]);
expect(tree.dfs(node => node.key, 'POST', tree.getNode(6), 'RECURSIVE')).toEqual([7, 3, 6]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', tree.getNode(6), 'RECURSIVE', false)).toEqual([
7, 3, 6
]);
expect(tree.dfs(node => (node !== null ? node.key : null), 'POST', tree.getNode(6), 'RECURSIVE', true)).toEqual([
7,
3,
null,
6
]);
});
it('should clear the tree', () => {
@ -987,11 +838,6 @@ describe('BinaryTree iterative methods test', () => {
expect([...values]).toEqual(['b', 'a', 'c']);
});
it('should leaves', () => {
const leaves = binaryTree.leaves();
expect(leaves).toEqual([2, 3]);
});
it('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]);

View file

@ -956,15 +956,9 @@ describe('BST iterative methods test', () => {
let bst: BST<number, string>;
beforeEach(() => {
bst = new BST();
bst.addMany(
[
[1, 'a'],
[2, 'b'],
[3, 'c']
],
[],
false
);
bst.add([1, 'a']);
bst.add([2, 'b']);
bst.add([3, 'c']);
});
it('The node obtained by get Node should match the node type', () => {
@ -1038,69 +1032,4 @@ describe('BST iterative methods test', () => {
const values = bst.values();
expect([...values]).toEqual(['a', 'b', 'c']);
});
it('should leaves', () => {
const leaves = bst.leaves();
expect(leaves).toEqual([3]);
});
it('should collapsed, unbalanced, balanced bst leaves', () => {
const collapsedToLinkedList = new BST();
collapsedToLinkedList.addMany(
[
[1, 'a'],
[2, 'b'],
[3, 'c'],
[4, 'd'],
[5, 'e'],
[6, 'f'],
[7, 'g'],
[8, 'h'],
[9, 'i']
],
[],
false
);
expect(collapsedToLinkedList.leaves()).toEqual([9]);
const unbalanced = new BST();
unbalanced.addMany(
[
[2, 'b'],
[1, 'a'],
[3, 'c'],
[4, 'd'],
[5, 'e'],
[6, 'f'],
[7, 'g'],
[8, 'h'],
[9, 'i']
],
[],
false
);
expect(unbalanced.leaves()).toEqual([1, 9]);
const balanced = new BST();
balanced.addMany(
[
[2, 'b'],
[1, 'a'],
[3, 'c'],
[4, 'd'],
[5, 'e'],
[6, 'f'],
[7, 'g'],
[8, 'h'],
[9, 'i']
],
[],
true
);
expect(balanced.leaves()).toEqual([1, 6, 4, 9]);
expect(balanced.leaves(node => node?.value)).toEqual(['a', 'f', 'd', 'i']);
});
});

View file

@ -677,126 +677,3 @@ describe('RedBlackTree 2', () => {
});
});
});
describe('RedBlackTree - _deleteFixup', () => {
let tree: RedBlackTree<number, number>;
beforeEach(() => {
tree = new RedBlackTree();
});
it('should handle deleting a red leaf node', () => {
tree.add(10, 10);
tree.add(5, 5); // Red leaf
tree.add(20, 20);
expect(tree.delete(5)).toHaveLength(1); // Delete red leaf
expect(tree.root?.left).toBe(tree.NIL); // Left child should be NIL
});
it('should handle deleting a black leaf node', () => {
tree.add(10, 10);
tree.add(5, 5); // Black node
tree.add(20, 20);
tree.add(1, 1); // Black leaf node
expect(tree.delete(1)).toHaveLength(1); // Delete black leaf
expect(tree.root?.left?.left).toBe(tree.NIL);
});
it('should handle deleting black node with red sibling', () => {
tree.add(10, 10);
tree.add(5, 5); // Black node
tree.add(20, 20); // Red sibling
tree.add(25, 25); // Force the sibling to be red
expect(tree.delete(5)).toHaveLength(1); // Delete black node
expect(tree.root?.right?.color).toBe('BLACK'); // Ensure sibling color is black after fixup
});
it('should handle deleting black node with black sibling', () => {
tree.add(10, 10);
tree.add(5, 5); // Black node
tree.add(20, 20); // Black sibling
expect(tree.delete(5)).toHaveLength(1); // Delete black node
expect(tree.root?.left).toBe(tree.NIL);
});
it('should handle deleting the root node', () => {
tree.add(10, 10); // Root node
tree.add(5, 5);
tree.add(20, 20);
expect(tree.delete(10)).toHaveLength(1); // Delete root node
expect(tree.root?.key).toBe(20); // New root should be 20
});
it('should handle complex case with multiple rotations', () => {
tree.add(10, 10);
tree.add(5, 5);
tree.add(15, 15);
tree.add(12, 12);
tree.add(18, 18);
tree.add(16, 16);
// Delete a node that will cause rotations and color changes
expect(tree.delete(5)).toHaveLength(1);
// Verify the color and structure after fixup
expect(tree.root?.color).toBe('BLACK');
expect(tree.root?.left).toBe(tree.NIL);
expect(tree.root?.right?.left?.color).toBe('BLACK');
});
it('should handle complex delete fixup scenarios', () => {
const tree = new RedBlackTree<number, number>();
// Build a tree that will require complex fixup
tree.add(20, 20);
tree.add(10, 10);
tree.add(30, 30);
tree.add(5, 5);
tree.add(15, 15);
tree.add(25, 25);
tree.add(35, 35);
tree.add(2, 2);
tree.add(8, 8);
// This deletion should trigger a complex fixup
tree.delete(2);
// tree.print(tree.root, { isShowNull: true, isShowRedBlackNIL: true, isShowUndefined: false });
expect(tree.isLeaf(2)).toBe(false);
expect(tree.isLeaf(8)).toBe(true);
expect(tree.isLeaf(15)).toBe(true);
expect(tree.isLeaf(25)).toBe(true);
expect(tree.isLeaf(35)).toBe(true);
expect(tree.isLeaf(20)).toBe(false);
expect(tree.isLeaf(30)).toBe(false);
// Verify tree structure and colors after fixup
expect(tree.root?.color).toBe('BLACK');
expect(tree.root?.key).toBe(20);
expect(tree.root?.left?.color).toBe('RED');
expect(tree.root?.left?.key).toBe(10);
expect(tree.root?.right?.color).toBe('BLACK');
expect(tree.root?.right?.key).toBe(30);
expect(tree.root?.left?.left?.color).toBe('BLACK');
expect(tree.root?.left?.left?.key).toBe(5);
expect(tree.root?.left?.right?.color).toBe('BLACK');
expect(tree.root?.left?.right?.key).toBe(15);
expect(tree.leaves(node => (node === null ? '' : `${node.key} ${node.color}`), tree.root, 'RECURSIVE')).toEqual([
'8 RED',
'15 BLACK',
'25 RED',
'35 RED'
]);
expect(tree.listLevels(node => (node === tree.NIL ? 'NIL' : `${node.key} ${node.color}`))).toEqual([
['20 BLACK'],
['10 RED', '30 BLACK'],
['5 BLACK', '15 BLACK', '25 RED', '35 RED'],
['NIL', '8 RED', 'NIL', 'NIL', 'NIL', 'NIL', 'NIL', 'NIL'],
['NIL', 'NIL']
]);
});
});

View file

@ -829,9 +829,4 @@ describe('TreeMultiMap iterative methods test', () => {
const values = treeMM.values();
expect([...values]).toEqual(['a', 'b', 'c']);
});
it('should leaves', () => {
const leaves = treeMM.leaves();
expect(leaves).toEqual([1, 3]);
});
});