Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Delegates.TreeTraversal
- {
- public static class Traversal
- {
- private static IEnumerable<TResult> Travers<TData,TResult>(
- TData data,
- Func<TData, IEnumerable<TResult>> getResults,
- Func<TData, IEnumerable<TData>> updateStack)
- {
- var stack = new Stack<TData>();
- stack.Push(data);
- while (stack.Count > 0)
- {
- var temp = stack.Pop();
- foreach (var res in getResults(temp))
- yield return res;
- foreach (var res in updateStack(temp))
- stack.Push(res);
- }
- }
- public static IEnumerable<TParameters> GetBinaryTreeValues<TParameters>(BinaryTree<TParameters> data)
- {
- return Travers(
- data,
- x => new List<TParameters> {x.Value},
- tree => new[] {tree.Left, tree.Right}.Where(x => x != null));
- }
- public static IEnumerable<Job> GetEndJobs(Job data)
- {
- return Travers(
- data,
- x => x.Subjobs.Count == 0 ? new[] {x} : new Job[0],
- tree => tree.Subjobs);
- }
- public static IEnumerable<Product> GetProducts(ProductCategory data)
- {
- return Travers(
- data,
- x => x.Products,
- tree => tree.Categories);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement