Refactor: All binary tree data structures now support the toEntryFn for extracting key and value from raw data at once, avoiding users from having to traverse data multiple times, thus improving performance. Chore: Explicitly use the override keyword.

This commit is contained in:
Revone 2024-01-26 18:20:15 +08:00
parent 3c26538c2e
commit 02d71a23ba
27 changed files with 1526 additions and 1149 deletions

View file

@ -736,43 +736,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>6.34</td><td>157.78</td><td>1.90e-4</td></tr><tr><td>100,000 add & poll</td><td>31.39</td><td>31.86</td><td>8.18e-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>100,000 add</td><td>6.38</td><td>156.71</td><td>2.05e-4</td></tr><tr><td>100,000 add & poll</td><td>31.53</td><td>31.71</td><td>8.78e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>rb-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add</td><td>59.64</td><td>16.77</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>73.67</td><td>13.57</td><td>0.00</td></tr><tr><td>100,000 get</td><td>89.91</td><td>11.12</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>26.08</td><td>38.34</td><td>0.00</td></tr><tr><td>100,000 add & delete orderly</td><td>145.44</td><td>6.88</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>204.81</td><td>4.88</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>58.68</td><td>17.04</td><td>9.39e-4</td></tr><tr><td>100,000 add randomly</td><td>75.49</td><td>13.25</td><td>0.00</td></tr><tr><td>100,000 get</td><td>90.48</td><td>11.05</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>29.71</td><td>33.66</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>122.02</td><td>8.20</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>214.39</td><td>4.66</td><td>0.02</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>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.84</td><td>24.48</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>4.83</td><td>207.16</td><td>5.59e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2376.78</td><td>0.42</td><td>0.34</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>42.67</td><td>23.44</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.92</td><td>203.05</td><td>7.10e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2380.40</td><td>0.42</td><td>0.24</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.73</td><td>40.44</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>30.08</td><td>33.24</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>30.53</td><td>32.75</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.19</td><td>313.76</td><td>3.01e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2184.78</td><td>0.46</td><td>0.19</td></tr><tr><td>100,000 unshift & shift</td><td>2.94</td><td>339.72</td><td>3.66e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4196.69</td><td>0.24</td><td>0.37</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>24.97</td><td>40.04</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>30.75</td><td>32.52</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>30.60</td><td>32.67</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.22</td><td>310.21</td><td>2.47e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2153.50</td><td>0.46</td><td>0.13</td></tr><tr><td>100,000 unshift & shift</td><td>2.89</td><td>345.71</td><td>2.85e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4177.84</td><td>0.24</td><td>0.28</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>117.77</td><td>8.49</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>200.53</td><td>4.99</td><td>0.00</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>172.75</td><td>5.79</td><td>0.03</td></tr><tr><td>1,000,000 set & get</td><td>117.61</td><td>8.50</td><td>0.02</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>265.24</td><td>3.77</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>168.61</td><td>5.93</td><td>0.01</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>325.91</td><td>3.07</td><td>0.04</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>301.51</td><td>3.32</td><td>0.03</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>274.47</td><td>3.64</td><td>0.04</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>116.95</td><td>8.55</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>206.87</td><td>4.83</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>167.65</td><td>5.96</td><td>0.01</td></tr><tr><td>1,000,000 set & get</td><td>122.70</td><td>8.15</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>263.26</td><td>3.80</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>167.72</td><td>5.96</td><td>0.01</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>320.17</td><td>3.12</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>292.05</td><td>3.42</td><td>0.02</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>267.27</td><td>3.74</td><td>0.02</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>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>42.93</td><td>23.29</td><td>8.30e-4</td></tr><tr><td>100,000 getWords</td><td>82.14</td><td>12.17</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>43.93</td><td>22.76</td><td>5.85e-4</td></tr><tr><td>100,000 getWords</td><td>83.42</td><td>11.99</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'>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>231.62</td><td>4.32</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>304.23</td><td>3.29</td><td>0.00</td></tr><tr><td>100,000 get</td><td>113.93</td><td>8.78</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>30.37</td><td>32.92</td><td>0.00</td></tr><tr><td>100,000 add & delete orderly</td><td>395.02</td><td>2.53</td><td>0.02</td></tr><tr><td>100,000 add & delete randomly</td><td>542.16</td><td>1.84</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>232.60</td><td>4.30</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>306.92</td><td>3.26</td><td>0.00</td></tr><tr><td>100,000 get</td><td>113.28</td><td>8.83</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>33.20</td><td>30.12</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>399.10</td><td>2.51</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>551.57</td><td>1.81</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'>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.02</td><td>166.10</td><td>9.19e-5</td></tr><tr><td>10,000 RBTree get randomly</td><td>7.96</td><td>125.61</td><td>7.80e-5</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>16.66</td><td>60.02</td><td>1.52e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>21.64</td><td>46.21</td><td>1.93e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>8.40</td><td>119.09</td><td>1.34e-4</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>40.84</td><td>24.49</td><td>2.56e-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>6.24</td><td>160.31</td><td>1.80e-4</td></tr><tr><td>10,000 RBTree get randomly</td><td>7.93</td><td>126.09</td><td>8.91e-5</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>17.05</td><td>58.65</td><td>1.66e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>21.90</td><td>45.66</td><td>2.09e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>8.68</td><td>115.15</td><td>0.00</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>41.66</td><td>24.00</td><td>6.11e-4</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>9892.74</td><td>8.38e-7</td></tr><tr><td>1,000 addEdge</td><td>6.07</td><td>164.74</td><td>1.70e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.13e+4</td><td>2.16e-6</td></tr><tr><td>1,000 getEdge</td><td>38.66</td><td>25.87</td><td>0.02</td></tr><tr><td>tarjan</td><td>208.11</td><td>4.81</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>184.45</td><td>5.42</td><td>0.02</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.10</td><td>9614.91</td><td>7.24e-6</td></tr><tr><td>1,000 addEdge</td><td>6.68</td><td>149.81</td><td>5.43e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.18e+4</td><td>3.02e-7</td></tr><tr><td>1,000 getEdge</td><td>21.46</td><td>46.60</td><td>0.00</td></tr><tr><td>tarjan</td><td>204.99</td><td>4.88</td><td>0.03</td></tr><tr><td>topologicalSort</td><td>166.17</td><td>6.02</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'>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>218.49</td><td>4.58</td><td>0.04</td></tr><tr><td>1,000,000 unshift</td><td>213.52</td><td>4.68</td><td>0.06</td></tr><tr><td>1,000,000 unshift & shift</td><td>155.82</td><td>6.42</td><td>0.02</td></tr><tr><td>1,000,000 addBefore</td><td>325.79</td><td>3.07</td><td>0.08</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>217.48</td><td>4.60</td><td>0.04</td></tr><tr><td>1,000,000 unshift</td><td>221.64</td><td>4.51</td><td>0.02</td></tr><tr><td>1,000,000 unshift & shift</td><td>175.58</td><td>5.70</td><td>0.03</td></tr><tr><td>1,000,000 addBefore</td><td>311.89</td><td>3.21</td><td>0.04</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>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>191.94</td><td>5.21</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>214.82</td><td>4.66</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>247.53</td><td>4.04</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push & shift</td><td>211.02</td><td>4.74</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>216.29</td><td>4.62</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>248.75</td><td>4.02</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>26.63</td><td>37.56</td><td>1.30e-4</td></tr><tr><td>100,000 add & poll</td><td>76.82</td><td>13.02</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.53</td><td>36.32</td><td>0.00</td></tr><tr><td>100,000 add & poll</td><td>76.95</td><td>13.00</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'>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>38.58</td><td>25.92</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>46.06</td><td>21.71</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>39.39</td><td>25.39</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>47.72</td><td>20.96</td><td>0.01</td></tr></table></div>
</div>
[//]: # (No deletion!!! End of Replace Section)

View file

@ -16,6 +16,7 @@ import type {
IterationType,
KeyOrNodeOrEntry
} from '../../types';
import { BTNEntry } from '../../types';
import { IBinaryTree } from '../../interfaces';
import { AVLTree, AVLTreeNode } from './avl-tree';
@ -65,19 +66,36 @@ export class AVLTreeMultiMapNode<
export class AVLTreeMultiMap<
K extends Comparable,
V = any,
R = BTNEntry<K, V>,
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
K,
V,
R,
NODE,
AVLTreeMultiMapNested<K, V, R, NODE>
>
>
extends AVLTree<K, V, NODE, TREE>
implements IBinaryTree<K, V, NODE, TREE> {
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeMultiMapOptions<K>) {
extends AVLTree<K, V, R, NODE, TREE>
implements IBinaryTree<K, V, R, NODE, TREE> {
/**
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
* iterable object that can contain either keys, nodes, entries, or raw elements.
* @param [options] - The `options` parameter is an optional object that can be used to customize the
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
*/
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
options?: AVLTreeMultiMapOptions<K, V, R>
) {
super([], options);
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
}
protected _count = 0;
// TODO the _count is not accurate after nodes count modified
/**
* The function calculates the sum of the count property of all nodes in a tree using depth-first
* search.
@ -107,20 +125,29 @@ export class AVLTreeMultiMap<
}
/**
* The function creates a new BSTNode with the given key, value, and count.
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
* distinguish one node from another in the tree.
* @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
* The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
* which is a generic type that can be replaced with any specific type when using the function.
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
* associated with the key in the node. It is of type `V`, which can be any data type.
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
* calling the `createNode` method. If provided, it specifies the initial count for the node.
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
*/
override createNode(key: K, value?: V, count?: number): NODE {
return new AVLTreeMultiMapNode(key, value, count) as NODE;
}
override createTree(options?: AVLTreeMultiMapOptions<K>): TREE {
return new AVLTreeMultiMap<K, V, NODE, TREE>([], {
/**
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
* @param [options] - The `options` parameter is an optional object that contains additional
* configuration options for creating the AVLTreeMultiMap. It can have the following properties:
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
* object.
*/
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
iterationType: this.iterationType,
comparator: this.comparator,
...options
@ -128,49 +155,52 @@ export class AVLTreeMultiMap<
}
/**
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
* can be one of the following:
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
* it defaults to `undefined`.
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
* times the value should be added to the node. If not provided, it defaults to 1.
* @returns a node of type `NODE` or `undefined`.
* The function checks if the input is an instance of AVLTreeMultiMapNode.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `AVLTreeMultiMapNode` class.
*/
override keyValueOrEntryToNode(
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
value?: V,
count = 1
): NODE | undefined {
let node: NODE | undefined;
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
return;
} else if (this.isNode(keyOrNodeOrEntry)) {
node = keyOrNodeOrEntry;
} else if (this.isEntry(keyOrNodeOrEntry)) {
const [key, value] = keyOrNodeOrEntry;
if (key === undefined || key === null) {
return;
} else {
node = this.createNode(key, value, count);
}
} else if (!this.isNode(keyOrNodeOrEntry)) {
node = this.createNode(keyOrNodeOrEntry, value, count);
} else {
return;
}
return node;
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
}
/**
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
* class.
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
* a node object.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `override` function. It represents the value associated with the key in the data structure. If no
* value is provided, it will default to `undefined`.
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
* @returns either a NODE object or undefined.
*/
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
override keyValueOrEntryOrRawElementToNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
value?: V,
count = 1
): NODE | undefined {
if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null) return;
if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
if (this.toEntryFn) {
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
if (key) return this.createNode(key, entryValue ?? value, count);
}
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
const [key, value] = keyOrNodeOrEntryOrRawElement;
if (key === undefined || key === null) return;
else return this.createNode(key, value, count);
}
if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
return;
}
/**
@ -182,20 +212,21 @@ export class AVLTreeMultiMap<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function overrides the add method of a binary tree node and adds a new node to the tree.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
* entry. It represents the key, node, or entry that you want to add to the binary tree.
* The function overrides the add method of a TypeScript class to add a new node to a data structure
* and update the count.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
* can also accept a value of type `KeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
* entry, or raw element
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
* binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
* method.
* data structure. It is an optional parameter, so it can be omitted if not needed.
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
* be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
* added once. However, you can specify a different value for `count` if you want to add
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
* was not successful.
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
* be added once. However, you can specify a different value for `count` if you want to add
* @returns a boolean value.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
if (newNode === undefined) return false;
const orgNodeCount = newNode?.count || 0;
@ -215,19 +246,19 @@ export class AVLTreeMultiMap<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
* account the count of the node and balancing the tree if necessary.
* @param identifier - The identifier is the value or key that is used to identify the node that
* needs to be deleted from the binary tree. It can be of any type that is returned by the callback
* The `delete` function in a binary tree data structure deletes a node based on its identifier and
* returns the deleted node along with the parent node that needs to be balanced.
* @param identifier - The identifier parameter is the value used to identify the node that needs to
* be deleted from the binary tree. It can be of any type and is the return type of the callback
* function.
* @param {C} callback - The `callback` parameter is a function that is used to determine if a node
* should be deleted. It is optional and defaults to a default callback function. The `callback`
* function takes one parameter, which is the identifier of the node, and returns a value that is
* used to identify the node to
* @param {C} callback - The `callback` parameter is a function that is used to determine the
* equality of nodes in the binary tree. It is optional and has a default value of
* `this._DEFAULT_CALLBACK`. The `callback` function takes a single argument, which is the identifier
* of a node, and returns a value that
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
* being deleted. If set to true, the count of the node will not be considered and the node will be
* deleted regardless of its count. If set to false (default), the count of the node will be
* decremented by 1 and
* deleted regardless of its count. If set to false (default), the count of the node will be taken
* into account and the node
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
*/
override delete<C extends BTNCallback<NODE>>(
@ -300,7 +331,8 @@ export class AVLTreeMultiMap<
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The clear() function clears the contents of a data structure and sets the count to zero.
* The "clear" function overrides the parent class's "clear" function and also resets the count to
* zero.
*/
override clear() {
super.clear();
@ -315,13 +347,14 @@ export class AVLTreeMultiMap<
/**
* Time Complexity: O(n log n)
* Space Complexity: O(log n)
*
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
* tree using either a recursive or iterative approach.
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
* type of iteration to use when building the balanced binary search tree. It can have two possible
* values:
* @returns a boolean value.
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
* specifies the type of iteration to use when building the balanced binary search tree. It has a
* default value of `this.iterationType`, which means it will use the iteration type currently set in
* the object.
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
* balancing operation is successful, and `false` if there are no nodes to balance.
*/
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
const sorted = this.dfs(node => node, 'IN'),
@ -370,7 +403,7 @@ export class AVLTreeMultiMap<
* Time complexity: O(n)
* Space complexity: O(n)
*
* The `clone` function creates a deep copy of a tree object.
* The function overrides the clone method to create a deep copy of a tree object.
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
*/
override clone(): TREE {
@ -380,17 +413,26 @@ export class AVLTreeMultiMap<
}
/**
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
* which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
* node where the values from the source node will be swapped to.
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
* if either `srcNode` or `destNode` is undefined.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
* in a binary search tree.
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
* that will be swapped with the `destNode`.
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
* node where the properties will be swapped with the source node.
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
*/
protected override _swapProperties(
srcNode: BSTNKeyOrNode<K, NODE>,
destNode: BSTNKeyOrNode<K, NODE>
srcNode: R | BSTNKeyOrNode<K, NODE>,
destNode: R | BSTNKeyOrNode<K, NODE>
): NODE | undefined {
srcNode = this.ensureNode(srcNode);
destNode = this.ensureNode(destNode);
@ -417,12 +459,20 @@ export class AVLTreeMultiMap<
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function replaces an old node with a new node and updates the count property of the new node.
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
* needs to be replaced in a data structure.
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
* data structure. It is of type NODE.
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
* @returns The method is returning the result of calling the `_replaceNode` method from the
* superclass, after updating the `count` property of the `newNode` object.
* superclass, which is of type `NODE`.
*/
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
newNode.count = oldNode.count + newNode.count;

View file

@ -16,6 +16,7 @@ import type {
Comparable,
KeyOrNodeOrEntry
} from '../../types';
import { BTNEntry } from '../../types';
import { IBinaryTree } from '../../interfaces';
export class AVLTreeNode<
@ -68,33 +69,39 @@ export class AVLTreeNode<
export class AVLTree<
K extends Comparable,
V = any,
R = BTNEntry<K, V>,
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>
TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
>
extends BST<K, V, NODE, TREE>
implements IBinaryTree<K, V, NODE, TREE> {
extends BST<K, V, R, NODE, TREE>
implements IBinaryTree<K, V, R, NODE, TREE> {
/**
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
* objects. It represents a collection of nodes that will be added to the AVL tree during
* initialization.
* @param [options] - The `options` parameter is an optional object that allows you to customize the
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
* entries, or raw elements.
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
* be used to initialize the AVLTree.
* @param [options] - The `options` parameter is an optional object that can be used to customize the
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
* `nodeBuilder` (
*/
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeOptions<K>) {
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
options?: AVLTreeOptions<K, V, R>
) {
super([], options);
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
if (keysOrNodesOrEntriesOrRawElements) super.addMany(keysOrNodesOrEntriesOrRawElements);
}
/**
* The function creates a new AVL tree node with the specified key and value.
* @param {K} key - The key parameter is the key value that will be associated with
* the new node. It is used to determine the position of the node in the binary search tree.
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
* type `V`, which means it can be any value that is assignable to the `value` property of the
* node type `NODE`.
* @returns a new AVLTreeNode object with the specified key and value.
* The function creates a new AVL tree node with the given key and value.
* @param {K} key - The key parameter is of type K, which represents the key of the node being
* created.
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
* value associated with the key in the node being created.
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
* type NODE.
*/
override createNode(key: K, value?: V): NODE {
return new AVLTreeNode<K, V, NODE>(key, value) as NODE;
@ -107,8 +114,8 @@ export class AVLTree<
* being created.
* @returns a new AVLTree object.
*/
override createTree(options?: AVLTreeOptions<K>): TREE {
return new AVLTree<K, V, NODE, TREE>([], {
override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
return new AVLTree<K, V, R, NODE, TREE>([], {
iterationType: this.iterationType,
comparator: this.comparator,
...options
@ -116,12 +123,16 @@ export class AVLTree<
}
/**
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
* The function checks if the input is an instance of AVLTreeNode.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `AVLTreeNode` class.
*/
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
return keyOrNodeOrEntry instanceof AVLTreeNode;
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
}
/**
@ -134,18 +145,19 @@ export class AVLTree<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function overrides the add method of a binary tree node and balances the tree after inserting
* a new node.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
* entry.
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
* being added to the binary tree.
* @returns The method is returning either the inserted node or undefined.
* The function overrides the add method of a class and inserts a key-value pair into a data
* structure, then balances the path.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
* `RawElement`.
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
* the key or node being added to the data structure.
* @returns The method is returning a boolean value.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
if (keyOrNodeOrEntry === null) return false;
const inserted = super.add(keyOrNodeOrEntry, value);
if (inserted) this._balancePath(keyOrNodeOrEntry);
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
if (keyOrNodeOrEntryOrRawElement === null) return false;
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
return inserted;
}
@ -158,16 +170,14 @@ export class AVLTree<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function overrides the delete method of a binary tree, performs the deletion, and then
* balances the tree if necessary.
* The function overrides the delete method of a binary tree class and performs additional operations
* to balance the tree after deletion.
* @param identifier - The `identifier` parameter is the value or condition used to identify the
* node(s) to be deleted from the binary tree. It can be of any type and is the return type of the
* `callback` function.
* @param {C} callback - The `callback` parameter is a function that will be called for each node
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
* parameter of type `NODE
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
* node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
* binary tree's node type.
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
* node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
* @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
*/
override delete<C extends BTNCallback<NODE>>(
identifier: ReturnType<C>,
@ -183,18 +193,26 @@ export class AVLTree<
}
/**
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
* tree.
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
* node where the values from the source node will be swapped to.
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
* if either `srcNode` or `destNode` is undefined.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
* binary search tree.
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
* `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
*/
protected override _swapProperties(
srcNode: BSTNKeyOrNode<K, NODE>,
destNode: BSTNKeyOrNode<K, NODE>
srcNode: R | BSTNKeyOrNode<K, NODE>,
destNode: R | BSTNKeyOrNode<K, NODE>
): NODE | undefined {
const srcNodeEnsured = this.ensureNode(srcNode);
const destNodeEnsured = this.ensureNode(destNode);
@ -230,7 +248,8 @@ export class AVLTree<
* Space Complexity: O(1)
*
* The function calculates the balance factor of a node in a binary tree.
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
* binary tree data structure.
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
* height of the left subtree from the height of the right subtree.
*/
@ -275,7 +294,7 @@ export class AVLTree<
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
* @param {NODE} A - A is a node in a binary tree.
*/
protected _balanceLL(A: NODE): void {
@ -470,10 +489,10 @@ export class AVLTree<
*
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
* to restore balance in an AVL tree after inserting a node.
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
* AVL tree that needs to be balanced.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
* `KeyOrNodeOrEntry<K, V, NODE>`.
*/
protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void {
protected _balancePath(node: R | KeyOrNodeOrEntry<K, V, NODE>): void {
node = this.ensureNode(node);
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
for (let i = 0; i < path.length; i++) {
@ -514,13 +533,22 @@ export class AVLTree<
}
/**
* The function replaces an old node with a new node while preserving the height of the old node.
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the
* `newNode`.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function replaces an old node with a new node and sets the height of the new node to be the
* same as the old node.
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
* the data structure.
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
* the data structure.
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the
* `oldNode` and `newNode` as arguments.
* @returns The method is returning the result of calling the `_replaceNode` method from the
* superclass, with the `oldNode` and `newNode` as arguments.
*/
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
newNode.height = oldNode.height;

File diff suppressed because it is too large Load diff

View file

@ -11,7 +11,7 @@ import type {
BSTNodeNested,
BSTOptions,
BTNCallback,
BTNodePureExemplar,
BTNPureKeyOrNodeOrEntry,
Comparable,
Comparator,
CP,
@ -19,6 +19,7 @@ import type {
IterationType,
KeyOrNodeOrEntry
} from '../../types';
import { BTNEntry } from '../../types';
import { BinaryTree, BinaryTreeNode } from './binary-tree';
import { IBinaryTree } from '../../interfaces';
import { Queue } from '../queue';
@ -95,21 +96,24 @@ export class BSTNode<
export class BST<
K extends Comparable,
V = any,
R = BTNEntry<K, V>,
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>
TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
>
extends BinaryTree<K, V, NODE, TREE>
implements IBinaryTree<K, V, NODE, TREE> {
extends BinaryTree<K, V, R, NODE, TREE>
implements IBinaryTree<K, V, R, NODE, TREE> {
/**
* This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
* the tree with keys, nodes, or entries and optional options.
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
* contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
* keys, nodes, or entries.
* @param [options] - The `options` parameter is an optional object that can contain additional
* configuration options for the binary search tree. It can have the following properties:
* This is the constructor function for a Binary Search Tree class in TypeScript.
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
* added to the binary search tree during the construction of the object.
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
* It can include a comparator function that defines the order of the elements in the tree.
*/
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BSTOptions<K>) {
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
options?: BSTOptions<K, V, R>
) {
super([], options);
if (options) {
@ -117,7 +121,7 @@ export class BST<
if (comparator) this._comparator = comparator;
}
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
}
protected override _root?: NODE = undefined;
@ -130,20 +134,6 @@ export class BST<
return this._root;
}
protected _comparator: Comparator<K> = (a: K, b: K): CP => {
if (a > b) return 1;
if (a < b) return -1;
return 0;
};
/**
* The function returns the value of the _comparator property.
* @returns The `_comparator` property is being returned.
*/
get comparator() {
return this._comparator;
}
/**
* The function creates a new BSTNode with the given key and value and returns it.
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@ -159,13 +149,12 @@ export class BST<
/**
* The function creates a new binary search tree with the specified options.
* @param [options] - The `options` parameter is an optional object that allows you to customize the
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
* partial object of type `BSTOptions<K>`.
* @returns a new instance of the BST class, with the provided options merged with the default
* options. The returned value is casted as TREE.
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
* following properties:
* @returns a new instance of the BST class with the provided options.
*/
override createTree(options?: Partial<BSTOptions<K>>): TREE {
return new BST<K, V, NODE, TREE>([], {
override createTree(options?: Partial<BSTOptions<K, V, R>>): TREE {
return new BST<K, V, R, NODE, TREE>([], {
iterationType: this.iterationType,
comparator: this.comparator,
...options
@ -173,97 +162,69 @@ export class BST<
}
/**
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
* otherwise it returns undefined.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
* @returns a node of type NODE or undefined.
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
* element.
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
* value associated with a key in a key-value pair.
* @returns either a NODE object or undefined.
*/
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
let node: NODE | undefined;
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
return;
} else if (this.isNode(keyOrNodeOrEntry)) {
node = keyOrNodeOrEntry;
} else if (this.isEntry(keyOrNodeOrEntry)) {
const [key, value] = keyOrNodeOrEntry;
if (key === undefined || key === null) {
return;
} else {
node = this.createNode(key, value);
}
} else if (!this.isNode(keyOrNodeOrEntry)) {
node = this.createNode(keyOrNodeOrEntry, value);
} else {
return;
}
return node;
override keyValueOrEntryOrRawElementToNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
value?: V
): NODE | undefined {
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
* otherwise it returns the key itself.
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
* `undefined`.
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
* type of iteration to be performed. It has a default value of `'ITERATIVE'`.
* @returns either a node object (NODE) or undefined.
* The function ensures the existence of a node in a data structure and returns it, or undefined if
* it doesn't exist.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
* entry, or raw element that needs to be ensured in the tree.
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
* value of `'ITERATIVE'`.
* @returns The method is returning either the node that was ensured or `undefined` if the node could
* not be ensured.
*/
override ensureNode(
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
iterationType: IterationType = 'ITERATIVE'
): NODE | undefined {
if (keyOrNodeOrEntry === this.NIL) return;
if (this.isRealNode(keyOrNodeOrEntry)) {
return keyOrNodeOrEntry;
}
if (this.isEntry(keyOrNodeOrEntry)) {
const key = keyOrNodeOrEntry[0];
if (key === null || key === undefined) return;
return this.getNodeByKey(key, iterationType);
}
const key = keyOrNodeOrEntry;
if (key === null || key === undefined) return;
return this.getNodeByKey(key, iterationType);
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
}
/**
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
* The function checks if the input is an instance of the BSTNode class.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `BSTNode` class.
*/
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
return keyOrNodeOrEntry instanceof BSTNode;
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
* updating the value if the key already exists.
* @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
* @param {V} [value] - The value to be added to the binary search tree.
* @returns The method returns a boolean value.
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
* key in the binary search tree. If provided, it will be stored in the node along with the key.
* @returns a boolean value.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
if (newNode === undefined) return false;
if (this.root === undefined) {
@ -275,17 +236,8 @@ export class BST<
let current = this.root;
while (current !== undefined) {
if (this.comparator(current.key, newNode.key) === 0) {
// if (current !== newNode) {
// The key value is the same but the reference is different, update the value of the existing node
this._replaceNode(current, newNode);
return true;
// } else {
// The key value is the same and the reference is the same, replace the entire node
// this._replaceNode(current, newNode);
// return;
// }
} else if (this.comparator(current.key, newNode.key) > 0) {
if (current.left === undefined) {
current.left = newNode;
@ -307,19 +259,18 @@ export class BST<
}
/**
* Time Complexity: O(k log n)
* Space Complexity: O(k + log n)
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*/
/**
* Time Complexity: O(k log n)
* Space Complexity: O(k + log n)
*
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
* the structure if specified, and returns an array indicating whether each key or node was
* successfully inserted.
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
* data structure.
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
* an array indicating whether each key or node was successfully inserted.
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
* elements to be added to the data structure.
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
* order. If not provided, undefined will be assigned as the value for each key or node.
@ -328,14 +279,13 @@ export class BST<
* algorithm. If set to false, the elements will be added without balancing the tree. The default
* value is true.
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
* specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
* has a default value of `this.iterationType`, which means it will use the iteration type specified
* in the binary tree instance.
* @returns The function `addMany` returns an array of booleans indicating whether each key or node
* or entry was successfully inserted into the data structure.
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
* tree. It can have two possible values:
* @returns The function `addMany` returns an array of booleans indicating whether each element was
* successfully inserted into the data structure.
*/
override addMany(
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>,
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
values?: Iterable<V | undefined>,
isBalanceAdd = true,
iterationType: IterationType = this.iterationType
@ -349,7 +299,7 @@ export class BST<
}
if (!isBalanceAdd) {
for (const kve of keysOrNodesOrEntries) {
for (const kve of keysOrNodesOrEntriesOrRawElements) {
const value = valuesIterator?.next().value;
const nn = this.add(kve, value);
inserted.push(nn);
@ -357,29 +307,36 @@ export class BST<
return inserted;
}
const realBTNExemplars: BTNodePureExemplar<K, V, NODE>[] = [];
const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, NODE>): kve is BTNodePureExemplar<K, V, NODE> => {
const isRealBTNExemplar = (kve: R | KeyOrNodeOrEntry<K, V, NODE>): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
if (kve === undefined || kve === null) return false;
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
};
for (const kve of keysOrNodesOrEntries) {
for (const kve of keysOrNodesOrEntriesOrRawElements) {
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
}
let sorted: BTNodePureExemplar<K, V, NODE>[] = [];
let sorted: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
sorted = realBTNExemplars.sort((a, b) => {
let keyA: K | undefined | null, keyB: K | undefined | null;
if (this.isEntry(a)) {
keyA = a[0];
} else if (this.isRealNode(a)) keyA = a.key;
else keyA = a;
if (this.isEntry(a)) keyA = a[0];
else if (this.isRealNode(a)) keyA = a.key;
else if (this.toEntryFn) {
keyA = this.toEntryFn(a as R)[0];
} else {
keyA = a as K;
}
if (this.isEntry(b)) keyB = b[0];
else if (this.isRealNode(b)) keyB = b.key;
else keyB = b;
else if (this.toEntryFn) {
keyB = this.toEntryFn(b as R)[0];
} else {
keyB = b as K;
}
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
return this.comparator(keyA, keyB);
@ -387,7 +344,7 @@ export class BST<
return 0;
});
const _dfs = (arr: BTNodePureExemplar<K, V, NODE>[]) => {
const _dfs = (arr: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[]) => {
if (arr.length === 0) return;
const mid = Math.floor((arr.length - 1) / 2);
@ -425,38 +382,32 @@ export class BST<
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(k + log n)
* /
/**
* Time Complexity: O(log n)
* Space Complexity: O(k + log n)
*
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
* recursive or iterative approach.
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
* identifier and callback function.
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
* callback function `C`.
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
* function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
* first node that matches the identifier. If set to true, the function will return an array
* containing only the first matching node. If set to false (default), the function will continue
* searching for all nodes that match the identifier and return an array containing
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
* for the traversal. It can be either a key value or a node object. If it is undefined, the
* traversal will start from the root of the tree.
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
* performed on the binary tree. It can have two possible values:
* @returns The method returns an array of nodes (`NODE[]`).
* want to search for in the binary tree. It can be of any type that is returned by the callback
* function.
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
* returns a value. This value is used to identify the nodes that match the given identifier. The
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
* false, all matching nodes will be returned. The default value is false.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
* iteration to be performed. It can have two possible values:
* @returns The method `getNodes` returns an array of `NODE` objects.
*/
override getNodes<C extends BTNCallback<NODE>>(
identifier: ReturnType<C> | undefined,
callback: C = this._DEFAULT_CALLBACK as C,
onlyOne = false,
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE[] {
beginRoot = this.ensureNode(beginRoot);
@ -524,58 +475,57 @@ export class BST<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
* callback function.
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
* that you want to search for in the binary search tree. It can be of any type that is compatible
* with the type of nodes in the tree.
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
* function should take a node as its parameter and return a value that can be compared to the
* `identifier` parameter.
* The function `getNode` returns the first node that matches the given identifier and callback
* function in a binary search tree.
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
* want to search for in the binary search tree. It can be of any type that is compatible with the
* type returned by the callback function.
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
* node matches the desired criteria. It should be a function that takes a node as an argument and
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
* provided, the default callback will be
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
* using the `ensureNode` method. If it is not provided, the `root`
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
* its default value is taken from the `iterationType` property of the class.
* @returns The method is returning a value of type `NODE | null | undefined`.
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
* with that key. If it is a node, the search will start from that node.
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
* of the following values:
* @returns The method is returning a NODE object or undefined.
*/
override getNode<C extends BTNCallback<NODE>>(
identifier: ReturnType<C> | undefined,
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE | undefined {
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
* Time Complexity: O(k log n)
* Space Complexity: O(k + log n)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
* either recursive or iterative methods.
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
* It is used to identify the node that we want to retrieve.
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
* type of iteration to use when searching for a node in the binary tree. It can have two possible
* values:
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
* found in the binary tree. If no node is found, it returns `undefined`.
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
* is typically a unique identifier or a value that can be used to determine the position of the node
* in the tree structure.
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
* It has a default value of `'ITERATIVE'`.
* @returns The method is returning a NODE object or undefined.
*/
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
* Time Complexity: O(log n)
* Space Complexity: O(k + log n)
*/
/**
@ -585,30 +535,31 @@ export class BST<
* The function overrides the depth-first search method and returns an array of the return types of
* the callback function.
* @param {C} callback - The `callback` parameter is a function that will be called for each node
* during the depth-first search traversal. It is an optional parameter and if not provided, a
* default callback function will be used.
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
* nodes are visited during the depth-first search. It can have one of the following values:
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
* Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
* value is provided, the DFS traversal will start from the root of the tree.
* @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
* iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
* during the depth-first search traversal. It is an optional parameter and defaults to
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
* take one of the following values:
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
* node entry. If not specified, the default value is the root of the tree.
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
* following values:
* @returns The method is returning an array of the return type of the callback function.
*/
override dfs<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
pattern: DFSOrderPattern = 'IN',
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = 'ITERATIVE'
): ReturnType<C>[] {
return super.dfs(callback, pattern, beginRoot, iterationType, false);
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
@ -618,85 +569,85 @@ export class BST<
* The function overrides the breadth-first search method and returns an array of the return types of
* the callback function.
* @param {C} callback - The `callback` parameter is a function that will be called for each node
* visited during the breadth-first search traversal. It is an optional parameter and if not
* provided, a default callback function will be used.
* @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
* traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
* the tree is used as the starting point.
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
* be performed during the breadth-first search (BFS) traversal. It determines the order in which the
* nodes are visited.
* @returns The method is returning an array of the return type of the callback function.
* visited during the breadth-first search. It should take a single argument, which is the current
* node being visited, and it can return a value of any type.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
* object. If no value is provided, the default value is the root of the tree.
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
* the following values:
* @returns an array of the return type of the callback function.
*/
override bfs<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): ReturnType<C>[] {
return super.bfs(callback, beginRoot, iterationType, false);
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time complexity: O(n)
* Space complexity: O(n)
*
* The function overrides the listLevels method and returns an array of arrays containing the return
* type of the callback function for each level of the tree.
* The function overrides the listLevels method from the superclass and returns an array of arrays
* containing the results of the callback function applied to each level of the tree.
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
* during the level listing process.
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
* provided, the root of the binary tree is used as the starting point.
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
* be performed on the tree. It determines the order in which the nodes are visited during the
* iteration.
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
* tree during the iteration process.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
* value is provided, the root of
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
* of iteration to be performed on the tree. It can have one of the following values:
* @returns The method is returning a two-dimensional array of the return type of the callback
* function.
*/
override listLevels<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): ReturnType<C>[][] {
return super.listLevels(callback, beginRoot, iterationType, false);
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
* Time complexity: O(n)
* Space complexity: O(n)
*
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
* are either lesser or greater than a target node, depending on the specified comparison type.
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
* each node that meets a certain condition based on a target node and a comparison value.
* @param {C} callback - The `callback` parameter is a function that will be called for each node
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
* parameter of type `NODE` (the node type) and returns a value of any type.
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
* which is the current node being traversed, and returns a value of any type.
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
* `CP`, which is a custom type representing the comparison operator. The possible values for
* `lesserOrGreater` are
* @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
* binary tree that you want to traverse from. It can be specified either by its key, by the node
* object itself, or it can be left undefined to start the traversal from the root of the tree.
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
* performed on the binary tree. It can have two possible values:
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
* 0, or 1, where:
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
* the binary tree that you want to start traversing from. It can be specified either by providing
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
* `targetNode` is provided,
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
* traversal to be performed on the binary tree. It can have two possible values:
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
*/
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
lesserOrGreater: CP = -1,
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
targetNode: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): ReturnType<C>[] {
const targetNodeEnsured = this.ensureNode(targetNode);
@ -734,19 +685,20 @@ export class BST<
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
* Time complexity: O(n)
* Space complexity: O(n)
*
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
* ensures the tree is perfectly balanced.
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
* type of iteration to use when building a balanced binary search tree. It can have two possible
* values:
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
* the binary search tree is perfectly balanced, otherwise it returns `false`.
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
* specifies the type of iteration to use when building a balanced binary search tree. It has a
* default value of `this.iterationType`, which means it will use the iteration type specified in the
* current instance of the class.
* @returns The function `perfectlyBalance` returns a boolean value.
*/
perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
@ -787,27 +739,20 @@ export class BST<
}
/**
* Balancing Adjustment:
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
*
* Use Cases and Efficiency:
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
* Time complexity: O(n)
* Space complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
*
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
* to check if the AVL tree is balanced. It can have two possible values:
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
* iterative approach.
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
* value of `this.iterationType`, which means it will use the iteration type specified in the current
* instance of the AVL tree.
* @returns a boolean value.
*/
isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
@ -855,9 +800,45 @@ export class BST<
}
/**
* The function sets the root property of an object and updates the parent property of the new root.
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
* can either be an object of type `NODE` or it can be `undefined`.
* Time complexity: O(n)
* Space complexity: O(n)
*/
protected _DEFAULT_COMPARATOR = (a: K, b: K): number => {
if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) {
throw TypeError(
'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.'
);
}
if (a > b) return 1;
if (a < b) return -1;
return 0;
};
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
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.
*/
get comparator() {
return this._comparator;
}
/**
* The function sets the root of a tree-like structure and updates the parent property of the new
* root.
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
*/
protected override _setRoot(v: NODE | undefined) {
if (v) {

View file

@ -9,6 +9,7 @@ import type {
RedBlackTreeNested,
RedBlackTreeNodeNested
} from '../../types';
import { BTNEntry } from '../../types';
import { BST, BSTNode } from './bst';
import { IBinaryTree } from '../../interfaces';
@ -55,28 +56,32 @@ export class RedBlackTreeNode<
export class RedBlackTree<
K extends Comparable,
V = any,
R = BTNEntry<K, V>,
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>
TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
>
extends BST<K, V, NODE, TREE>
implements IBinaryTree<K, V, NODE, TREE> {
extends BST<K, V, R, NODE, TREE>
implements IBinaryTree<K, V, R, NODE, TREE> {
/**
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
* contain keys, nodes, or entries. It is used to initialize the RBTree with the provided keys,
* nodes, or entries.
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
* initialize the RBTree with the provided elements.
* @param [options] - The `options` parameter is an optional object that can be passed to the
* constructor. It allows you to customize the behavior of the RBTree. It can include properties such
* as `compareKeys`, `compareValues`, `allowDuplicates`, etc. These properties define how the RBTree
* should compare keys and
* constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
* depend on the implementation
*/
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
options?: RBTreeOptions<K, V, R>
) {
super([], options);
this._root = this.NIL;
if (keysOrNodesOrEntries) {
this.addMany(keysOrNodesOrEntries);
if (keysOrNodesOrEntriesOrRawElements) {
this.addMany(keysOrNodesOrEntriesOrRawElements);
}
}
@ -92,29 +97,30 @@ export class RedBlackTree<
/**
* The function creates a new Red-Black Tree node with the specified key, value, and color.
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
* which is a generic type representing the key's data type.
* @param {K} key - The key parameter represents the key value of the node being created. It is of
* type K, which is a generic type that can be replaced with any specific type when using the
* function.
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
* associated with the key in the node. It is not required and can be omitted if not needed.
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
* Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
* can be either "'RED'" or "'BLACK'".
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
* value, and color.
* associated with the key in the node. It is not required and can be omitted if you only need to
* create a node with a key.
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
* set to "BLACK" if not specified.
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
* returned.
*/
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE {
return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
}
/**
* The function creates a Red-Black Tree with the given options and returns it.
* @param [options] - The `options` parameter is an optional object that contains configuration
* options for creating the Red-Black Tree. It is of type `RBTreeOptions<K>`, where `K` represents
* the type of keys in the tree.
* The function creates a new Red-Black Tree with the specified options.
* @param [options] - The `options` parameter is an optional object that contains additional
* configuration options for creating the Red-Black Tree. It has the following properties:
* @returns a new instance of a RedBlackTree object.
*/
override createTree(options?: RBTreeOptions<K>): TREE {
return new RedBlackTree<K, V, NODE, TREE>([], {
override createTree(options?: RBTreeOptions<K, V, R>): TREE {
return new RedBlackTree<K, V, R, NODE, TREE>([], {
iterationType: this.iterationType,
...options
}) as TREE;
@ -126,54 +132,57 @@ export class RedBlackTree<
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
* valid, otherwise it returns undefined.
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
* @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
* @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
*/
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
let node: NODE | undefined;
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
return;
} else if (this.isNode(keyOrNodeOrEntry)) {
node = keyOrNodeOrEntry;
} else if (this.isEntry(keyOrNodeOrEntry)) {
const [key, value] = keyOrNodeOrEntry;
if (key === undefined || key === null) {
return;
} else {
node = this.createNode(key, value, 'RED');
}
} else if (!this.isNode(keyOrNodeOrEntry)) {
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
} else {
return;
}
return node;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
* /
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function checks if the input is an instance of the RedBlackTreeNode class.
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The object to check.
* @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `RedBlackTreeNode` class.
*/
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
}
// /**
// * Time Complexity: O(1)
// * Space Complexity: O(1)
// */
//
// /**
// * Time Complexity: O(1)
// * Space Complexity: O(1)
// *
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
// * valid, otherwise it returns undefined.
// * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
// */
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
//
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
//
// if (this.toEntryFn) {
// const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
// if (key) return this.createNode(key, entryValue ?? value, 'RED');
// }
//
// if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
// const [key, value] = keyOrNodeOrEntryOrRawElement;
// if (key === undefined || key === null) return;
// else return this.createNode(key, value, 'RED');
// }
//
// if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'RED');
//
// return ;
// }
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -200,17 +209,19 @@ export class RedBlackTree<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function adds a new node to a Red-Black Tree data structure and returns a boolean indicating
* whether the operation was successful.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
* entry.
* @param {V} [value] - The `value` parameter is the value associated with the key that is being
* added to the tree.
* @returns The method is returning a boolean value. It returns true if the node was successfully
* added or updated, and false otherwise.
* The function adds a new node to a binary search tree and returns true if the node was successfully
* added.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
* the key in the data structure. It represents the value that you want to add or update in the data
* structure.
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
* the method returns true. If the node already exists and its value is updated, the method also
* returns true. If the node cannot be added or updated, the method returns false.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
if (!this.isRealNode(newNode)) return false;
const insertStatus = this._insert(newNode);
@ -236,16 +247,16 @@ export class RedBlackTree<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function `delete` in a binary tree class deletes a node from the tree and fixes the tree if
* necessary.
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
* identifier of the node that needs to be deleted from the binary tree. It can be of any type that
* is returned by the callback function `C`. It can also be `null` or `undefined` if the node to be
* deleted is not found.
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
* return the identifier of the node to
* The function overrides the delete method of a binary tree data structure, allowing for the
* deletion of a node and maintaining the balance of the tree.
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
* that identifies the node to be deleted from the binary tree. It can be of any type that is
* returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
* delete.
* @param {C} callback - The `callback` parameter is a function that is used to determine the
* equality of nodes in the binary tree. It is optional and has a default value of
* `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
* that extends the `BTNCallback
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
*/
override delete<C extends BTNCallback<NODE>>(
@ -309,6 +320,14 @@ export class RedBlackTree<
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function sets the root of a tree-like structure and updates the parent property of the new
* root.
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
@ -332,8 +351,8 @@ export class RedBlackTree<
* The function replaces an old node with a new node while preserving the color of the old node.
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
* the data structure.
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the old node in
* the data structure.
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
* data structure.
* @returns The method is returning the result of calling the `_replaceNode` method from the
* superclass, with the `oldNode` and `newNode` parameters.
*/
@ -352,12 +371,13 @@ export class RedBlackTree<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `_insert` function inserts or updates a node in a binary search tree and performs necessary
* fix-ups to maintain the red-black tree properties.
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into a
* binary search tree. It contains a `key` property that is used to determine the position of the
* node in the tree.
* @returns {'inserted' | 'updated'} - The result of the insertion.
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
* maintain the red-black tree properties.
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
* binary search tree.
* @returns a string value indicating the result of the insertion operation. It can return either
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
* was created and inserted into the tree.
*/
protected _insert(node: NODE): CRUD {
let current = this.root;
@ -432,8 +452,8 @@ export class RedBlackTree<
* Space Complexity: O(1)
*
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree. It can
* either be a valid node object or `undefined`.
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
* structure. It can either be a valid node or `undefined`.
*/
protected _insertFixup(z: NODE | undefined): void {
// Continue fixing the tree as long as the parent of z is red
@ -504,9 +524,10 @@ export class RedBlackTree<
*
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
* the colors and performing rotations.
* @param {NODE | undefined} node - The `node` parameter represents a node in a Red-Black Tree data
* structure. It can be either a valid node object or `undefined`.
* @returns The function does not return any value. It has a return type of `void`.
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
* be either a valid node object or `undefined`.
* @returns The function does not return any value. It has a return type of `void`, which means it
* does not return anything.
*/
protected _deleteFixup(node: NODE | undefined): void {
// Early exit condition

View file

@ -17,6 +17,7 @@ import type {
TreeMultiMapNodeNested,
TreeMultiMapOptions
} from '../../types';
import { BTNEntry } from '../../types';
import { IBinaryTree } from '../../interfaces';
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
@ -65,23 +66,27 @@ export class TreeMultiMapNode<
export class TreeMultiMap<
K extends Comparable,
V = any,
R = BTNEntry<K, V>,
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>
TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
>
extends RedBlackTree<K, V, NODE, TREE>
implements IBinaryTree<K, V, NODE, TREE> {
extends RedBlackTree<K, V, R, NODE, TREE>
implements IBinaryTree<K, V, R, NODE, TREE> {
/**
* The constructor function initializes a new instance of the TreeMultiMap class with optional
* initial keys, nodes, or entries.
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
* contain keys, nodes, or entries. It is used to initialize the TreeMultiMap with the provided keys,
* nodes, or entries.
* @param [options] - The `options` parameter is an optional object that can be passed to the
* constructor. It allows you to customize the behavior of the `TreeMultiMap` instance.
* The constructor function initializes a TreeMultiMap object with optional initial data.
* @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
* TreeMultiMap with initial data.
* @param [options] - The `options` parameter is an optional object that can be used to customize the
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
* `compareValues`, which are functions used to compare keys and values respectively.
*/
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: TreeMultiMapOptions<K>) {
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [],
options?: TreeMultiMapOptions<K, V, R>
) {
super([], options);
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
}
protected _count = 0;
@ -117,16 +122,15 @@ export class TreeMultiMap<
/**
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
* which is a generic type representing the key type of the node.
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
* node. It is an optional parameter, which means it can be omitted when calling the `createNode`
* function. If provided, it should be of type `V`.
* which is a generic type representing the type of keys in the tree.
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
* associated with the key in the node. It is of type `V`, which can be any data type.
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
* the tree. It is an optional parameter and is used to keep track of the number of values associated
* with a key in the tree.
* @returns A new instance of the TreeMultiMapNode class is being returned.
* @returns A new instance of the TreeMultiMapNode class, casted as NODE.
*/
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
return new TreeMultiMapNode(key, value, count, color) as NODE;
@ -135,64 +139,67 @@ export class TreeMultiMap<
/**
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
* @param [options] - The `options` parameter is an optional object that contains additional
* configuration options for creating the `TreeMultiMap`. It can include properties such as
* `keyComparator`, `valueComparator`, `allowDuplicates`, etc.
* configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
* R>`.
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
* existing `iterationType` option. The returned value is casted as `TREE`.
* existing `iterationType` property. The returned value is casted as `TREE`.
*/
override createTree(options?: TreeMultiMapOptions<K>): TREE {
return new TreeMultiMap<K, V, NODE, TREE>([], {
override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
return new TreeMultiMap<K, V, R, NODE, TREE>([], {
iterationType: this.iterationType,
...options
}) as TREE;
}
/**
* The function `keyValueOrEntryToNode` takes a key, value, and count and returns a node if the input
* is valid.
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
* NODE>`. It can accept three types of values:
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
* value associated with a key in a key-value pair.
* @param [count=1] - The count parameter is an optional parameter that specifies the number of times
* the key-value pair should be added to the node. If not provided, it defaults to 1.
* @returns a NODE object or undefined.
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
* node based on the input.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that represents the value
* associated with the key in the node. It is used when creating a new node or updating the value of
* an existing node.
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
* @returns either a NODE object or undefined.
*/
override keyValueOrEntryToNode(
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
override keyValueOrEntryOrRawElementToNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
value?: V,
count = 1
): NODE | undefined {
let node: NODE | undefined;
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
return;
} else if (this.isNode(keyOrNodeOrEntry)) {
node = keyOrNodeOrEntry;
} else if (this.isEntry(keyOrNodeOrEntry)) {
const [key, value] = keyOrNodeOrEntry;
if (key === undefined || key === null) {
return;
} else {
node = this.createNode(key, value, 'BLACK', count);
}
} else if (!this.isNode(keyOrNodeOrEntry)) {
node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
} else {
return;
if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null) return;
if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
if (this.toEntryFn) {
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
if (key) return this.getNodeByKey(key);
}
return node;
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
const [key, value] = keyOrNodeOrEntryOrRawElement;
if (key === undefined || key === null) return;
else return this.createNode(key, value, 'BLACK', count);
}
if (this.isKey(keyOrNodeOrEntryOrRawElement))
return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'BLACK', count);
return;
}
/**
* The function "isNode" checks if a given key, node, or entry is an instance of the TreeMultiMapNode
* class.
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
* NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntry` is an instance
* of the `TreeMultiMapNode` class.
* The function checks if the input is an instance of the TreeMultiMapNode class.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `TreeMultiMapNode` class.
*/
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
return keyOrNodeOrEntry instanceof TreeMultiMapNode;
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
}
/**
@ -204,17 +211,20 @@ export class TreeMultiMap<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function overrides the add method in TypeScript and adds a new node to the data structure.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
* The function overrides the add method of a class and adds a new node to a data structure, updating
* the count and returning a boolean indicating success.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
* data structure.
* data structure. It is an optional parameter, so it can be omitted if not needed.
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
* be added once. However, you can specify a different value for `count` if you want to add
* @returns a boolean value.
* be added to the data structure. By default, it is set to 1, meaning that if no value is provided
* for `count`, the key-value pair will be added once.
* @returns The method is returning a boolean value. It returns true if the addition of the new node
* was successful, and false otherwise.
*/
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
const orgCount = newNode?.count || 0;
const isSuccessAdded = super.add(newNode);
@ -235,20 +245,18 @@ export class TreeMultiMap<
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `delete` function in a TypeScript class is used to delete nodes from a binary tree based on a
* given identifier, and it returns an array of results containing information about the deleted
* nodes.
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value used
* to identify the node to be deleted. It can be of any type that is returned by the callback
* function. It can also be null or undefined if no node needs to be deleted.
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
* used
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
* node when performing deletion. If set to true, the count of the target node will not be considered
* and the node will be deleted regardless of its count. If set to false (default), the count of the
* target node will be decremented
* The function `delete` is used to remove a node from a binary tree and fix the tree if necessary.
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value or
* key that is used to identify the node that needs to be deleted from the binary tree. It can be of
* any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
* the node to be deleted
* @param {C} callback - The `callback` parameter is a function that is used to determine the
* equality of nodes in the binary tree. It is optional and has a default value of
* `this._DEFAULT_CALLBACK`. The `callback` function is used to compare nodes when searching for a
* specific node or when performing other operations on the
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
* being deleted. If set to true, the count of the node will not be taken into account when deleting
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
*/
override delete<C extends BTNCallback<NODE>>(
@ -372,10 +380,12 @@ export class TreeMultiMap<
*
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
* tree using either a recursive or iterative approach.
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
* type of iteration to use when building the balanced binary search tree. It can have two possible
* values:
* @returns a boolean value.
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
* specifies the type of iteration to use when building the balanced binary search tree. It has a
* default value of `this.iterationType`, which means it will use the iteration type specified by the
* `iterationType` property of the current object.
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
* balancing operation is successful, and `false` if there are no nodes to balance.
*/
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
const sorted = this.dfs(node => node, 'IN'),
@ -434,19 +444,27 @@ export class TreeMultiMap<
}
/**
* The function swaps the properties of two nodes in a binary search tree.
* @param srcNode - The source node that needs to be swapped with the destination node. It can be
* either a key or a node object.
* @param destNode - The `destNode` parameter is the node in the binary search tree where the
* properties will be swapped with the `srcNode`.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
* in a binary search tree.
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
* instance of the `BSTNKeyOrNode<K, NODE>` class.
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
* node where the properties will be swapped with the source node.
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
* If both `srcNode` and `destNode` are valid nodes, the method swaps their `key`, `value`, `count`,
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
* If either `srcNode` or `destNode` is
* If either `srcNode` or `destNode` is undefined, it returns undefined.
*/
protected override _swapProperties(
srcNode: BSTNKeyOrNode<K, NODE>,
destNode: BSTNKeyOrNode<K, NODE>
srcNode: R | BSTNKeyOrNode<K, NODE>,
destNode: R | BSTNKeyOrNode<K, NODE>
): NODE | undefined {
srcNode = this.ensureNode(srcNode);
destNode = this.ensureNode(destNode);
@ -473,14 +491,22 @@ export class TreeMultiMap<
}
/**
* The function replaces an old node with a new node and updates the count property of the new node.
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
* needs to be replaced in the data structure.
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
* @returns The method is returning the result of calling the `_replaceNode` method from the
* superclass, after updating the `count` property of the `newNode` object.
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function replaces an old node with a new node and updates the count property of the new node.
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
* structure.
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
* @returns The method is returning the result of calling the `_replaceNode` method from the
* superclass, which is of type `NODE`.
*/
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
newNode.count = oldNode.count + newNode.count;
return super._replaceNode(oldNode, newNode);
}

View file

@ -496,9 +496,9 @@ 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).
*/
@ -639,9 +639,9 @@ export abstract class AbstractGraph<
* 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).
*/
@ -777,9 +777,9 @@ export abstract class AbstractGraph<
* 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).
*
@ -887,9 +887,9 @@ export abstract class AbstractGraph<
/**
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
* /
*/
/**
/**
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
*/
@ -907,9 +907,9 @@ export abstract class AbstractGraph<
* 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).
*

View file

@ -16,20 +16,21 @@ import { IterableEntryBase } from '../base';
import { isWeakKey, rangeCheck } from '../../utils';
/**
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key map to a value.
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
* 3. Unique Keys: Keys are unique.
* If you try to insert another entry with the same key, the new one will replace the old entry.
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
*/
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
/**
* The constructor function initializes a HashMap object with an optional initial collection and
* options.
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
* `T`. It is an optional parameter and its default value is an empty array `[]`.
* @param [options] - The `options` parameter is an optional object that can contain two properties:
*/
constructor(rawCollection: Iterable<R | [K, V]> = [], options?: HashMapOptions<K, V, R>) {
constructor(entryOrRawElements: Iterable<R | [K, V]> = [], options?: HashMapOptions<K, V, R>) {
super();
if (options) {
const { hashFn, toEntryFn } = options;
@ -40,8 +41,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
this._toEntryFn = toEntryFn;
}
}
if (rawCollection) {
this.setMany(rawCollection);
if (entryOrRawElements) {
this.setMany(entryOrRawElements);
}
}
@ -67,16 +68,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
return this._objMap;
}
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => {
if (this.isEntry(rawElement)) {
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
return rawElement;
} else {
throw new Error(
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
);
}
};
protected _toEntryFn?: (rawElement: R) => [K, V];
/**
* The function returns the value of the _toEntryFn property.
@ -164,23 +156,24 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
/**
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
* pair using a mapping function, and sets each key-value pair in the current object.
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
* `T`.
* @returns The `setMany` function is returning an array of booleans.
*/
setMany(rawCollection: Iterable<R | [K, V]>): boolean[] {
setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
const results: boolean[] = [];
for (const rawEle of rawCollection) {
let key, value;
for (const rawEle of entryOrRawElements) {
let key: K | undefined, value: V | undefined;
if (this.isEntry(rawEle)) {
key = rawEle[0];
value = rawEle[1];
} else {
} else if (this.toEntryFn) {
const item = this.toEntryFn(rawEle);
key = item[0];
value = item[1];
}
results.push(this.set(key, value));
if (key !== undefined && value !== undefined) results.push(this.set(key, value));
}
return results;
}
@ -383,14 +376,14 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
/**
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements. It is
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements. It is
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
* `rawCollection` is converted to a key-value pair using the `toEntryFn` function (if provided) and
* `entryOrRawElements` is converted to a key-value pair using the `toEntryFn` function (if provided) and
* then added to the HashMap
* @param [options] - The `options` parameter is an optional object that can contain the following
* properties:
*/
constructor(rawCollection: Iterable<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
constructor(entryOrRawElements: Iterable<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
super();
this._sentinel = <HashMapLinkedNode<K, V>>{};
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
@ -405,8 +398,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
}
if (rawCollection) {
for (const el of rawCollection) {
if (entryOrRawElements) {
for (const el of entryOrRawElements) {
const [key, value] = this.toEntryFn(el);
this.set(key, value);
}
@ -460,7 +453,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
/**
* The function returns the head node of a HashMapLinkedNode.
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and
* value type `V | undefined`.
* a value type `V | undefined`.
*/
get head(): HashMapLinkedNode<K, V | undefined> {
return this._head;
@ -482,7 +475,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return rawElement;
} else {
throw new Error(
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
"If the provided entryOrRawElements does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
);
}
};
@ -590,7 +583,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
node = this.objMap.get(hash);
if (!node && isNewKey) {
// Create new node
// Create a new node
node = { key: <K>hash, value, prev: this.tail, next: this._sentinel };
this.objMap.set(hash, node);
} else if (node) {
@ -630,13 +623,13 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
* using a provided function, and sets each key-value pair in the current object, returning an array
* of booleans indicating the success of each set operation.
* @param rawCollection - The rawCollection parameter is an iterable collection of elements of type
* @param entryOrRawElements - The entryOrRawElements parameter is an iterable collection of elements of type
* R.
* @returns The `setMany` function returns an array of booleans.
*/
setMany(rawCollection: Iterable<R>): boolean[] {
setMany(entryOrRawElements: Iterable<R>): boolean[] {
const results: boolean[] = [];
for (const rawEle of rawCollection) {
for (const rawEle of entryOrRawElements) {
const [key, value] = this.toEntryFn(rawEle);
results.push(this.set(key, value));
}
@ -692,9 +685,9 @@ 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)
*
@ -717,9 +710,9 @@ 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)
*
@ -764,9 +757,9 @@ 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)
*
@ -787,9 +780,9 @@ 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)
*
@ -814,9 +807,9 @@ 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)
*
@ -854,9 +847,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* /
*/
/**
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
@ -886,9 +879,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* /
*/
/**
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
@ -940,9 +933,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
* 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)
* where n is the number of entries in the LinkedHashMap.

View file

@ -814,9 +814,9 @@ export class Deque<E> extends IterableElementBase<E> {
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
* /
*/
/**
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*

View file

@ -358,7 +358,7 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
* values as the original `LinkedListQueue`.
*/
clone(): LinkedListQueue<E> {
override clone(): LinkedListQueue<E> {
return new LinkedListQueue<E>(this.values());
}
}

View file

@ -91,7 +91,7 @@ export class TrieNode {
* 8. Autocomplete: Providing recommended words or phrases as a user types.
* 9. Spell Check: Checking the spelling of words.
* 10. IP Routing: Used in certain types of IP routing algorithms.
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data."
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
*/
export class Trie extends IterableElementBase<string, Trie> {
/**
@ -187,7 +187,7 @@ export class Trie extends IterableElementBase<string, Trie> {
* @param {string} word - The word to check for.
* @returns {boolean} True if the word is present in the Trie.
*/
has(word: string): boolean {
override has(word: string): boolean {
word = this._caseProcess(word);
let cur = this.root;
for (const c of word) {

View file

@ -12,16 +12,17 @@ import type {
export interface IBinaryTree<
K extends Comparable,
V = any,
N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>,
TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>
R = [K, V],
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>,
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTreeNested<K, V, R, NODE>
> {
createNode(key: K, value?: N['value']): N;
createNode(key: K, value?: NODE['value']): NODE;
createTree(options?: Partial<BinaryTreeOptions>): TREE;
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
add(keyOrNodeOrEntryOrRawElement: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<NODE>[];
}

View file

@ -1,4 +1,3 @@
export type BSTVariant = 'STANDARD' | 'INVERSE';
export type CP = 1 | -1 | 0;
/**
@ -39,7 +38,7 @@ export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
export type BTNodePureKeyOrNode<K, N> = K | N;
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
export type BTNPureKeyOrNodeOrEntry<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
export type BSTNKeyOrNode<K, N> = K | undefined | N;

View file

@ -4,6 +4,6 @@ import { Comparable } from "../../utils";
export type AVLTreeMultiMapNodeNested<K extends Comparable, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type AVLTreeMultiMapNested<K extends Comparable, V, N extends AVLTreeMultiMapNode<K, V, N>> = AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type AVLTreeMultiMapNested<K extends Comparable, V, R, NODE extends AVLTreeMultiMapNode<K, V, NODE>> = AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type AVLTreeMultiMapOptions<K> = AVLTreeOptions<K> & {}
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {}

View file

@ -4,6 +4,6 @@ import { Comparable } from "../../utils";
export type AVLTreeNodeNested<K extends Comparable, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type AVLTreeNested<K extends Comparable, V, N extends AVLTreeNode<K, V, N>> = AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type AVLTreeNested<K extends Comparable, V, R, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type AVLTreeOptions<K> = BSTOptions<K> & {};
export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};

View file

@ -1,11 +1,12 @@
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
import { IterationType } from "../../common";
import { Comparable } from "../../utils";
import { BTNEntry, IterationType } from '../../common';
import { Comparable } from '../../utils';
export type BinaryTreeNodeNested<K extends Comparable, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BinaryTreeNested<K extends Comparable, V, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BinaryTreeNested<K extends Comparable, V, R, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BinaryTreeOptions = {
export type BinaryTreeOptions<K, V, R> = {
iterationType?: IterationType
toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
}

View file

@ -1,12 +1,12 @@
import { BST, BSTNode } from '../../../data-structures';
import type { BinaryTreeOptions } from './binary-tree';
import { Comparator } from "../../common";
import { Comparable } from "../../utils";
import { Comparator } from '../../common';
import { Comparable } from '../../utils';
export type BSTNodeNested<K extends Comparable, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BSTNested<K extends Comparable, V, N extends BSTNode<K, V, N>> = BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BSTNested<K extends Comparable, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BSTOptions<K> = BinaryTreeOptions & {
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
comparator?: Comparator<K>
}

View file

@ -6,6 +6,6 @@ export type RBTNColor = 'RED' | 'BLACK';
export type RedBlackTreeNodeNested<K extends Comparable, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type RedBlackTreeNested<K extends Comparable, V, N extends RedBlackTreeNode<K, V, N>> = RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type RedBlackTreeNested<K extends Comparable, V, R, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type RBTreeOptions<K> = BSTOptions<K> & {};
export type RBTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};

View file

@ -1,9 +1,9 @@
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures';
import type { RBTreeOptions } from './rb-tree';
import { Comparable } from "../../utils";
import { Comparable } from '../../utils';
export type TreeMultiMapNodeNested<K extends Comparable, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type TreeMultiMapNested<K extends Comparable, V, N extends TreeMultiMapNode<K, V, N>> = TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type TreeMultiMapNested<K extends Comparable, V, R, NODE extends TreeMultiMapNode<K, V, NODE>> = TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type TreeMultiMapOptions<K> = RBTreeOptions<K> & {}
export type TreeMultiMapOptions<K, V, R> = RBTreeOptions<K, V, R> & {}

View file

@ -108,7 +108,7 @@ export const roundFixed = (num: number, digit: number = 10) => {
export function isComparable(key: any): key is Comparable {
const keyType = typeof key;
if (keyType === 'number') return isNaN(key);
if (keyType === 'number') return !isNaN(key);
if (keyType === 'string') return true;
if (keyType === 'bigint') return true;
if (keyType === 'boolean') return true;
@ -117,8 +117,8 @@ export function isComparable(key: any): key is Comparable {
if (keyType === 'function') return isComparable(key());
if (keyType === 'object') {
if (key === null) return true;
if (typeof key.valueOf === 'function') return isComparable(key.valueOf());
if (typeof key.toString === 'function') return isComparable(key.toString());
// if (typeof key.valueOf === 'function') return isComparable(key.valueOf()); // This will keep recursing because every object has a valueOf method.
// if (typeof key.toString === 'function') return isComparable(key.toString()); // This will also keep recursing because every string type has a toString method.
return false;
}

View file

@ -11,6 +11,20 @@
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.44.0/dist/umd/data-structure-typed.min.js'></script>-->
<script src='https://unpkg.com/js-sdsl@4.4.2/dist/umd/js-sdsl.js'></script>
<script>
const logPerf = function (label = 'function running cost', fn, args, thisArg) {
const start = performance.now();
let result;
if (thisArg) {
if (args && args.length > 0) result = fn.apply(thisArg, args);
else result = fn.apply(thisArg);
} else {
if (args && args.length > 0) result = fn(...args);
else result = fn();
}
console.log(`${(performance.now() - start).toFixed(2)} ms, ${label}, ${result}`);
}
</script>
</head>
<body>
<div id='app'>
@ -26,19 +40,19 @@
const { Queue } = dataStructureTyped;
const queue = new Queue();
const n = 1000000;
const startEn = performance.now();
for (let i = 0; i < n; i++) {
queue.push(i);
}
console.log((performance.now() - startEn).toFixed(2), `Queue ${n.toLocaleString()} enqueue `);
logPerf(`Queue ${n.toLocaleString()} enqueue`, () => {
for (let i = 0; i < n; i++) {
queue.push(i);
}
})
let last = 0;
const startTime = performance.now();
for (let i = 0; i < n; i++) {
last = queue.shift();
}
console.log((performance.now() - startTime).toFixed(2), `Queue ${n.toLocaleString()} dequeue `);
logPerf(`Queue ${n.toLocaleString()} dequeue `, () => {
for (let i = 0; i < n; i++) {
last = queue.shift();
}
})
} catch (e) {
console.error(e);

View file

@ -689,3 +689,71 @@ describe('AVLTreeMultiMap iterative methods test', () => {
expect([...values]).toEqual(['a', 'b', 'c']);
});
});
describe('AVLTree toEntryFn', () => {
it('should toEntryFn 1', () => {
const tree = new AVLTreeMultiMap<number, number, { obj: { id: number } }>([], {
toEntryFn: ele => [ele.obj.id, ele.obj.id]
});
tree.add({ obj: { id: 1 } });
tree.add({ obj: { id: 2 } });
tree.add({ obj: { id: 3 } });
tree.add({ obj: { id: 4 } });
tree.add({ obj: { id: 5 } });
const expected = [1, 2, 3, 4, 5];
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
});
it('should toEntryFn 2', () => {
const tree = new AVLTreeMultiMap<number, number, { obj: { id: number } }>(
[{ obj: { id: 1 } }, { obj: { id: 2 } }, { obj: { id: 3 } }, { obj: { id: 4 } }, { obj: { id: 5 } }],
{
toEntryFn: ele => [ele.obj.id, ele.obj.id]
}
);
const expected = [1, 2, 3, 4, 5];
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
});
it('should toEntryFn throw error', () => {
expect(
() =>
new AVLTreeMultiMap<{ obj: { id: number } }, number>([
{ obj: { id: 1 } },
{ obj: { id: 2 } },
{ obj: { id: 3 } },
{ obj: { id: 4 } },
{ obj: { id: 5 } }
])
).toThrowError(
'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.'
);
});
it('should toEntryFn 3', () => {
const tree = new AVLTreeMultiMap<{ obj: { id: number } }, number>(
[{ obj: { id: 1 } }, { obj: { id: 2 } }, { obj: { id: 3 } }, { obj: { id: 4 } }, { obj: { id: 5 } }],
{ comparator: (a, b) => a.obj.id - b.obj.id }
);
const expected = [
{ obj: { id: 1 } },
{ obj: { id: 2 } },
{ obj: { id: 3 } },
{ obj: { id: 4 } },
{ obj: { id: 5 } }
];
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
});
});

View file

@ -403,6 +403,62 @@ describe('BinaryTree Morris Traversal', () => {
});
});
describe('BinaryTree toEntryFn', () => {
it('should toEntryFn 1', () => {
const tree = new BinaryTree<number, number, { obj: { id: number } }>([], {
toEntryFn: ele => [ele.obj.id, ele.obj.id]
});
tree.add({ obj: { id: 1 } });
tree.add({ obj: { id: 2 } });
tree.add({ obj: { id: 3 } });
tree.add({ obj: { id: 4 } });
tree.add({ obj: { id: 5 } });
const expected = [4, 2, 5, 1, 3];
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
});
it('should toEntryFn 2', () => {
const tree = new BinaryTree<number, number, { obj: { id: number } }>(
[{ obj: { id: 1 } }, { obj: { id: 2 } }, { obj: { id: 3 } }, { obj: { id: 4 } }, { obj: { id: 5 } }],
{
toEntryFn: ele => [ele.obj.id, ele.obj.id]
}
);
const expected = [4, 2, 5, 1, 3];
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
});
it('should toEntryFn 3', () => {
const tree = new BinaryTree<{ obj: { id: number } }, number>([
{ obj: { id: 1 } },
{ obj: { id: 2 } },
{ obj: { id: 3 } },
{ obj: { id: 4 } },
{ obj: { id: 5 } }
]);
const expected = [
{ obj: { id: 4 } },
{ obj: { id: 2 } },
{ obj: { id: 5 } },
{ obj: { id: 1 } },
{ obj: { id: 3 } }
];
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
});
});
describe('BinaryTree traversals', () => {
const tree = new BinaryTree<number>();

View file

@ -132,11 +132,11 @@ class MyDirectedGraph<
VO extends MyVertex<V> = MyVertex<V>,
EO extends MyEdge<E> = MyEdge<E>
> extends DirectedGraph<V, E, VO, EO> {
createVertex(key: VertexKey, value: V): VO {
override createVertex(key: VertexKey, value: V): VO {
return new MyVertex(key, value) as VO;
}
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
return new MyEdge(src, dest, weight ?? 1, value) as EO;
}

View file

@ -206,14 +206,20 @@ export function logBigOMetrics(target: any, propertyKey: string, descriptor: Pro
return descriptor;
}
export const logPerf = function (fn: (...args: any[]) => any, args: any[], thisArg?: any) {
export const logPerf = function (
label: string = 'function running cost',
fn: (...args: any[]) => any,
args: any[],
thisArg?: any
) {
const start = performance.now();
let result: any;
if (thisArg) {
if (args && args.length > 0) fn.apply(thisArg, args);
else fn.apply(thisArg);
if (args && args.length > 0) result = fn.apply(thisArg, args);
else result = fn.apply(thisArg);
} else {
if (args && args.length > 0) fn(...args);
else fn();
if (args && args.length > 0) result = fn(...args);
else result = fn();
}
console.log(`function running cost : ${(performance.now() - start).toFixed(2)} ms`);
console.log(`${(performance.now() - start).toFixed(2)} ms, ${label}, ${result}`);
};

View file

@ -16,6 +16,7 @@
"strict": true, // Enable strict type checking, including enabling all strict options.
"traceResolution": false, // Whether to trace the module resolution process.
"types": ["node", "jest"], // Specifies the name of the type declaration file to include.
"noImplicitOverride": true,
"typeRoots": ["node_modules/@types", "src/types", "test/types"]
},
"compileOnSave": false, // Whether to automatically compile when saving.