|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using DataStructures.Trees; |
| 4 | +using Algorithms.Common; |
| 5 | + |
| 6 | +namespace Algorithms.Trees |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Simple Recursive Tree Traversal and Search Algorithms. |
| 10 | + /// </summary> |
| 11 | + public static class BinaryTreeRecursiveWalker |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Specifies the mode of travelling through the tree. |
| 15 | + /// </summary> |
| 16 | + public enum TraversalMode |
| 17 | + { |
| 18 | + InOrder = 0, |
| 19 | + PreOrder = 1, |
| 20 | + PostOrder = 2 |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + /************************************************************************************ |
| 25 | + * PRIVATE HELPERS SECTION |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Private helper method for Preorder Traversal. |
| 31 | + /// </summary> |
| 32 | + private static void PreOrderVisitor<T>(BSTNode<T> BinaryTreeRoot, Action<T> Action) where T : IComparable<T> |
| 33 | + { |
| 34 | + if (BinaryTreeRoot == null) |
| 35 | + return; |
| 36 | + |
| 37 | + Action(BinaryTreeRoot.Value); |
| 38 | + BinaryTreeRecursiveWalker.PreOrderVisitor<T>(BinaryTreeRoot.LeftChild, Action); |
| 39 | + BinaryTreeRecursiveWalker.PreOrderVisitor<T>(BinaryTreeRoot.RightChild, Action); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Private helper method for Inorder Traversal. |
| 44 | + /// </summary> |
| 45 | + private static void InOrderVisitor<T>(BSTNode<T> BinaryTreeRoot, Action<T> Action) where T : IComparable<T> |
| 46 | + { |
| 47 | + if (BinaryTreeRoot == null) |
| 48 | + return; |
| 49 | + |
| 50 | + BinaryTreeRecursiveWalker.InOrderVisitor<T>(BinaryTreeRoot.LeftChild, Action); |
| 51 | + Action(BinaryTreeRoot.Value); |
| 52 | + BinaryTreeRecursiveWalker.InOrderVisitor<T>(BinaryTreeRoot.RightChild, Action); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Private helper method for Preorder Traversal. |
| 57 | + /// </summary> |
| 58 | + private static void PostOrderVisitor<T>(BSTNode<T> BinaryTreeRoot, Action<T> Action) where T : IComparable<T> |
| 59 | + { |
| 60 | + if (BinaryTreeRoot == null) |
| 61 | + return; |
| 62 | + |
| 63 | + BinaryTreeRecursiveWalker.PostOrderVisitor<T>(BinaryTreeRoot.LeftChild, Action); |
| 64 | + BinaryTreeRecursiveWalker.PostOrderVisitor<T>(BinaryTreeRoot.RightChild, Action); |
| 65 | + Action(BinaryTreeRoot.Value); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Private helper method for Preorder Searcher. |
| 70 | + /// </summary> |
| 71 | + private static bool PreOrderSearcher<T>(BSTNode<T> BinaryTreeRoot, T Value, bool IsBinarySearchTree=false) where T : IComparable<T> |
| 72 | + { |
| 73 | + var current = BinaryTreeRoot.Value; |
| 74 | + var hasLeft = BinaryTreeRoot.HasLeftChild; |
| 75 | + var hasRight = BinaryTreeRoot.HasRightChild; |
| 76 | + |
| 77 | + if (current.IsEqualTo(Value)) |
| 78 | + return true; |
| 79 | + |
| 80 | + if (IsBinarySearchTree == true) |
| 81 | + { |
| 82 | + if (BinaryTreeRoot.HasLeftChild && current.IsGreaterThan(Value)) |
| 83 | + return PreOrderSearcher<T>(BinaryTreeRoot.LeftChild, Value); |
| 84 | + |
| 85 | + else if (BinaryTreeRoot.HasRightChild && current.IsLessThan(Value)) |
| 86 | + return PreOrderSearcher<T>(BinaryTreeRoot.RightChild, Value); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + if (hasLeft && PreOrderSearcher<T>(BinaryTreeRoot.LeftChild, Value) == true) |
| 91 | + return true; |
| 92 | + |
| 93 | + if (hasRight && PreOrderSearcher<T>(BinaryTreeRoot.RightChild, Value) == true) |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Private helper method for Inorder Searcher. |
| 102 | + /// </summary> |
| 103 | + private static bool InOrderSearcher<T>(BSTNode<T> BinaryTreeRoot, T Value, bool IsBinarySearchTree=false) where T : IComparable<T> |
| 104 | + { |
| 105 | + var current = BinaryTreeRoot.Value; |
| 106 | + var hasLeft = BinaryTreeRoot.HasLeftChild; |
| 107 | + var hasRight = BinaryTreeRoot.HasRightChild; |
| 108 | + |
| 109 | + if (IsBinarySearchTree == true) |
| 110 | + { |
| 111 | + if (hasLeft && current.IsGreaterThan(Value)) |
| 112 | + return BinaryTreeRecursiveWalker.InOrderSearcher<T>(BinaryTreeRoot.LeftChild, Value); |
| 113 | + |
| 114 | + else if (current.IsEqualTo(Value)) |
| 115 | + return true; |
| 116 | + |
| 117 | + else if (hasRight && current.IsLessThan(Value)) |
| 118 | + return BinaryTreeRecursiveWalker.InOrderSearcher<T>(BinaryTreeRoot.RightChild, Value); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + if (hasLeft && InOrderSearcher<T>(BinaryTreeRoot.LeftChild, Value) == true) |
| 123 | + return true; |
| 124 | + |
| 125 | + if (current.IsEqualTo(Value)) |
| 126 | + return true; |
| 127 | + |
| 128 | + if (hasRight && InOrderSearcher<T>(BinaryTreeRoot.RightChild, Value) == true) |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Private helper method for Inorder Searcher. |
| 137 | + /// </summary> |
| 138 | + private static bool PostOrderSearcher<T>(BSTNode<T> BinaryTreeRoot, T Value, bool IsBinarySearchTree=false) where T : IComparable<T> |
| 139 | + { |
| 140 | + var current = BinaryTreeRoot.Value; |
| 141 | + var hasLeft = BinaryTreeRoot.HasLeftChild; |
| 142 | + var hasRight = BinaryTreeRoot.HasRightChild; |
| 143 | + |
| 144 | + if (IsBinarySearchTree == true) |
| 145 | + { |
| 146 | + if (hasLeft && current.IsGreaterThan(Value)) |
| 147 | + return BinaryTreeRecursiveWalker.PostOrderSearcher<T>(BinaryTreeRoot.LeftChild, Value); |
| 148 | + |
| 149 | + else if (hasRight && current.IsLessThan(Value)) |
| 150 | + return BinaryTreeRecursiveWalker.PostOrderSearcher<T>(BinaryTreeRoot.RightChild, Value); |
| 151 | + |
| 152 | + else if (current.IsEqualTo(Value)) |
| 153 | + return true; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + if (hasLeft && PostOrderSearcher<T>(BinaryTreeRoot.LeftChild, Value) == true) |
| 158 | + return true; |
| 159 | + |
| 160 | + if (hasRight && PostOrderSearcher<T>(BinaryTreeRoot.RightChild, Value) == true) |
| 161 | + return true; |
| 162 | + |
| 163 | + if (current.IsEqualTo(Value)) |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + /************************************************************************************ |
| 172 | + * PUBLIC API SECTION |
| 173 | + * |
| 174 | + */ |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Recusrsivley walks the tree and prints the values of all nodes. |
| 178 | + /// By default this method traverses the tree in inorder fashion. |
| 179 | + /// </summary> |
| 180 | + public static void PrintAll<T>(BSTNode<T> BinaryTreeRoot, TraversalMode Mode=TraversalMode.InOrder) where T : IComparable<T> |
| 181 | + { |
| 182 | + if (BinaryTreeRoot == null) |
| 183 | + throw new ArgumentNullException("Tree root cannot be null."); |
| 184 | + |
| 185 | + var printAction = new Action<T>((T nodeValue) => |
| 186 | + System.Console.Write(String.Format("{0} ", nodeValue))); |
| 187 | + |
| 188 | + BinaryTreeRecursiveWalker.ForEach(BinaryTreeRoot, printAction, Mode); |
| 189 | + System.Console.WriteLine(); |
| 190 | + } |
| 191 | + |
| 192 | + /// <summary> |
| 193 | + /// Recursively Visits All nodes in tree applying a given action to all nodes. |
| 194 | + /// By default this method traverses the tree in inorder fashion. |
| 195 | + /// </summary> |
| 196 | + public static void ForEach<T>(BSTNode<T> BinaryTreeRoot, Action<T> Action, TraversalMode Mode=TraversalMode.InOrder) where T : IComparable<T> |
| 197 | + { |
| 198 | + if (BinaryTreeRoot == null) |
| 199 | + throw new ArgumentNullException("Tree root cannot be null."); |
| 200 | + |
| 201 | + if (Action == null) |
| 202 | + throw new ArgumentNullException("Action<T> Action cannot be null."); |
| 203 | + |
| 204 | + // Traverse |
| 205 | + switch (Mode) |
| 206 | + { |
| 207 | + case TraversalMode.PreOrder: |
| 208 | + BinaryTreeRecursiveWalker.PreOrderVisitor(BinaryTreeRoot, Action); |
| 209 | + return; |
| 210 | + case TraversalMode.InOrder: |
| 211 | + BinaryTreeRecursiveWalker.InOrderVisitor(BinaryTreeRoot, Action); |
| 212 | + return; |
| 213 | + case TraversalMode.PostOrder: |
| 214 | + BinaryTreeRecursiveWalker.PostOrderVisitor(BinaryTreeRoot, Action); |
| 215 | + return; |
| 216 | + default: |
| 217 | + BinaryTreeRecursiveWalker.InOrderVisitor(BinaryTreeRoot, Action); |
| 218 | + return; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + /// <summary> |
| 223 | + /// Search the tree for the specified value. |
| 224 | + /// By default this method traverses the tree in inorder fashion. |
| 225 | + /// </summary> |
| 226 | + public static bool Contains<T>(BSTNode<T> BinaryTreeRoot, T Value, TraversalMode Mode=TraversalMode.InOrder) where T : IComparable<T> |
| 227 | + { |
| 228 | + if (BinaryTreeRoot == null) |
| 229 | + throw new ArgumentNullException("Tree root cannot be null."); |
| 230 | + |
| 231 | + // Traverse |
| 232 | + // Traverse |
| 233 | + switch (Mode) |
| 234 | + { |
| 235 | + case TraversalMode.PreOrder: |
| 236 | + return BinaryTreeRecursiveWalker.PreOrderSearcher(BinaryTreeRoot, Value); |
| 237 | + case TraversalMode.InOrder: |
| 238 | + return BinaryTreeRecursiveWalker.InOrderSearcher(BinaryTreeRoot, Value); |
| 239 | + case TraversalMode.PostOrder: |
| 240 | + return BinaryTreeRecursiveWalker.PostOrderSearcher(BinaryTreeRoot, Value); |
| 241 | + default: |
| 242 | + return BinaryTreeRecursiveWalker.InOrderSearcher(BinaryTreeRoot, Value); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + /// <summary> |
| 247 | + /// Search the tree for the specified value. |
| 248 | + /// By default this method traverses the tree in inorder fashion. |
| 249 | + /// </summary> |
| 250 | + public static bool BinarySearch<T>(BSTNode<T> BinaryTreeRoot, T Value, TraversalMode Mode=TraversalMode.InOrder) where T : IComparable<T> |
| 251 | + { |
| 252 | + if (BinaryTreeRoot == null) |
| 253 | + throw new ArgumentNullException("Tree root cannot be null."); |
| 254 | + |
| 255 | + // Traverse |
| 256 | + // Traverse |
| 257 | + switch (Mode) |
| 258 | + { |
| 259 | + case TraversalMode.PreOrder: |
| 260 | + return BinaryTreeRecursiveWalker.PreOrderSearcher(BinaryTreeRoot, Value, IsBinarySearchTree: true); |
| 261 | + case TraversalMode.InOrder: |
| 262 | + return BinaryTreeRecursiveWalker.InOrderSearcher(BinaryTreeRoot, Value, IsBinarySearchTree: true); |
| 263 | + case TraversalMode.PostOrder: |
| 264 | + return BinaryTreeRecursiveWalker.PostOrderSearcher(BinaryTreeRoot, Value, IsBinarySearchTree:true); |
| 265 | + default: |
| 266 | + return BinaryTreeRecursiveWalker.InOrderSearcher(BinaryTreeRoot, Value, IsBinarySearchTree: true); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + /// <summary> |
| 271 | + /// Search the tree for all matches for a given predicate function. |
| 272 | + /// By default this method traverses the tree in inorder fashion. |
| 273 | + /// </summary> |
| 274 | + public static List<T> FindAllMatches<T>(BSTNode<T> BinaryTreeRoot, Predicate<T> Match, TraversalMode Mode=TraversalMode.InOrder) where T : IComparable<T> |
| 275 | + { |
| 276 | + throw new NotImplementedException(); |
| 277 | + } |
| 278 | + } |
| 279 | +} |
| 280 | + |
0 commit comments