public IBinaryTree FindPath(IBinaryTree tree) { SimpleTreeNode[,] nodes = tree.GetNodes(); int lines = nodes.GetLength(0); _preprocessedNodes = new SimpleTreeNode[lines, lines]; _largestValues = new SimpleTreeNode[lines, lines]; bool topIsEven = nodes[0, 0].Value % 2 == 0; bool lineCountIsEven = lines % 2 == 0; bool currentLineIsEven = topIsEven; for (int i = 0; i < lines; i++) { for (int j = 0; j <= i; j++) { _largestValues[i, j] = new SimpleTreeNode(nodes[i, j]); _preprocessedNodes[i, j] = new SimpleTreeNode() { Value = (nodes[i, j].Value % 2 == 0) == currentLineIsEven ? nodes[i, j].Value : (int?)null }; } currentLineIsEven = !currentLineIsEven; } for (int i = lines - 2; i >= 0; i--) { for (int j = 0; j <= i; j++) { int?left = _preprocessedNodes[i + 1, j].Value; int?rigth = _preprocessedNodes[i + 1, j + 1].Value; if (!left.HasValue) { _largestValues[i, j].Value += _largestValues[i + 1, j + 1].Value; nodes[i, j].PathDirection = 1; } else if (!rigth.HasValue) { _largestValues[i, j].Value += _largestValues[i + 1, j].Value; } else { if (_largestValues[i + 1, j].Value.Value > _largestValues[i + 1, j + 1].Value.Value) { _largestValues[i, j].Value += _largestValues[i + 1, j].Value; } else { _largestValues[i, j].Value += _largestValues[i + 1, j + 1].Value; nodes[i, j].PathDirection = 1; } } } } return(tree); }
public long CalculateSum(IBinaryTree tree) { SimpleTreeNode[,] nodes = tree.GetNodes(); int lines = nodes.GetLength(0); SimpleTreeNode[,] preprocessedNodes = new SimpleTreeNode[lines, lines]; bool topIsEven = nodes[0, 0].Value % 2 == 0; bool lineCountIsEven = lines % 2 == 0; bool currentLineIsEven = topIsEven; for (int i = 0; i < lines; i++) { for (int j = 0; j <= i; j++) { preprocessedNodes[i, j] = new SimpleTreeNode() { Value = (nodes[i, j].Value % 2 == 0) == currentLineIsEven ? nodes[i, j].Value : int.MinValue }; } currentLineIsEven = !currentLineIsEven; } int posSolutions = (int)Math.Pow(2, preprocessedNodes.GetLength(0) - 1); int largestSum = 0; int tempSum, index; for (int i = 0; i <= posSolutions; i++) { tempSum = preprocessedNodes[0, 0].Value.Value; index = 0; for (int j = 0; j < preprocessedNodes.GetLength(0) - 1; j++) { index = index + (i >> j & 1); tempSum += preprocessedNodes[j + 1, index].Value.Value; } if (tempSum > largestSum) { largestSum = tempSum; } } return(largestSum); }
public void Print(IBinaryTree tree) { Console.WriteLine(); SimpleTreeNode[,] nodes = tree.GetNodes(); int lines = nodes.GetLength(0); for (int i = 0; i < lines; i++) { for (int j = 0; j <= i; j++) { Console.Write((nodes[i, j].Value + " ").PadRight(6, ' ')); } Console.WriteLine(); } Console.WriteLine(); }
public void PrintWithPath(IBinaryTree tree) { Console.WriteLine(); SimpleTreeNode[,] nodes = tree.GetNodes(); int lines = nodes.GetLength(0); int pathIndex = 0; int nextPathIndex = 0; int[] pathValues = new int[lines]; for (int i = 0; i < lines; i++) { for (int j = 0; j <= i; j++) { if (pathIndex == j) { Console.ForegroundColor = ConsoleColor.Red; nextPathIndex = pathIndex + nodes[i, j].PathDirection; pathValues[i] = nodes[i, j].Value.Value; } else { Console.ForegroundColor = ConsoleColor.Gray; } Console.Write((nodes[i, j].Value + " ").PadRight(6, ' ')); } Console.WriteLine(); pathIndex = nextPathIndex; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.Write(string.Join(" + ", pathValues)); Console.WriteLine(" = " + pathValues.Sum()); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); }