private IntTree BuildTree1to10() { IntTree tree = new IntTree(5); tree.Add(6); tree.Add(9); tree.Add(1); tree.Add(3); tree.Add(4); tree.Add(8); tree.Add(2); tree.Add(10); tree.Add(5); tree.Add(7); return(tree); }
public void DepthFirstRootRight() { IntTree root = new IntTree() { Item = 0 }; IntTree left = new IntTree() { Item = 4 }; IntTree right = new IntTree() { Item = 1 }; left.Add(new IntTree() { Item = 6 }); left.Add(new IntTree() { Item = 5 }); right.Add(new IntTree() { Item = 3 }); right.Add(new IntTree() { Item = 2 }); root.Add(left); root.Add(right); List <int> values = new List <int>(); root.DepthFirst(t => values.Add(t.Item), true, false); Assert.Equal(new int[] { 0, 1, 2, 3, 4, 5, 6 }, values); }
public void BreathFirstLevelsLeafRight() { IntTree root = new IntTree() { Item = 0 }; IntTree left = new IntTree() { Item = 1 }; IntTree right = new IntTree() { Item = 2 }; left.Add(new IntTree() { Item = 3 }); left.Add(new IntTree() { Item = 4 }); right.Add(new IntTree() { Item = 5 }); right.Add(new IntTree() { Item = 6 }); root.Add(left); root.Add(right); List <string> values = new List <string>(); root.BreathFirstLevels(t => values.Add(string.Join("-", t.Select(p => p.Item))), false, false); Assert.Equal(new string[] { "6-5-4-3", "2-1", "0" }, values); }
public void BreathFirstLevelsRootLeft() { IntTree root = new IntTree() { Item = 0 }; IntTree left = new IntTree() { Item = 1 }; IntTree right = new IntTree() { Item = 2 }; left.Add(new IntTree() { Item = 3 }); left.Add(new IntTree() { Item = 4 }); right.Add(new IntTree() { Item = 5 }); right.Add(new IntTree() { Item = 6 }); root.Add(left); root.Add(right); List <string> values = new List <string>(); root.BreathFirstLevels(t => values.Add(string.Join("-", t.Select(p => p.Item)))); Assert.Equal(new string[] { "0", "1-2", "3-4-5-6" }, values); }
public void BreathFirstLeafLeft() { IntTree root = new IntTree() { Item = 6 }; IntTree left = new IntTree() { Item = 4 }; IntTree right = new IntTree() { Item = 5 }; left.Add(new IntTree() { Item = 0 }); left.Add(new IntTree() { Item = 1 }); right.Add(new IntTree() { Item = 2 }); right.Add(new IntTree() { Item = 3 }); root.Add(left); root.Add(right); List <int> values = new List <int>(); root.BreathFirst(t => values.Add(t.Item), false); Assert.Equal(new int[] { 0, 1, 2, 3, 4, 5, 6 }, values); }
public void DepthFirstPathsLeafRight() { IntTree root = new IntTree() { Item = 6 }; IntTree left = new IntTree() { Item = 5 }; IntTree right = new IntTree() { Item = 2 }; left.Add(new IntTree() { Item = 4 }); left.Add(new IntTree() { Item = 3 }); right.Add(new IntTree() { Item = 1 }); right.Add(new IntTree() { Item = 0 }); root.Add(left); root.Add(right); List <string> values = new List <string>(); root.DepthFirstPaths((p, t) => values.Add($"{p.Item}-{t.Item}"), false, false); Assert.Equal(new string[] { "2-0", "2-1", "6-2", "5-3", "5-4", "6-5" }, values); }
public void DepthFirstPathsRootRight() { IntTree root = new IntTree() { Item = 0 }; IntTree left = new IntTree() { Item = 4 }; IntTree right = new IntTree() { Item = 1 }; left.Add(new IntTree() { Item = 6 }); left.Add(new IntTree() { Item = 5 }); right.Add(new IntTree() { Item = 3 }); right.Add(new IntTree() { Item = 2 }); root.Add(left); root.Add(right); List <string> values = new List <string>(); root.DepthFirstPaths((p, t) => values.Add($"{p.Item}-{t.Item}"), true, false); Assert.Equal(new string[] { "0-1", "1-2", "1-3", "0-4", "4-5", "4-6" }, values); }
static void Main(string[] args) { // Instantiate a new IntTree and populate it with Nodes. IntTree tree = new IntTree(); tree.Add(tree.Root, 10); tree.Add(tree.Root, 5); tree.Add(tree.Root, 3); tree.Add(tree.Root, 4); tree.Add(tree.Root, 7); tree.Add(tree.Root, 15); // Convert the IntTree Nodes to an array of int values. int[] TreeArray = tree.PreOrder(tree.Root); // Output the int array to the console. foreach (int value in TreeArray) { Console.Write($"{value}, "); } Console.WriteLine(); // Convert the IntTree to a FizzBuzz StringTree. StringTree st = FizzBuzzTree(tree); // Convert the StringTree Nodes to an array of string values. string[] FizzArray = st.PreOrder(st.Root); // Output the string array to the console. foreach (string value in FizzArray) { Console.Write($"{value}, "); } Console.WriteLine(); }
private IntTree BuildTreeminus50to50() { IntTree tree = new IntTree(0); tree.Add(-30); tree.Add(40); tree.Add(-40); tree.Add(10); tree.Add(40); tree.Add(-50); tree.Add(-10); tree.Add(20); tree.Add(50); tree.Add(-20); tree.Add(30); return(tree); }