private static void ValidatePosition(int[,] fillTable, int dim, CNode pNodo, List <PositionEntity> tree, CTree arbol, int i, int j, List <PositionEntity> position) { foreach (PositionEntity item in position) { if (pNodo.ValueTree > fillTable[pNodo.XPos + item.XPosition, pNodo.YPos + item.YPosition]) { CNode nodo = arbol.Insert(fillTable[i + item.XPosition + pNodo.XPos, j + item.YPosition + pNodo.YPos], pNodo); nodo.XPos = i + item.XPosition + pNodo.XPos; nodo.YPos = j + item.YPosition + pNodo.YPos; FindCloseNumbers(fillTable, dim, nodo, tree); } } }
private static void FindCloseNumbers(int[,] fillTable, int dim, CNode pNode, List <PositionEntity> position) { CTree tree = new CTree(); PositionEntity positionEntity; List <CTree> treesProperty = new List <CTree>(); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { if (pNode == null) { positionEntity = new PositionEntity(); //Inserta La Raiz CNode root = tree.Insert(fillTable[i, j], null); root.XPos = i; root.YPos = j; positionEntity.Height = positionEntity.Height++; //Tiene que llamar a la misma funcion FindCloseNumbers(fillTable, dim, root, position); CTree treeProperty = new CTree(); treeProperty.Height = tree.FindMaxLevelTree(root); treeProperty.Steeper = tree.FindSteeperTree(root); treeProperty.Root = root; treesProperty.Add(treeProperty); Console.Write("Tree: "); Console.Write("\r\n"); tree.TransversaPreO(root); } else { if (pNode.XPos == 0 && pNode.YPos == 0) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.East, XPosition = 0, YPosition = 1, Value = fillTable[i + pNode.XPos, j + 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.South, XPosition = 1, YPosition = 0, Value = fillTable[i + pNode.XPos + 1, j + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } //Valida los elementos que estan en la fila 0 else if (pNode.XPos == 0 && pNode.YPos > 0 && pNode.YPos < dim - 1) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.West, XPosition = 0, YPosition = -1, Value = fillTable[i + pNode.XPos, j - 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.East, XPosition = 0, YPosition = 1, Value = fillTable[i + pNode.XPos, j + 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.South, XPosition = 1, YPosition = 0, Value = fillTable[i + pNode.XPos + 1, j + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } //Valida los elemento que estan en la columna 0 else if (pNode.XPos > 0 && pNode.XPos < dim - 1 && pNode.YPos == 0) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.North, XPosition = -1, YPosition = 0, Value = fillTable[i + pNode.XPos - 1, j + pNode.YPos] }, new PositionEntity { PositionName = Position.East, XPosition = 0, YPosition = 1, Value = fillTable[i + pNode.XPos, j + 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.South, XPosition = 1, YPosition = 0, Value = fillTable[i + pNode.XPos + 1, j + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } //Valida que el numero no este en la fila 0 ni en la columna 0 else if (pNode.XPos > 0 && pNode.YPos > 0 && pNode.XPos < dim - 1 && pNode.YPos < dim - 1) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.North, XPosition = -1, YPosition = 0, Value = fillTable[i + pNode.XPos - 1, j + pNode.YPos] }, new PositionEntity { PositionName = Position.East, XPosition = 0, YPosition = 1, Value = fillTable[i + pNode.XPos, j + 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.West, XPosition = 0, YPosition = -1, Value = fillTable[i + pNode.XPos, j - 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.South, XPosition = 1, YPosition = 0, Value = fillTable[i + pNode.XPos + 1, j + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } //Valida el numero en la posicion 0, dim-1 else if (pNode.XPos == 0 && pNode.YPos == dim - 1) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.West, XPosition = 0, YPosition = -1, Value = fillTable[i + pNode.XPos, j - 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.South, XPosition = 1, YPosition = 0, Value = fillTable[i + pNode.XPos + 1, j + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } //Valida los numeros que estan en la columna dim-1 y las filas > 0 else if (pNode.XPos > 0 && pNode.XPos < dim - 1 && pNode.YPos == dim - 1) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.North, XPosition = -1, YPosition = 0, Value = fillTable[i + pNode.XPos - 1, j + pNode.YPos] }, new PositionEntity { PositionName = Position.West, XPosition = 0, YPosition = -1, Value = fillTable[i + pNode.XPos, j - 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.South, XPosition = 1, YPosition = 0, Value = fillTable[i + pNode.XPos + 1, j + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } else if (pNode.XPos == dim - 1 && pNode.YPos == 0) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.North, XPosition = -1, YPosition = 0, Value = fillTable[i + pNode.XPos - 1, j + pNode.YPos] }, new PositionEntity { PositionName = Position.East, XPosition = 0, YPosition = 1, Value = fillTable[i + pNode.XPos, j + 1 + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } else if (pNode.YPos > 0 && pNode.YPos < dim - 1 && pNode.XPos == dim - 1) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.North, XPosition = -1, YPosition = 0, Value = fillTable[i + pNode.XPos - 1, j + pNode.YPos] }, new PositionEntity { PositionName = Position.East, XPosition = 0, YPosition = 1, Value = fillTable[i + pNode.XPos, j + 1 + pNode.YPos] }, new PositionEntity { PositionName = Position.West, XPosition = 0, YPosition = -1, Value = fillTable[i + pNode.XPos, j - 1 + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } else if (pNode.XPos == dim - 1 && pNode.YPos == dim - 1) { List <PositionEntity> positions = new List <PositionEntity> { new PositionEntity { PositionName = Position.North, XPosition = -1, YPosition = 0, Value = fillTable[i + pNode.XPos - 1, j + pNode.YPos] }, new PositionEntity { PositionName = Position.West, XPosition = 0, YPosition = -1, Value = fillTable[i + pNode.XPos, j - 1 + pNode.YPos] } }; positions = positions.OrderByDescending(x => x.Value).ToList(); ValidatePosition(fillTable, dim, pNode, position, tree, i, j, positions); return; } } } } treesProperty.Sort(delegate(CTree a, CTree b) { int xdiff = a.Height.CompareTo(b.Height); if (xdiff != 0) { return(xdiff); } else { return(Convert.ToInt32(a.Steeper).CompareTo(b.Steeper)); } }); tree.SelectBestWay(treesProperty.Select(x => x.Root).ToList()[treesProperty.Count - 1]); Console.WriteLine("\r\n----------------------------LONGEST TREE-------------------------------\r\n"); tree.TransversaPreO(treesProperty.Select(x => x.Root).ToList()[treesProperty.Count - 1]); Console.WriteLine("The Size of the tree longest Path: {0}", treesProperty.Select(x => x.Height).ToList()[treesProperty.Count - 1]); Console.WriteLine("The Size of the tree steepest path: {0}", treesProperty.Select(x => x.Steeper).ToList()[treesProperty.Count - 1]); Console.ReadLine(); }