| | 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 ISortedBinaryTree<T> : IDataStructure<T>, |
| | 6 | | DataStructure.IAddable<T>, |
| | 7 | | DataStructure.IRemovable<T>, |
| | 8 | | DataStructure.ICountable, |
| | 9 | | DataStructure.IClearable, |
| | 10 | | DataStructure.IAuditable<T> |
| | 11 | | { |
| | 12 | | #region Properties |
| | 13 | |
|
| | 14 | | /// <summary>Gets the current least value in the tree.</summary> |
| | 15 | | T CurrentLeast { get; } |
| | 16 | | /// <summary>Gets the current greated value in the tree.</summary> |
| | 17 | | T CurrentGreatest { get; } |
| | 18 | |
|
| | 19 | | #endregion |
| | 20 | |
|
| | 21 | | #region Methods |
| | 22 | |
|
| | 23 | | /// <summary>Determines if the tree contains a value.</summary> |
| | 24 | | /// <typeparam name="TSift">The type of the sifting function.</typeparam> |
| | 25 | | /// <param name="sift">The sifting function that respects the sorting of the tree.</param> |
| | 26 | | /// <returns>True if the value is in the tree or false if not.</returns> |
| | 27 | | bool ContainsSift<TSift>(TSift sift = default) |
| | 28 | | where TSift : struct, IFunc<T, CompareResult>; |
| | 29 | |
|
| | 30 | | /// <summary>Tries to get a value.</summary> |
| | 31 | | /// <typeparam name="TSift">The type of the sifting function.</typeparam> |
| | 32 | | /// <param name="sift">The sifting function that respects the sorting of the tree.</param> |
| | 33 | | /// <returns>True if the value was found or false if not.</returns> |
| | 34 | | (bool Success, T? Value, Exception? Exception) TryGet<TSift>(TSift sift = default) |
| | 35 | | where TSift : struct, IFunc<T, CompareResult>; |
| | 36 | |
|
| | 37 | | /// <summary>Tries to remove a value.</summary> |
| | 38 | | /// <typeparam name="TSift">The type of the sifting function.</typeparam> |
| | 39 | | /// <param name="sift">The sifting function that respects the sorting of the tree.</param> |
| | 40 | | /// <returns>True if the remove succeeded or false if not.</returns> |
| | 41 | | (bool Success, Exception? Exception) TryRemoveSift<TSift>(TSift sift = default) |
| | 42 | | where TSift : struct, IFunc<T, CompareResult>; |
| | 43 | |
|
| | 44 | | /// <summary>Does an optimized step function (left to right) for sorted binary search trees.</summary> |
| | 45 | | /// <typeparam name="TStep">The type of the step function.</typeparam> |
| | 46 | | /// <param name="minimum">The minimum step value.</param> |
| | 47 | | /// <param name="maximum">The maximum step value.</param> |
| | 48 | | /// <param name="step">The step function.</param> |
| | 49 | | /// <returns>The result status of the stepper function.</returns> |
| | 50 | | StepStatus StepperBreak<TStep>(T minimum, T maximum, TStep step = default) |
| | 51 | | where TStep : struct, IFunc<T, StepStatus>; |
| | 52 | |
|
| | 53 | | /// <summary>Does a step function (right to left) for sorted binary search trees.</summary> |
| | 54 | | /// <typeparam name="TStep">The type of the step function.</typeparam> |
| | 55 | | /// <param name="step">The step function.</param> |
| | 56 | | /// <returns>The result status of the stepper function.</returns> |
| | 57 | | StepStatus StepperReverseBreak<TStep>(TStep step = default) |
| | 58 | | where TStep : struct, IFunc<T, StepStatus>; |
| | 59 | |
|
| | 60 | | /// <summary>Does an optimized step function (right to left) for sorted binary search trees.</summary> |
| | 61 | | /// <typeparam name="TStep">The type of the step function.</typeparam> |
| | 62 | | /// <param name="minimum">The minimum step value.</param> |
| | 63 | | /// <param name="maximum">The maximum step value.</param> |
| | 64 | | /// <param name="step">The step function.</param> |
| | 65 | | /// <returns>The result status of the stepper function.</returns> |
| | 66 | | StepStatus StepperReverseBreak<TStep>(T minimum, T maximum, TStep step = default) |
| | 67 | | where TStep : struct, IFunc<T, StepStatus>; |
| | 68 | |
|
| | 69 | | #endregion |
| | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary>A self-sorting binary tree based on the heights of each node.</summary> |
| | 73 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 74 | | /// <typeparam name="TCompare">The type that is comparing <typeparamref name="T"/> values.</typeparam> |
| | 75 | | public interface ISortedBinaryTree<T, TCompare> : ISortedBinaryTree<T>, |
| | 76 | | DataStructure.IComparing<T, TCompare> |
| | 77 | | where TCompare : struct, IFunc<T, T, CompareResult> |
| | 78 | | { |
| | 79 | |
|
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary>Static members for <see cref="ISortedBinaryTree{T}"/> and <see cref="ISortedBinaryTree{T, TCompare}"/>.</su |
| | 83 | | public static class SortedBinaryTree |
| | 84 | | { |
| | 85 | | #region Extension Methods |
| | 86 | |
|
| | 87 | | /// <summary>Determines if the tree contains a value.</summary> |
| | 88 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 89 | | /// <param name="tree">The tree to perfrom the contains check on.</param> |
| | 90 | | /// <param name="sift">The sifting function that respects the sorting of the tree.</param> |
| | 91 | | /// <returns>True if the value is in the tree or false if not.</returns> |
| | 92 | | public static bool ContainsSift<T>(this ISortedBinaryTree<T> tree, Func<T, CompareResult> sift) |
| 0 | 93 | | { |
| 0 | 94 | | if (sift is null) throw new ArgumentNullException(nameof(sift)); |
| 0 | 95 | | return tree.ContainsSift<SFunc<T, CompareResult>>(sift); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary>Tries to get a value.</summary> |
| | 99 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 100 | | /// <param name="tree">The tree to perfrom the TryGet on.</param> |
| | 101 | | /// <param name="sift">The sifting function that respects the sorting of the tree.</param> |
| | 102 | | /// <returns>True if the value was found or false if not.</returns> |
| | 103 | | public static (bool Success, T? Value, Exception? Exception) TryGet<T>(this ISortedBinaryTree<T> tree, Func<T, Compare |
| 0 | 104 | | { |
| 0 | 105 | | if (sift is null) throw new ArgumentNullException(nameof(sift)); |
| 0 | 106 | | return tree.TryGet<SFunc<T, CompareResult>>(sift); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary>Tries to remove a value.</summary> |
| | 110 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 111 | | /// <param name="tree">The tree to perfrom the TryRemoveSift on.</param> |
| | 112 | | /// <param name="sift">The sifting function that respects the sorting of the tree.</param> |
| | 113 | | /// <returns>True if the remove succeeded or false if not.</returns> |
| | 114 | | public static (bool Success, Exception? Exception) TryRemoveSift<T>(this ISortedBinaryTree<T> tree, Func<T, CompareRes |
| 0 | 115 | | { |
| 0 | 116 | | if (sift is null) throw new ArgumentNullException(nameof(sift)); |
| 0 | 117 | | return tree.TryRemoveSift<SFunc<T, CompareResult>>(sift); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary>Does an optimized step function (left to right) for sorted binary search trees.</summary> |
| | 121 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 122 | | /// <param name="tree">The tree to traverse.</param> |
| | 123 | | /// <param name="minimum">The minimum step value.</param> |
| | 124 | | /// <param name="maximum">The maximum step value.</param> |
| | 125 | | /// <param name="step">The step function.</param> |
| | 126 | | public static void Stepper<T>(this ISortedBinaryTree<T> tree, T minimum, T maximum, Action<T> step) |
| 0 | 127 | | { |
| 0 | 128 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| 0 | 129 | | tree.StepperBreak<StepBreakFromAction<T, SAction<T>>>(minimum, maximum, new SAction<T>() { Action = step }); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | /// <summary>Does an optimized step function (left to right) for sorted binary search trees.</summary> |
| | 133 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 134 | | /// <typeparam name="TStep">The type of the step function.</typeparam> |
| | 135 | | /// <param name="tree">The tree to traverse.</param> |
| | 136 | | /// <param name="minimum">The minimum step value.</param> |
| | 137 | | /// <param name="maximum">The maximum step value.</param> |
| | 138 | | /// <param name="step">The step function.</param> |
| | 139 | | public static void Stepper<T, TStep>(this ISortedBinaryTree<T> tree, T minimum, T maximum, TStep step = default) |
| | 140 | | where TStep : struct, IAction<T> => |
| 0 | 141 | | tree.StepperBreak<StepBreakFromAction<T, TStep>>(minimum, maximum, step); |
| | 142 | |
|
| | 143 | | /// <summary>Does an optimized step function (left to right) for sorted binary search trees.</summary> |
| | 144 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 145 | | /// <param name="tree">The tree to traverse.</param> |
| | 146 | | /// <param name="minimum">The minimum step value.</param> |
| | 147 | | /// <param name="maximum">The maximum step value.</param> |
| | 148 | | /// <param name="step">The step function.</param> |
| | 149 | | /// <returns>The result status of the stepper function.</returns> |
| | 150 | | public static StepStatus StepperBreak<T>(this ISortedBinaryTree<T> tree, T minimum, T maximum, Func<T, StepStatus> ste |
| 0 | 151 | | { |
| 0 | 152 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| 0 | 153 | | return tree.StepperBreak<SFunc<T, StepStatus>>(minimum, maximum, step); |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | /// <summary>Does an optimized step function (right to left) for sorted binary search trees.</summary> |
| | 157 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 158 | | /// <param name="tree">The tree to traverse.</param> |
| | 159 | | /// <param name="minimum">The minimum step value.</param> |
| | 160 | | /// <param name="maximum">The maximum step value.</param> |
| | 161 | | /// <param name="step">The step function.</param> |
| | 162 | | public static void StepperReverse<T>(this ISortedBinaryTree<T> tree, T minimum, T maximum, Action<T> step) |
| 0 | 163 | | { |
| 0 | 164 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| 0 | 165 | | tree.StepperReverseBreak<StepBreakFromAction<T, SAction<T>>>(minimum, maximum, new SAction<T>() { Action = step }); |
| 0 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary>Does an optimized step function (right to left) for sorted binary search trees.</summary> |
| | 169 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 170 | | /// <typeparam name="TStep">The type of the step function.</typeparam> |
| | 171 | | /// <param name="tree">The tree to traverse.</param> |
| | 172 | | /// <param name="minimum">The minimum step value.</param> |
| | 173 | | /// <param name="maximum">The maximum step value.</param> |
| | 174 | | /// <param name="step">The step function.</param> |
| | 175 | | public static void StepperReverse<T, TStep>(this ISortedBinaryTree<T> tree, T minimum, T maximum, TStep step = default |
| | 176 | | where TStep : struct, IAction<T> => |
| 0 | 177 | | tree.StepperReverseBreak<StepBreakFromAction<T, TStep>>(minimum, maximum, step); |
| | 178 | |
|
| | 179 | | /// <summary>Does an optimized step function (right to left) for sorted binary search trees.</summary> |
| | 180 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 181 | | /// <param name="tree">The tree to traverse.</param> |
| | 182 | | /// <param name="minimum">The minimum step value.</param> |
| | 183 | | /// <param name="maximum">The maximum step value.</param> |
| | 184 | | /// <param name="step">The step function.</param> |
| | 185 | | /// <returns>The result status of the stepper function.</returns> |
| | 186 | | public static StepStatus StepperReverseBreak<T>(this ISortedBinaryTree<T> tree, T minimum, T maximum, Func<T, StepStat |
| 0 | 187 | | { |
| 0 | 188 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| 0 | 189 | | return tree.StepperReverseBreak<SFunc<T, StepStatus>>(minimum, maximum, step); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | #endregion |
| | 193 | | } |