fix: #82. refactor: Minor performance optimization, rewrite the _compare method within the Red-Black Tree. test: Change the test sequence in binary-tree-overall.test.

This commit is contained in:
Revone 2024-01-19 17:13:25 +08:00
parent 2eac608960
commit 3fa7155c97
8 changed files with 146 additions and 167 deletions

View file

@ -8,7 +8,9 @@ All notable changes to this project will be documented in this file.
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
## [v1.51.5](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.51.6](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming)
## [v1.51.5](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...v1.51.5) (18 January 2024)
### Changes

View file

@ -736,43 +736,45 @@ 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.44</td><td>155.39</td><td>1.89e-4</td></tr><tr><td>100,000 add & poll</td><td>31.54</td><td>31.71</td><td>7.91e-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.31</td><td>158.42</td><td>1.69e-4</td></tr><tr><td>100,000 add & poll</td><td>32.21</td><td>31.05</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'>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>55.64</td><td>17.97</td><td>3.93e-4</td></tr><tr><td>100,000 add randomly</td><td>70.35</td><td>14.21</td><td>0.00</td></tr><tr><td>100,000 get</td><td>115.51</td><td>8.66</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>27.64</td><td>36.18</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>120.73</td><td>8.28</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>223.37</td><td>4.48</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>59.70</td><td>16.75</td><td>7.70e-4</td></tr><tr><td>100,000 add randomly</td><td>72.55</td><td>13.78</td><td>0.00</td></tr><tr><td>100,000 get</td><td>104.48</td><td>9.57</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>27.70</td><td>36.10</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>126.07</td><td>7.93</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>212.96</td><td>4.70</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'>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>42.87</td><td>23.33</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.87</td><td>205.17</td><td>6.94e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2196.84</td><td>0.46</td><td>0.19</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>44.49</td><td>22.48</td><td>0.01</td></tr><tr><td>100,000 push & shift</td><td>4.82</td><td>207.40</td><td>5.25e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2201.76</td><td>0.45</td><td>0.10</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>23.68</td><td>42.22</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>30.68</td><td>32.60</td><td>0.00</td></tr><tr><td>1,000,000 push & shift</td><td>30.49</td><td>32.80</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>3.21</td><td>311.51</td><td>2.41e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2510.08</td><td>0.40</td><td>0.34</td></tr><tr><td>100,000 unshift & shift</td><td>2.89</td><td>346.57</td><td>2.98e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4581.65</td><td>0.22</td><td>0.40</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>120.66</td><td>8.29</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>202.57</td><td>4.94</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>167.46</td><td>5.97</td><td>0.01</td></tr><tr><td>1,000,000 set & get</td><td>115.60</td><td>8.65</td><td>0.01</td></tr><tr><td>Native JS Map 1,000,000 set & get</td><td>265.34</td><td>3.77</td><td>0.01</td></tr><tr><td>Native JS Set 1,000,000 add & has</td><td>167.85</td><td>5.96</td><td>0.01</td></tr><tr><td>1,000,000 ObjKey set & get</td><td>308.73</td><td>3.24</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set & get</td><td>300.60</td><td>3.33</td><td>0.03</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add & has</td><td>270.49</td><td>3.70</td><td>0.04</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<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>12.34</td><td>81.06</td><td>1.27e-4</td></tr><tr><td>1,000,000 push & pop</td><td>21.35</td><td>46.84</td><td>1.44e-4</td></tr><tr><td>1,000,000 push & shift</td><td>21.54</td><td>46.42</td><td>0.00</td></tr><tr><td>100,000 push & shift</td><td>2.09</td><td>478.80</td><td>1.60e-4</td></tr><tr><td>Native JS Array 100,000 push & shift</td><td>2233.46</td><td>0.45</td><td>0.12</td></tr><tr><td>100,000 unshift & shift</td><td>1.99</td><td>502.42</td><td>1.55e-4</td></tr><tr><td>Native JS Array 100,000 unshift & shift</td><td>4153.08</td><td>0.24</td><td>0.34</td></tr></table></div>
</div><div id="json-to-html"><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;"><tbody><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>129.84</td><td>7.70</td><td>0.04</td></tr><tr><td>Native JS Map 1,000,000 set</td><td>238.66</td><td>4.19</td><td>0.03</td></tr><tr><td>Native JS Set 1,000,000 add</td><td>194.68</td><td>5.14</td><td>0.01</td></tr><tr><td>1,000,000 set &amp; get</td><td>134.95</td><td>7.41</td><td>0.03</td></tr><tr><td>Native JS Map 1,000,000 set &amp; get</td><td>320.94</td><td>3.12</td><td>0.04</td></tr><tr><td>Native JS Set 1,000,000 add &amp; has</td><td>261.56</td><td>3.82</td><td>0.07</td></tr><tr><td>1,000,000 ObjKey set &amp; get</td><td>363.11</td><td>2.75</td><td>0.06</td></tr><tr><td>Native JS Map 1,000,000 ObjKey set &amp; get</td><td>344.03</td><td>2.91</td><td>0.05</td></tr><tr><td>Native JS Set 1,000,000 ObjKey add &amp; has</td><td>351.35</td><td>2.85</td><td>0.10</td></tr></tbody></table></div>
</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>45.79</td><td>21.84</td><td>7.32e-4</td></tr><tr><td>100,000 getWords</td><td>87.85</td><td>11.38</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>44.75</td><td>22.35</td><td>6.31e-4</td></tr><tr><td>100,000 getWords</td><td>83.96</td><td>11.91</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>260.78</td><td>3.83</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>306.61</td><td>3.26</td><td>0.00</td></tr><tr><td>100,000 get</td><td>140.27</td><td>7.13</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>29.90</td><td>33.45</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>428.76</td><td>2.33</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>580.74</td><td>1.72</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add</td><td>262.23</td><td>3.81</td><td>0.00</td></tr><tr><td>100,000 add randomly</td><td>323.18</td><td>3.09</td><td>0.00</td></tr><tr><td>100,000 get</td><td>130.85</td><td>7.64</td><td>0.00</td></tr><tr><td>100,000 iterator</td><td>31.79</td><td>31.46</td><td>0.01</td></tr><tr><td>100,000 add & delete orderly</td><td>423.80</td><td>2.36</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>585.79</td><td>1.71</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</td><td>5.74</td><td>174.10</td><td>9.29e-5</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>18.83</td><td>53.10</td><td>1.49e-4</td></tr><tr><td>10,000 RBTree get</td><td>0.77</td><td>1290.55</td><td>7.33e-6</td></tr><tr><td>10,000 AVLTree add</td><td>22.60</td><td>44.25</td><td>2.14e-4</td></tr><tr><td>10,000 AVLTree get</td><td>10.63</td><td>94.08</td><td>1.02e-4</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>44.17</td><td>22.64</td><td>3.52e-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>5.84</td><td>171.13</td><td>8.92e-5</td></tr><tr><td>10,000 RBTree get randomly</td><td>9.48</td><td>105.49</td><td>5.40e-4</td></tr><tr><td>10,000 RBTree add & delete randomly</td><td>18.19</td><td>54.98</td><td>1.42e-4</td></tr><tr><td>10,000 AVLTree add randomly</td><td>26.45</td><td>37.80</td><td>2.66e-4</td></tr><tr><td>10,000 AVLTree get randomly</td><td>10.63</td><td>94.09</td><td>1.36e-4</td></tr><tr><td>10,000 AVLTree add & delete randomly</td><td>49.07</td><td>20.38</td><td>3.49e-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.11</td><td>9501.69</td><td>1.02e-6</td></tr><tr><td>1,000 addEdge</td><td>6.18</td><td>161.81</td><td>4.27e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.16e+4</td><td>3.23e-7</td></tr><tr><td>1,000 getEdge</td><td>23.31</td><td>42.90</td><td>0.00</td></tr><tr><td>tarjan</td><td>206.06</td><td>4.85</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>181.65</td><td>5.51</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.10</td><td>1.01e+4</td><td>1.05e-6</td></tr><tr><td>1,000 addEdge</td><td>6.18</td><td>161.88</td><td>7.72e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.17e+4</td><td>3.58e-7</td></tr><tr><td>1,000 getEdge</td><td>23.03</td><td>43.43</td><td>0.00</td></tr><tr><td>tarjan</td><td>203.18</td><td>4.92</td><td>0.02</td></tr><tr><td>topologicalSort</td><td>174.78</td><td>5.72</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>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>207.88</td><td>4.81</td><td>0.04</td></tr><tr><td>1,000,000 unshift</td><td>214.33</td><td>4.67</td><td>0.06</td></tr><tr><td>1,000,000 unshift & shift</td><td>185.54</td><td>5.39</td><td>0.04</td></tr><tr><td>1,000,000 addBefore</td><td>308.66</td><td>3.24</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>227.39</td><td>4.40</td><td>0.03</td></tr><tr><td>1,000,000 unshift</td><td>211.70</td><td>4.72</td><td>0.02</td></tr><tr><td>1,000,000 unshift & shift</td><td>171.84</td><td>5.82</td><td>0.02</td></tr><tr><td>1,000,000 addBefore</td><td>318.69</td><td>3.14</td><td>0.06</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>singly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push & shift</td><td>202.61</td><td>4.94</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>219.69</td><td>4.55</td><td>0.02</td></tr><tr><td>10,000 addBefore</td><td>247.13</td><td>4.05</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push & shift</td><td>201.65</td><td>4.96</td><td>0.04</td></tr><tr><td>10,000 push & pop</td><td>216.76</td><td>4.61</td><td>0.01</td></tr><tr><td>10,000 addBefore</td><td>248.82</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>27.36</td><td>36.55</td><td>9.92e-4</td></tr><tr><td>100,000 add & poll</td><td>146.72</td><td>6.82</td><td>6.84e-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>27.35</td><td>36.56</td><td>0.00</td></tr><tr><td>100,000 add & poll</td><td>78.76</td><td>12.70</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'>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>39.36</td><td>25.41</td><td>0.01</td></tr><tr><td>1,000,000 push & pop</td><td>47.86</td><td>20.89</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>38.79</td><td>25.78</td><td>0.00</td></tr><tr><td>1,000,000 push & pop</td><td>47.01</td><td>21.27</td><td>0.01</td></tr></table></div>
</div>
[//]: # (No deletion!!! End of Replace Section)

52
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "data-structure-typed",
"version": "1.51.5",
"version": "1.51.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "data-structure-typed",
"version": "1.51.5",
"version": "1.51.6",
"license": "MIT",
"devDependencies": {
"@swc/core": "^1.3.96",
@ -16,11 +16,11 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"auto-changelog": "^2.4.0",
"avl-tree-typed": "^1.51.4",
"avl-tree-typed": "^1.51.5",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.51.4",
"bst-typed": "^1.51.4",
"data-structure-typed": "^1.51.4",
"binary-tree-typed": "^1.51.5",
"bst-typed": "^1.51.5",
"data-structure-typed": "^1.51.5",
"dependency-cruiser": "^14.1.0",
"doctoc": "^2.2.1",
"eslint": "^8.50.0",
@ -29,7 +29,7 @@
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"fast-glob": "^3.3.1",
"heap-typed": "^1.51.4",
"heap-typed": "^1.51.5",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.7.0",
"js-sdsl": "^4.4.2",
@ -3097,12 +3097,12 @@
}
},
"node_modules/avl-tree-typed": {
"version": "1.51.4",
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.51.4.tgz",
"integrity": "sha512-9kK90VK8FHzawVDf4p55AWEnC2iprGWD9UVBGUg333K2vf75oGkzfVKL0lMV/BsSprTYf98Sreo4KQ03VNZL4Q==",
"version": "1.51.5",
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.51.5.tgz",
"integrity": "sha512-cw/xCqnyRDh3lh11YkMcZDFV1rQoozfhKOGkkeO+xT7aHu8a1jtK9zJrrqCqIfOYKTieTsirwvngBloQb6PH/g==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.51.4"
"data-structure-typed": "^1.51.5"
}
},
"node_modules/babel-jest": {
@ -3306,12 +3306,12 @@
}
},
"node_modules/binary-tree-typed": {
"version": "1.51.4",
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.51.4.tgz",
"integrity": "sha512-iAYoTj36MUNMjHPT4mGjQ4ggh+3YOY4f25EM45yYz1SLehXxeYGrGnoLu4L0r3UgNp3KkvbMp7BvmML2hTSDgA==",
"version": "1.51.5",
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.51.5.tgz",
"integrity": "sha512-a34NqnG4KNUNwcRHQC7vjYpqQYkKy9QaWZx7uHiRcbHQjU/8EA9ICl4U3s3X8V3Ur+CclOsBYNPUdeAd38SH9A==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.51.4"
"data-structure-typed": "^1.51.5"
}
},
"node_modules/brace-expansion": {
@ -3390,12 +3390,12 @@
}
},
"node_modules/bst-typed": {
"version": "1.51.4",
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.51.4.tgz",
"integrity": "sha512-HDt+d7fMrnLa7FYBZH7FCWZQF+t5VA77L0QUgHI/m6lr/opGUmNSxqsEk4VsYwc4JskNi70F1HxApl26MLxAhQ==",
"version": "1.51.5",
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.51.5.tgz",
"integrity": "sha512-HmpA3D4WmPBcWIbJMSTJ2wLPwwfl9ulNXJyuSIYGFLZoTa9+g1oA4HRL43IUnBCLgVWSXsq7Bhc/Jp1jvN/84w==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.51.4"
"data-structure-typed": "^1.51.5"
}
},
"node_modules/buffer-from": {
@ -3838,9 +3838,9 @@
}
},
"node_modules/data-structure-typed": {
"version": "1.51.4",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.51.4.tgz",
"integrity": "sha512-nVMJaCwf9m+2MqtGmPfc3LNda8Jhek2gDQ2D1tam/rous+k++geROPRDfWhYzBYXU24ndLeYP/NepB5Po6qsMQ==",
"version": "1.51.5",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.51.5.tgz",
"integrity": "sha512-uVONyHrk2UHXDHkRDm7EnXO9TNybAU1Rmd/9r1Ld5vaFPa/oIXy9yzFk6deTDgsnbiHASnogA3IWiqhCag1RZQ==",
"dev": true
},
"node_modules/debug": {
@ -5363,12 +5363,12 @@
}
},
"node_modules/heap-typed": {
"version": "1.51.4",
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.51.4.tgz",
"integrity": "sha512-82U5jW7U87VefYCZ/Wmq6jfN6fPKT5IdThJ5VSBMNyMNGSP60UiK0DSftoGif2kQWsI/2ay7fmm7sFIjkQy4rQ==",
"version": "1.51.5",
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.51.5.tgz",
"integrity": "sha512-mCD9etfviFYdbDEkXJWTyKVX7f2LPRCQHHLjo+VOsE+IBJRTScuNIJKSD+HKZOntNBCWW0VWdc+ZzkXEKU2mhw==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.51.4"
"data-structure-typed": "^1.51.5"
}
},
"node_modules/html-escaper": {

View file

@ -66,11 +66,11 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"auto-changelog": "^2.4.0",
"avl-tree-typed": "^1.51.4",
"avl-tree-typed": "^1.51.5",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.51.4",
"bst-typed": "^1.51.4",
"data-structure-typed": "^1.51.4",
"binary-tree-typed": "^1.51.5",
"bst-typed": "^1.51.5",
"data-structure-typed": "^1.51.5",
"dependency-cruiser": "^14.1.0",
"doctoc": "^2.2.1",
"eslint": "^8.50.0",
@ -79,7 +79,7 @@
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"fast-glob": "^3.3.1",
"heap-typed": "^1.51.4",
"heap-typed": "^1.51.5",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.7.0",
"js-sdsl": "^4.4.2",

View file

@ -278,17 +278,19 @@ export class BinaryTree<
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
iterationType: IterationType = 'ITERATIVE'
): NODE | null | undefined {
if (keyOrNodeOrEntry === this.NIL) return;
if (this.isRealNode(keyOrNodeOrEntry)) {
return keyOrNodeOrEntry;
} else if (this.isEntry(keyOrNodeOrEntry)) {
if (keyOrNodeOrEntry[0] === null) return null;
if (keyOrNodeOrEntry[0] === undefined) return;
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
} else {
if (keyOrNodeOrEntry === null) return null;
if (keyOrNodeOrEntry === undefined) return;
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
}
if (this.isEntry(keyOrNodeOrEntry)) {
const key = keyOrNodeOrEntry[0];
if (key === null) return null;
if (key === undefined) return;
return this.getNodeByKey(key, iterationType);
}
if (keyOrNodeOrEntry === null) return null;
if (keyOrNodeOrEntry === undefined) return;
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
}
/**
@ -722,29 +724,8 @@ export class BinaryTree<
* @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`.
*/
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
if (!this.root) return undefined;
if (iterationType === 'RECURSIVE') {
const dfs = (cur: NODE): NODE | undefined => {
if (cur.key === key) return cur;
if (!cur.left && !cur.right) return;
if (cur.left) return dfs(cur.left);
if (cur.right) return dfs(cur.right);
};
return dfs(this.root);
} else {
const stack = [this.root];
while (stack.length > 0) {
const cur = stack.pop();
if (cur) {
if (cur.key === key) return cur;
cur.left && stack.push(cur.left);
cur.right && stack.push(cur.right);
}
}
}
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
}
override get<C extends BTNCallback<NODE, K>>(
@ -801,7 +782,7 @@ export class BinaryTree<
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): V | undefined {
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
}
override has<C extends BTNCallback<NODE, K>>(

View file

@ -214,15 +214,20 @@ export class BST<
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
iterationType: IterationType = 'ITERATIVE'
): NODE | undefined {
if (keyOrNodeOrEntry === this.NIL) return;
if (this.isRealNode(keyOrNodeOrEntry)) {
return keyOrNodeOrEntry;
} else if (this.isEntry(keyOrNodeOrEntry)) {
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined) return;
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
} else {
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) return;
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
}
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);
}
/**
@ -406,51 +411,6 @@ export class BST<
return inserted;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* 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`.
*/
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
if (!this.isRealNode(this.root)) return;
if (iterationType === 'RECURSIVE') {
const dfs = (cur: NODE): NODE | undefined => {
if (cur.key === key) return cur;
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') return dfs(cur.left);
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') return dfs(cur.right);
};
return dfs(this.root);
} else {
const stack = [this.root];
while (stack.length > 0) {
const cur = stack.pop();
if (this.isRealNode(cur)) {
if (this._compare(cur.key, key) === 'EQ') return cur;
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') stack.push(cur.left);
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') stack.push(cur.right);
}
}
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(k + log n)
@ -513,29 +473,27 @@ export class BST<
} else {
const stack = [beginRoot];
while (stack.length > 0) {
const cur = stack.pop();
if (this.isRealNode(cur)) {
const callbackResult = callback(cur);
if (callbackResult === identifier) {
ans.push(cur);
if (onlyOne) return ans;
}
// TODO potential bug
if (callback === this._DEFAULT_CALLBACK) {
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right);
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left);
const cur = stack.pop()!;
const callbackResult = callback(cur);
if (callbackResult === identifier) {
ans.push(cur);
if (onlyOne) return ans;
}
// TODO potential bug
if (callback === this._DEFAULT_CALLBACK) {
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right);
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left);
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
// // @ts-ignore
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
// // @ts-ignore
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
} else {
this.isRealNode(cur.right) && stack.push(cur.right);
this.isRealNode(cur.left) && stack.push(cur.left);
}
// // @ts-ignore
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
// // @ts-ignore
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
} else {
this.isRealNode(cur.right) && stack.push(cur.right);
this.isRealNode(cur.left) && stack.push(cur.left);
}
}
}
@ -578,6 +536,29 @@ export class BST<
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* 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`.
*/
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)
@ -920,7 +901,9 @@ export class BST<
const extractedB = this.extractor(b);
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
if (compared > 0) return 'GT';
if (compared < 0) return 'LT';
return 'EQ';
}
/**
@ -935,10 +918,7 @@ export class BST<
protected _lt(a: K, b: K): boolean {
const extractedA = this.extractor(a);
const extractedB = this.extractor(b);
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
// return extractedA < extractedB;
// return a < b;
}
/**
@ -952,9 +932,6 @@ export class BST<
protected _gt(a: K, b: K): boolean {
const extractedA = this.extractor(a);
const extractedB = this.extractor(b);
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
// return extractedA > extractedB;
// return a > b;
}
}

View file

@ -6,7 +6,7 @@ import type {
RedBlackTreeNested,
RedBlackTreeNodeNested
} from '../../types';
import { CRUD, RBTNColor } from '../../types';
import { CP, CRUD, RBTNColor } from '../../types';
import { BST, BSTNode } from './bst';
import { IBinaryTree } from '../../interfaces';
@ -656,4 +656,21 @@ export class RedBlackTree<
x.right = y;
y.parent = x;
}
/**
* The function compares two values using a comparator function and returns whether the first value
* is greater than, less than, or equal to the second value.
* @param {K} a - The parameter "a" is of type K.
* @param {K} b - The parameter "b" in the above code represents a K.
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
* than), 'LT' (less than), or 'EQ' (equal).
*/
protected override _compare(a: K, b: K): CP {
const extractedA = this.extractor(a);
const extractedB = this.extractor(b);
const compared = extractedA - extractedB;
if (compared > 0) return 'GT';
if (compared < 0) return 'LT';
return 'EQ';
}
}

View file

@ -6,26 +6,26 @@ const suite = new Benchmark.Suite();
const rbTree = new RedBlackTree();
const avlTree = new AVLTree();
const { TEN_THOUSAND } = magnitude;
const arr = getRandomIntArray(TEN_THOUSAND, 0, TEN_THOUSAND, true);
const arr = getRandomIntArray(TEN_THOUSAND, 0, TEN_THOUSAND - 1, true);
suite
.add(`${TEN_THOUSAND.toLocaleString()} RBTree add`, () => {
.add(`${TEN_THOUSAND.toLocaleString()} RBTree add randomly`, () => {
rbTree.clear();
for (let i = 0; i < arr.length; i++) rbTree.add(arr[i]);
})
.add(`${TEN_THOUSAND.toLocaleString()} RBTree get randomly`, () => {
for (let i = 0; i < arr.length; i++) rbTree.get(arr[i]);
})
.add(`${TEN_THOUSAND.toLocaleString()} RBTree add & delete randomly`, () => {
rbTree.clear();
for (let i = 0; i < arr.length; i++) rbTree.add(arr[i]);
for (let i = 0; i < arr.length; i++) rbTree.delete(arr[i]);
})
.add(`${TEN_THOUSAND.toLocaleString()} RBTree get`, () => {
for (let i = 0; i < arr.length; i++) rbTree.get(arr[i]);
})
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree add`, () => {
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree add randomly`, () => {
avlTree.clear();
for (let i = 0; i < arr.length; i++) avlTree.add(arr[i]);
})
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree get`, () => {
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree get randomly`, () => {
for (let i = 0; i < arr.length; i++) avlTree.get(arr[i]);
})
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree add & delete randomly`, () => {