| | 1 | | namespace Towel.DataStructures; |
| | 2 | |
|
| | 3 | | /// <summary>A self-sorting binary tree based on the heights of each node.</summary> |
| | 4 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 5 | | public interface IAvlTree<T> : ISortedBinaryTree<T> |
| | 6 | | { |
| | 7 | |
|
| | 8 | | } |
| | 9 | |
|
| | 10 | | /// <summary>A self-sorting binary tree based on the heights of each node.</summary> |
| | 11 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 12 | | /// <typeparam name="TCompare">The type that is comparing <typeparamref name="T"/> values.</typeparam> |
| | 13 | | public interface IAvlTree<T, TCompare> : IAvlTree<T>, ISortedBinaryTree<T, TCompare> |
| | 14 | | where TCompare : struct, IFunc<T, T, CompareResult> |
| | 15 | | { |
| | 16 | |
|
| | 17 | | } |
| | 18 | |
|
| | 19 | | /// <summary>Static helpers for <see cref="IAvlTree{T}"/> and <see cref="IAvlTree{T, TCompare}"/>.</summary> |
| | 20 | | public static class AvlTree |
| | 21 | | { |
| | 22 | |
|
| | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary>Static helpers for <see cref="AvlTreeLinked{T, TCompare}"/>.</summary> |
| | 26 | | public static class AvlTreeLinked |
| | 27 | | { |
| | 28 | | #region Extension Methods |
| | 29 | |
|
| | 30 | | /// <summary>Constructs a new <see cref="AvlTreeLinked{T, TCompare}"/>.</summary> |
| | 31 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 32 | | /// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param> |
| | 33 | | /// <returns>The new constructed <see cref="AvlTreeLinked{T, TCompare}"/>.</returns> |
| | 34 | | public static AvlTreeLinked<T, SFunc<T, T, CompareResult>> New<T>( |
| | 35 | | Func<T, T, CompareResult>? compare = null) => |
| | 36 | | new(compare ?? Compare); |
| | 37 | |
|
| | 38 | | #endregion |
| | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary>A self-sorting binary tree based on the heights of each node.</summary> |
| | 42 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 43 | | /// <typeparam name="TCompare">The type that is comparing <typeparamref name="T"/> values.</typeparam> |
| | 44 | | public class AvlTreeLinked<T, TCompare> : IAvlTree<T, TCompare>, |
| | 45 | | ICloneable<AvlTreeLinked<T, TCompare>> |
| | 46 | | where TCompare : struct, IFunc<T, T, CompareResult> |
| | 47 | | { |
| | 48 | | internal Node? _root; |
| | 49 | | internal int _count; |
| | 50 | | internal TCompare _compare; |
| | 51 | |
|
| | 52 | | #region Nested Types |
| | 53 | |
|
| | 54 | | internal class Node |
| | 55 | | { |
| | 56 | | internal T Value; |
| | 57 | | internal Node? LeftChild; |
| | 58 | | internal Node? RightChild; |
| | 59 | | internal int Height; |
| | 60 | |
|
| 1431 | 61 | | internal Node( |
| 1431 | 62 | | T value, |
| 1431 | 63 | | int height = default, |
| 1431 | 64 | | Node? leftChild = null, |
| 1431 | 65 | | Node? rightChild = null) |
| 1431 | 66 | | { |
| 1431 | 67 | | Value = value; |
| 1431 | 68 | | Height = height; |
| 1431 | 69 | | LeftChild = leftChild; |
| 1431 | 70 | | RightChild = rightChild; |
| 1431 | 71 | | } |
| | 72 | | } |
| | 73 | |
|
| | 74 | | #endregion |
| | 75 | |
|
| | 76 | | #region Constructors |
| | 77 | |
|
| | 78 | | /// <summary>Constructs an AVL Tree.</summary> |
| | 79 | | /// <param name="compare">The comparison function for sorting <typeparamref name="T"/> values.</param> |
| 17 | 80 | | public AvlTreeLinked(TCompare compare = default) |
| 17 | 81 | | { |
| 17 | 82 | | _root = null; |
| 17 | 83 | | _count = 0; |
| 17 | 84 | | _compare = compare; |
| 17 | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary>This constructor if for cloning purposes.</summary> |
| | 88 | | /// <param name="tree">The tree to clone.</param> |
| 0 | 89 | | internal AvlTreeLinked(AvlTreeLinked<T, TCompare> tree) |
| 0 | 90 | | { |
| | 91 | | static Node Clone(Node node) => |
| 0 | 92 | | new( |
| 0 | 93 | | value: node.Value, |
| 0 | 94 | | height: node.Height, |
| 0 | 95 | | leftChild: node.LeftChild is null ? null : Clone(node.LeftChild), |
| 0 | 96 | | rightChild: node.RightChild is null ? null : Clone(node.RightChild)); |
| 0 | 97 | | _root = tree._root is null ? null : Clone(tree._root); |
| 0 | 98 | | _count = tree._count; |
| 0 | 99 | | _compare = tree._compare; |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | #endregion |
| | 103 | |
|
| | 104 | | #region Properties |
| | 105 | |
|
| | 106 | | /// <inheritdoc/> |
| | 107 | | public T CurrentLeast |
| | 108 | | { |
| | 109 | | get |
| 0 | 110 | | { |
| 0 | 111 | | if (_root is null) throw new InvalidOperationException("Attempting to get the current least value from an empty AV |
| 0 | 112 | | Node node = _root; |
| 0 | 113 | | while (node.LeftChild is not null) |
| 0 | 114 | | { |
| 0 | 115 | | node = node.LeftChild; |
| 0 | 116 | | } |
| 0 | 117 | | return node.Value; |
| 0 | 118 | | } |
| | 119 | | } |
| | 120 | |
|
| | 121 | | /// <inheritdoc/> |
| | 122 | | public T CurrentGreatest |
| | 123 | | { |
| | 124 | | get |
| 0 | 125 | | { |
| 0 | 126 | | if (_root is null) throw new InvalidOperationException("Attempting to get the current greatest value from an empty |
| 0 | 127 | | Node node = _root; |
| 0 | 128 | | while (node.RightChild is not null) |
| 0 | 129 | | { |
| 0 | 130 | | node = node.RightChild; |
| 0 | 131 | | } |
| 0 | 132 | | return node.Value; |
| 0 | 133 | | } |
| | 134 | | } |
| | 135 | |
|
| | 136 | | /// <inheritdoc/> |
| 3420 | 137 | | public TCompare Compare => _compare; |
| | 138 | |
|
| | 139 | | /// <inheritdoc/> |
| 6016 | 140 | | public int Count => _count; |
| | 141 | |
|
| | 142 | | #endregion |
| | 143 | |
|
| | 144 | | #region Methods |
| | 145 | |
|
| | 146 | | /// <inheritdoc/> |
| | 147 | | public (bool Success, Exception? Exception) TryAdd(T value) |
| 1433 | 148 | | { |
| 1433 | 149 | | Exception? capturedException = null; |
| | 150 | | Node? Add(Node? node) |
| 12740 | 151 | | { |
| 12740 | 152 | | if (node is null) |
| 1431 | 153 | | { |
| 1431 | 154 | | _count++; |
| 1431 | 155 | | return new Node(value: value); |
| | 156 | | } |
| 11309 | 157 | | CompareResult compareResult = _compare.Invoke(node.Value, value); |
| 11309 | 158 | | switch (compareResult) |
| | 159 | | { |
| 22614 | 160 | | case Less: node.RightChild = Add(node.RightChild); break; |
| 0 | 161 | | case Greater: node.LeftChild = Add(node.LeftChild); break; |
| | 162 | | case Equal: |
| 2 | 163 | | capturedException = new ArgumentException($"Adding to add a duplicate value to an {nameof(AvlTreeLinked<T, TCo |
| 2 | 164 | | return node; |
| | 165 | | default: |
| 0 | 166 | | capturedException = compareResult.IsDefined() |
| 0 | 167 | | ? new TowelBugException($"Unhandled {nameof(CompareResult)} value: {compareResult}.") |
| 0 | 168 | | : new ArgumentException($"Invalid {nameof(TCompare)} function; an undefined {nameof(CompareResult)} was retu |
| 0 | 169 | | break; |
| | 170 | | } |
| 11307 | 171 | | return capturedException is null ? Balance(node) : node; |
| 12740 | 172 | | } |
| 1433 | 173 | | _root = Add(_root); |
| 1433 | 174 | | return (capturedException is null, capturedException); |
| 1433 | 175 | | } |
| | 176 | |
|
| | 177 | | /// <inheritdoc/> |
| | 178 | | public void Clear() |
| 0 | 179 | | { |
| 0 | 180 | | _root = null; |
| 0 | 181 | | _count = 0; |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | /// <inheritdoc/> |
| 0 | 185 | | public AvlTreeLinked<T, TCompare> Clone() => new(this); |
| | 186 | |
|
| | 187 | | /// <inheritdoc/> |
| 2212 | 188 | | public bool Contains(T value) => ContainsSift<SiftFromCompareAndValue<T, TCompare>>(new(value, Compare)); |
| | 189 | |
|
| | 190 | | /// <inheritdoc/> |
| | 191 | | public bool ContainsSift<TSift>(TSift sift = default) |
| | 192 | | where TSift : struct, IFunc<T, CompareResult> |
| 2212 | 193 | | { |
| 2212 | 194 | | Node? node = _root; |
| 19380 | 195 | | while (node is not null) |
| 18374 | 196 | | { |
| 18374 | 197 | | CompareResult compareResult = sift.Invoke(node.Value); |
| 18374 | 198 | | switch (compareResult) |
| | 199 | | { |
| 25406 | 200 | | case Less: node = node.RightChild; break; |
| 8930 | 201 | | case Greater: node = node.LeftChild; break; |
| 1206 | 202 | | case Equal: return true; |
| | 203 | | default: |
| 0 | 204 | | throw compareResult.IsDefined() |
| 0 | 205 | | ? new TowelBugException($"Unhandled {nameof(CompareResult)} value: {compareResult}.") |
| 0 | 206 | | : new ArgumentException($"Invalid {nameof(TCompare)} function; an undefined {nameof(CompareResult)} was retu |
| | 207 | | } |
| 17168 | 208 | | } |
| 1006 | 209 | | return false; |
| 2212 | 210 | | } |
| | 211 | |
|
| | 212 | | /// <inheritdoc/> |
| | 213 | | public (bool Success, T? Value, Exception? Exception) TryGet<TSift>(TSift sift = default) |
| | 214 | | where TSift : struct, IFunc<T, CompareResult> |
| 0 | 215 | | { |
| 0 | 216 | | Node? node = _root; |
| 0 | 217 | | while (node is not null) |
| 0 | 218 | | { |
| 0 | 219 | | CompareResult compareResult = sift.Invoke(node.Value); |
| 0 | 220 | | switch (compareResult) |
| | 221 | | { |
| 0 | 222 | | case Less: node = node.LeftChild; break; |
| 0 | 223 | | case Greater: node = node.RightChild; break; |
| 0 | 224 | | case Equal: return (true, node.Value, null); |
| 0 | 225 | | default: return (false, default, new ArgumentException($"Invalid {nameof(TCompare)} function; an undefined {name |
| | 226 | | } |
| 0 | 227 | | } |
| 0 | 228 | | return (false, default, new InvalidOperationException("Attempting to get a non-existing value from an AVL tree.")); |
| 0 | 229 | | } |
| | 230 | |
|
| | 231 | | /// <inheritdoc/> |
| 1208 | 232 | | public (bool Success, Exception? Exception) TryRemove(T value) => TryRemoveSift<SiftFromCompareAndValue<T, TCompare>>( |
| | 233 | |
|
| | 234 | | /// <inheritdoc/> |
| | 235 | | public (bool Success, Exception? Exception) TryRemoveSift<TSift>(TSift sift = default) |
| | 236 | | where TSift : struct, IFunc<T, CompareResult> |
| 1208 | 237 | | { |
| 1208 | 238 | | Exception? exception = null; |
| | 239 | | Node? Remove(Node? node) |
| 8635 | 240 | | { |
| 8635 | 241 | | if (node is not null) |
| 8633 | 242 | | { |
| 8633 | 243 | | CompareResult compareResult = sift.Invoke(node.Value); |
| 8633 | 244 | | switch (compareResult) |
| | 245 | | { |
| 6614 | 246 | | case Less: node.RightChild = Remove(node.RightChild); break; |
| 8240 | 247 | | case Greater: node.LeftChild = Remove(node.LeftChild); break; |
| | 248 | | case Equal: |
| 1206 | 249 | | if (node.RightChild is not null) |
| 589 | 250 | | { |
| 589 | 251 | | node.RightChild = RemoveLeftMost(node.RightChild, out Node leftMostOfRight); |
| 589 | 252 | | leftMostOfRight.RightChild = node.RightChild; |
| 589 | 253 | | leftMostOfRight.LeftChild = node.LeftChild; |
| 589 | 254 | | node = leftMostOfRight; |
| 589 | 255 | | } |
| 617 | 256 | | else if (node.LeftChild is not null) |
| 69 | 257 | | { |
| 69 | 258 | | node.LeftChild = RemoveRightMost(node.LeftChild, out Node rightMostOfLeft); |
| 69 | 259 | | rightMostOfLeft.RightChild = node.RightChild; |
| 69 | 260 | | rightMostOfLeft.LeftChild = node.LeftChild; |
| 69 | 261 | | node = rightMostOfLeft; |
| 69 | 262 | | } |
| | 263 | | else |
| 548 | 264 | | { |
| 548 | 265 | | return null; |
| | 266 | | } |
| 658 | 267 | | break; |
| | 268 | | default: |
| 0 | 269 | | exception = new ArgumentException($"Invalid {nameof(TCompare)} function; an undefined {nameof(CompareResult) |
| 0 | 270 | | return node; |
| | 271 | | } |
| 8085 | 272 | | SetHeight(node); |
| 8085 | 273 | | return Balance(node); |
| | 274 | | } |
| 2 | 275 | | exception = new ArgumentException("Attempting to remove a non-existing entry."); |
| 2 | 276 | | return node; |
| 8635 | 277 | | } |
| 1208 | 278 | | _root = Remove(_root); |
| 1208 | 279 | | if (exception is null) |
| 1206 | 280 | | { |
| 1206 | 281 | | _count--; |
| 1206 | 282 | | } |
| 1208 | 283 | | return (exception is null, exception); |
| 1208 | 284 | | } |
| | 285 | |
|
| | 286 | | /// <inheritdoc/> |
| | 287 | | public StepStatus StepperBreak<TStep>(TStep step = default) |
| | 288 | | where TStep : struct, IFunc<T, StepStatus> |
| 2007 | 289 | | { |
| | 290 | | StepStatus Stepper(Node? node) |
| 2002059 | 291 | | { |
| 2002059 | 292 | | if (node is not null) |
| 1000026 | 293 | | { |
| 1000026 | 294 | | if (Stepper(node.LeftChild) is Break || |
| 1000026 | 295 | | step.Invoke(node.Value) is Break || |
| 1000026 | 296 | | Stepper(node.RightChild) is Break) |
| 0 | 297 | | return Break; |
| 1000026 | 298 | | } |
| 2002059 | 299 | | return Continue; |
| 2002059 | 300 | | } |
| 2007 | 301 | | return Stepper(_root); |
| 2007 | 302 | | } |
| | 303 | |
|
| | 304 | | /// <inheritdoc/> |
| | 305 | | public virtual StepStatus StepperBreak<TStep>(T minimum, T maximum, TStep step = default) |
| | 306 | | where TStep : struct, IFunc<T, StepStatus> |
| 0 | 307 | | { |
| | 308 | | StepStatus Stepper(Node? node) |
| 0 | 309 | | { |
| 0 | 310 | | if (node is not null) |
| 0 | 311 | | { |
| 0 | 312 | | if (_compare.Invoke(node.Value, minimum) is Less) |
| 0 | 313 | | { |
| 0 | 314 | | return Stepper(node.RightChild); |
| | 315 | | } |
| 0 | 316 | | else if (_compare.Invoke(node.Value, maximum) is Greater) |
| 0 | 317 | | { |
| 0 | 318 | | return Stepper(node.LeftChild); |
| | 319 | | } |
| | 320 | | else |
| 0 | 321 | | { |
| 0 | 322 | | if (Stepper(node.LeftChild) is Break || |
| 0 | 323 | | step.Invoke(node.Value) is Break || |
| 0 | 324 | | Stepper(node.RightChild) is Break) |
| 0 | 325 | | return Break; |
| 0 | 326 | | } |
| 0 | 327 | | } |
| 0 | 328 | | return Continue; |
| 0 | 329 | | } |
| 0 | 330 | | if (_compare.Invoke(minimum, maximum) is Greater) |
| 0 | 331 | | { |
| 0 | 332 | | throw new InvalidOperationException("!(" + nameof(minimum) + " <= " + nameof(maximum) + ")"); |
| | 333 | | } |
| 0 | 334 | | return Stepper(_root); |
| 0 | 335 | | } |
| | 336 | |
|
| | 337 | | /// <inheritdoc/> |
| | 338 | | public StepStatus StepperReverseBreak<TStep>(TStep step = default) |
| | 339 | | where TStep : struct, IFunc<T, StepStatus> |
| 0 | 340 | | { |
| | 341 | | StepStatus StepperReverse(Node? node) |
| 0 | 342 | | { |
| 0 | 343 | | if (node is not null) |
| 0 | 344 | | { |
| 0 | 345 | | if (StepperReverse(node.LeftChild) is Break || |
| 0 | 346 | | step.Invoke(node.Value) is Break || |
| 0 | 347 | | StepperReverse(node.RightChild) is Break) |
| 0 | 348 | | return Break; |
| 0 | 349 | | } |
| 0 | 350 | | return Continue; |
| 0 | 351 | | } |
| 0 | 352 | | return StepperReverse(_root); |
| 0 | 353 | | } |
| | 354 | |
|
| | 355 | | /// <inheritdoc/> |
| | 356 | | public virtual StepStatus StepperReverseBreak<TStep>(T minimum, T maximum, TStep step = default) |
| | 357 | | where TStep : struct, IFunc<T, StepStatus> |
| 0 | 358 | | { |
| | 359 | | StepStatus StepperReverse(Node? node) |
| 0 | 360 | | { |
| 0 | 361 | | if (node is not null) |
| 0 | 362 | | { |
| 0 | 363 | | if (_compare.Invoke(node.Value, minimum) is Less) |
| 0 | 364 | | { |
| 0 | 365 | | return StepperReverse(node.RightChild); |
| | 366 | | } |
| 0 | 367 | | else if (_compare.Invoke(node.Value, maximum) is Greater) |
| 0 | 368 | | { |
| 0 | 369 | | return StepperReverse(node.LeftChild); |
| | 370 | | } |
| | 371 | | else |
| 0 | 372 | | { |
| 0 | 373 | | if (StepperReverse(node.LeftChild) is Break || |
| 0 | 374 | | step.Invoke(node.Value) is Break || |
| 0 | 375 | | StepperReverse(node.RightChild) is Break) |
| 0 | 376 | | return Break; |
| 0 | 377 | | } |
| 0 | 378 | | } |
| 0 | 379 | | return Continue; |
| 0 | 380 | | } |
| 0 | 381 | | if (_compare.Invoke(minimum, maximum) is Greater) |
| 0 | 382 | | { |
| 0 | 383 | | throw new InvalidOperationException("!(" + nameof(minimum) + " <= " + nameof(maximum) + ")"); |
| | 384 | | } |
| 0 | 385 | | return StepperReverse(_root); |
| 0 | 386 | | } |
| | 387 | |
|
| 0 | 388 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); |
| | 389 | |
|
| | 390 | | /// <inheritdoc/> |
| | 391 | | public System.Collections.Generic.IEnumerator<T> GetEnumerator() |
| 7 | 392 | | { |
| | 393 | | #warning TODO: optimize using stack |
| 7 | 394 | | IList<T> list = new ListLinked<T>(); |
| 33 | 395 | | this.Stepper(x => list.Add(x)); |
| 7 | 396 | | return list.GetEnumerator(); |
| 7 | 397 | | } |
| | 398 | |
|
| | 399 | | internal static Node Balance(Node node) |
| 19842 | 400 | | { |
| | 401 | | static Node RotateSingleLeft(Node node) |
| 1515 | 402 | | { |
| 1515 | 403 | | Node temp = node.RightChild!; |
| 1515 | 404 | | node.RightChild = temp.LeftChild; |
| 1515 | 405 | | temp.LeftChild = node; |
| 1515 | 406 | | SetHeight(node); |
| 1515 | 407 | | SetHeight(temp); |
| 1515 | 408 | | return temp; |
| 1515 | 409 | | } |
| | 410 | |
|
| | 411 | | static Node RotateDoubleLeft(Node node) |
| 26 | 412 | | { |
| 26 | 413 | | Node temp = node.RightChild!.LeftChild!; |
| 26 | 414 | | node.RightChild.LeftChild = temp.RightChild; |
| 26 | 415 | | temp.RightChild = node.RightChild; |
| 26 | 416 | | node.RightChild = temp.LeftChild; |
| 26 | 417 | | temp.LeftChild = node; |
| 26 | 418 | | SetHeight(temp.LeftChild); |
| 26 | 419 | | SetHeight(temp.RightChild); |
| 26 | 420 | | SetHeight(temp); |
| 26 | 421 | | return temp; |
| 26 | 422 | | } |
| | 423 | |
|
| | 424 | | static Node RotateSingleRight(Node node) |
| 58 | 425 | | { |
| 58 | 426 | | Node temp = node.LeftChild!; |
| 58 | 427 | | node.LeftChild = temp.RightChild; |
| 58 | 428 | | temp.RightChild = node; |
| 58 | 429 | | SetHeight(node); |
| 58 | 430 | | SetHeight(temp); |
| 58 | 431 | | return temp; |
| 58 | 432 | | } |
| | 433 | |
|
| | 434 | | static Node RotateDoubleRight(Node node) |
| 39 | 435 | | { |
| 39 | 436 | | Node temp = node.LeftChild!.RightChild!; |
| 39 | 437 | | node.LeftChild.RightChild = temp.LeftChild; |
| 39 | 438 | | temp.LeftChild = node.LeftChild; |
| 39 | 439 | | node.LeftChild = temp.RightChild; |
| 39 | 440 | | temp.RightChild = node; |
| 39 | 441 | | SetHeight(temp.RightChild); |
| 39 | 442 | | SetHeight(temp.LeftChild); |
| 39 | 443 | | SetHeight(temp); |
| 39 | 444 | | return temp; |
| 39 | 445 | | } |
| | 446 | |
|
| 19842 | 447 | | if (Height(node.LeftChild) == Height(node.RightChild) + 2) |
| 97 | 448 | | { |
| 97 | 449 | | return Height(node.LeftChild!.LeftChild) > Height(node.RightChild) |
| 97 | 450 | | ? RotateSingleRight(node) |
| 97 | 451 | | : RotateDoubleRight(node); |
| | 452 | | } |
| 19745 | 453 | | else if (Height(node.RightChild) == Height(node.LeftChild) + 2) |
| 1541 | 454 | | { |
| 1541 | 455 | | return Height(node.RightChild!.RightChild) > Height(node.LeftChild) |
| 1541 | 456 | | ? RotateSingleLeft(node) |
| 1541 | 457 | | : RotateDoubleLeft(node); |
| | 458 | | } |
| 18204 | 459 | | SetHeight(node); |
| 18204 | 460 | | return node; |
| 19842 | 461 | | } |
| | 462 | |
|
| | 463 | | /// <summary> |
| | 464 | | /// This is just a protection against the null valued leaf nodes, which have a height of "-1".<br/> |
| | 465 | | /// Runtime: O(1) |
| | 466 | | /// </summary> |
| | 467 | | /// <param name="node">The node to find the hight of.</param> |
| | 468 | | /// <returns>Returns "-1" if null (leaf) or the height property of the node.</returns> |
| 142610 | 469 | | internal static int Height(Node? node) => node is null ? -1 : node.Height; |
| | 470 | |
|
| | 471 | | /// <summary>Removes the left-most child of an AVL Tree node and returns it |
| | 472 | | /// through the out parameter.</summary> |
| | 473 | | /// <param name="node">The tree to remove the left-most child from.</param> |
| | 474 | | /// <param name="leftMost">The left-most child of this AVL tree.</param> |
| | 475 | | /// <returns>The updated tree with the removal.</returns> |
| | 476 | | internal static Node RemoveLeftMost(Node node, out Node leftMost) |
| 1039 | 477 | | { |
| 1039 | 478 | | if (node.LeftChild is null) |
| 589 | 479 | | { |
| 589 | 480 | | leftMost = node; |
| 589 | 481 | | return node.RightChild!; |
| | 482 | | } |
| 450 | 483 | | node.LeftChild = RemoveLeftMost(node.LeftChild, out leftMost); |
| 450 | 484 | | SetHeight(node); |
| 450 | 485 | | return Balance(node); |
| 1039 | 486 | | } |
| | 487 | |
|
| | 488 | | /// <summary>Removes the right-most child of an AVL Tree node and returns it |
| | 489 | | /// through the out parameter.</summary> |
| | 490 | | /// <param name="node">The tree to remove the right-most child from.</param> |
| | 491 | | /// <param name="rightMost">The right-most child of this AVL tree.</param> |
| | 492 | | /// <returns>The updated tree with the removal.</returns> |
| | 493 | | internal static Node RemoveRightMost(Node node, out Node rightMost) |
| 69 | 494 | | { |
| 69 | 495 | | if (node.RightChild is null) |
| 69 | 496 | | { |
| 69 | 497 | | rightMost = node; |
| 69 | 498 | | return node.LeftChild!; |
| | 499 | | } |
| 0 | 500 | | node.RightChild = RemoveRightMost(node.RightChild, out rightMost); |
| 0 | 501 | | SetHeight(node); |
| 0 | 502 | | return Balance(node); |
| 69 | 503 | | } |
| | 504 | |
|
| | 505 | | /// <summary> |
| | 506 | | /// Sets the height of a tree based on its children's heights.<br/> |
| | 507 | | /// Runtime: O(1) |
| | 508 | | /// </summary> |
| | 509 | | /// <param name="node">The tree to have its height adjusted.</param> |
| | 510 | | internal static void SetHeight(Node node) => |
| 30080 | 511 | | node.Height = Math.Max(Height(node.LeftChild), Height(node.RightChild)) + 1; |
| | 512 | |
|
| | 513 | | /// <inheritdoc/> |
| | 514 | | public T[] ToArray() |
| 0 | 515 | | { |
| | 516 | | #warning TODO: optimize |
| 0 | 517 | | T[] array = new T[_count]; |
| 0 | 518 | | int i = 0; |
| 0 | 519 | | this.Stepper(x => array[i++] = x); |
| 0 | 520 | | return array; |
| 0 | 521 | | } |
| | 522 | |
|
| | 523 | | #endregion |
| | 524 | | } |