Exemple #1
0
        //Transversa preorder
        public void TransversaPreO(CNode pNode)
        {
            if (pNode == null)
            {
                return;
            }

            //Me proceso primero a mi
            for (int n = 0; n < i; n++)
            {
                Console.Write("   ");
            }

            Console.WriteLine(pNode.ValueTree);

            //Luego proceso a mi hijo

            if (pNode.Son != null)
            {
                i++;
                TransversaPreO(pNode.Son);
                i--;
            }
            //Si tengo hermanos los proceso
            if (pNode.Brother != null)
            {
                TransversaPreO(pNode.Brother);
            }
        }
Exemple #2
0
        public CNode Find(int pValue, CNode pNode)
        {
            CNode finded = null;

            if (pNode == null)
            {
                return(finded);
            }
            if (pNode.ValueTree == pValue)
            {
                finded = pNode;
                return(finded);
            }
            //Luego proceso a mi hijo
            if (pNode.Son != null)
            {
                finded = Find(pValue, pNode.Son);

                if (finded != null)
                {
                    return(finded);
                }
            }
            //Si tengo hermanos los proceso
            if (pNode.Brother != null)
            {
                finded = Find(pValue, pNode.Brother);
                if (finded != null)
                {
                    return(finded);
                }
            }
            return(finded);
        }
Exemple #3
0
        public int SelectBestWay(CNode SonNode)
        {
            List <CNode> propertiesTree = FindPropertiesTree(SonNode, new CNode(), new List <CNode>());

            propertiesTree = propertiesTree.OrderBy(x => x.Level).ToList();

            return(propertiesTree.Select(x => Convert.ToInt32(x.ValueTree)).ToList()[0] + 1);
        }
Exemple #4
0
        public int FindMaxLevelTree(CNode SonNode)
        {
            List <CNode> propertiesTree = FindPropertiesTree(SonNode, new CNode(), new List <CNode>());

            propertiesTree = propertiesTree.OrderByDescending(x => x.Level).ToList();

            return(propertiesTree.Select(x => x.Level).ToList()[0] + 1);
        }
Exemple #5
0
 public CNode()
 {
     valueTree = null;
     xPos      = 0;
     yPos      = 0;
     level     = 0;
     son       = null;
     brother   = null;
 }
Exemple #6
0
        public int?FindSteeperTree(CNode SonNode)
        {
            List <CNode> propertiesTree = FindPropertiesTree(SonNode, new CNode(), new List <CNode>());

            propertiesTree = propertiesTree.OrderByDescending(x => x.ValueTree).ToList();
            int?steeperSize = propertiesTree.Select(x => x.ValueTree).ToList()[0] - propertiesTree.Select(x => x.ValueTree).ToList()[propertiesTree.Count - 1];

            return(steeperSize);
        }
Exemple #7
0
 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);
         }
     }
 }
Exemple #8
0
        private List <CNode> FindPropertiesTree(CNode SonNode, CNode porperty, List <CNode> heightList)
        {
            heightList.Add(porperty);
            CNode treeProperties = new CNode();

            if (SonNode == null)
            {
                treeProperties.Level     = porperty.Level;
                treeProperties.ValueTree = SonNode.ValueTree;
                return(heightList);
            }
            else
            {
                if (SonNode.Brother != null)
                {
                    if (SonNode.Son != null)
                    {
                        treeProperties.Level     = porperty.Level + 1;
                        treeProperties.ValueTree = SonNode.ValueTree;
                        FindPropertiesTree(SonNode.Son, treeProperties, heightList);
                        FindPropertiesTree(SonNode.Brother, treeProperties, heightList);
                    }
                    else
                    {
                        treeProperties.Level     = porperty.Level;
                        treeProperties.ValueTree = SonNode.ValueTree;
                        FindPropertiesTree(SonNode.Brother, treeProperties, heightList);
                    }
                }
                else
                {
                    if (SonNode.Son != null)
                    {
                        treeProperties.Level     = porperty.Level + 1;
                        treeProperties.ValueTree = SonNode.ValueTree;
                        FindPropertiesTree(SonNode.Son, treeProperties, heightList);
                    }
                    heightList.Add(SonNode);
                }
            }
            List <CNode> temp = heightList.Where(x => x.ValueTree == null).ToList <CNode>();

            foreach (CNode item in temp)
            {
                heightList.Remove(item);
            }

            return(heightList);
        }
Exemple #9
0
        public CNode Insert(int pValue, CNode pNode)
        {
            //Si no hay nodo donde insertar, tomamos como si fuera en la raiz
            if (pNode == null)
            {
                root           = new CNode();
                root.ValueTree = pValue;

                //No hay hijo
                root.Son = null;

                //No hay hermano
                root.Brother = null;
                return(root);
            }
            //Verificamos si no tiene hijo
            //Insertamos el dato como hijo
            if (pNode.Son == null)
            {
                CNode temp = new CNode();
                temp.ValueTree = pValue;
                //Conectamos el nuevo nodo como hijo
                pNode.Son = temp;

                return(temp);
            }
            else //Ya tiene un hijo, tenemos que insertarlo como hermano
            {
                work = pNode.Son;
                //recorre todos los hermanos hasta que llega al ultimo
                while (work.Brother != null)
                {
                    work = work.Brother;
                }
                //Creamos nodo temporal
                CNode temp = new CNode();
                temp.ValueTree = pValue;
                //Unimos el temporal al ultimo hermano
                work.Brother = temp;

                return(temp);
            }
        }
Exemple #10
0
        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();
        }
Exemple #11
0
 public CTree()
 {
     root = new CNode();
 }