コード例 #1
0
 public void InsertEdge(NodoBusqueda edge)
 {
     for (int x = 0; x < edges.Count; x++)
     {
         if (edges[x].keys[0].CompareTo(edge.keys[0]) > 0)
         {
             edges.Insert(x, edge);
             return;
         }
     }
     edges.Add(edge);
     edge.parent = this;
 }
コード例 #2
0
        public void Insert(string v)
        {
            if (Root == null)
            {
                Root = new NodoBusqueda(v);
                return;
            }
            NodoBusqueda actual = Root;
            NodoBusqueda father = null;

            while (actual != null)
            {
                if (actual.keys.Count == 5)
                {
                    if (father == null)
                    {
                        string         x        = actual.Pop(1);
                        NodoBusqueda   NewRoot  = new NodoBusqueda(x);
                        NodoBusqueda[] newNodos = actual.Split();
                        NewRoot.InsertEdge(newNodos[0]);
                        NewRoot.InsertEdge(newNodos[1]);
                        NewRoot.InsertEdge(newNodos[2]);
                        NewRoot.InsertEdge(newNodos[3]);
                        Root   = NewRoot;
                        actual = NewRoot;
                    }
                    else
                    {
                        string x = actual.Pop(1);
                        if (x != null)
                        {
                            father.Push(x);
                        }
                        NodoBusqueda[] nNodos = actual.Split();
                        int            pos1   = father.FindEdgePosition(nNodos[1].keys[0]);
                        father.InsertEdge(nNodos[1]);

                        int Actualpos = father.FindEdgePosition(v);
                        actual = father.GetEdge(Actualpos);
                    }
                }
                father = actual;
                actual = actual.Traverse(v);
                if (actual == null)
                {
                    father.Push(v);
                }
            }
        }
コード例 #3
0
        public NodoBusqueda Min(NodoBusqueda n = null)
        {
            if (n == null)
            {
                n = Root;
            }

            NodoBusqueda c = n;

            if (c != null)
            {
                while (c.edges.Count > 0)
                {
                    c = c.edges[0];
                }
            }
            return(c);
        }
コード例 #4
0
        public NodoBusqueda Find(string x)
        {
            NodoBusqueda c = Root;

            while (c != null)
            {
                if (c.Key(x) >= 0)
                {
                    return(x);
                }
                else
                {
                    int p = c.FindEdgePosition(x);
                    c = c.GetEdge(p);
                }
            }
            return(null);
        }
コード例 #5
0
        public NodoBusqueda[] Split()
        {
            if (keys.Count != 4)
            {
                throw new InvalidOperationException(string.Format("This node has {0} keys, can only split a 4 keys node", keys.Count));
            }
            NodoBusqueda new_right = new NodoBusqueda(keys[1]);

            for (int x = 4; x < edges.Count; x++)
            {
                new_right.edges.Add(this.edges[x]);
            }
            for (int x = edges.Count - 1; x >= 4; x--)
            {
                this.edges.RemoveAt(x);
            }
            for (int x = 1; x < edges.Count; x++)
            {
                keys.RemoveAt(x);
            }
            return(new NodoBusqueda[] { this, new_right });
        }
コード例 #6
0
        public string[] Inorder(NodoBusqueda n = null)
        {
            if (n == null)
            {
                n = Root;
            }
            int                                a     = 0;
            List <string>                      items = new List <string>();
            Tuple <NodoBusqueda, int>          c     = new Tuple <NodoBusqueda, int>(n, a);
            Stack <Tuple <NodoBusqueda, int> > stack = new Stack <Tuple <NodoBusqueda, int> >();

            while (stack.Count > 0 || c.Item1 != null)
            {
                if (c.Item1 != null)
                {
                    stack.Push(c);
                    NodoBusqueda left_child = c.Item1.GetEdge(c.Item2);
                    c = new Tuple <NodoBusqueda, int>(left_child, a);
                }
                else
                {
                    c = stack.Pop();
                    NodoBusqueda cNodo = c.Item1;

                    if (c.Item2 < cNodo.keys.Count)
                    {
                        items.Add(cNodo.keys[0]);
                        c = new Tuple <NodoBusqueda, int>(cNodo, c.Item2 + 1);
                    }
                    else
                    {
                        NodoBusqueda right_child = cNodo.GetEdge(c.Item2 + 1);
                        c = new Tuple <NodoBusqueda, int>(right_child, c.Item2 + 1);
                    }
                }
            }
            return(items.ToArray());
        }
コード例 #7
0
        public void Fuse(NodoBusqueda n1, NodoBusqueda n2)
        {
            int totalkeys  = n1.keys.Count;
            int totaledges = n1.edges.Count;

            totalkeys  += n2.keys.Count;
            totaledges += n2.edges.Count;
            totalkeys  += this.keys.Count;
            totaledges += this.edges.Count;

            if (totalkeys > 5)
            {
                throw new InvalidOperationException("Total keys of all nodes exceeded 5");
            }

            if (totaledges > 6)
            {
                throw new InvalidOperationException("Total edges of all nodes exceeded 6");
            }

            this.Fuse(n1);
            this.Fuse(n2);
        }
コード例 #8
0
        public void Fuse(NodoBusqueda n1)
        {
            int totalkeys  = n1.keys.Count;
            int totaledges = n1.edges.Count;

            totalkeys  += this.keys.Count;
            totaledges += this.edges.Count;

            if (totalkeys > 5)
            {
                throw new InvalidOperationException("Total keys of all nodes exceeded 5");
            }

            if (totaledges > 6)
            {
                throw new InvalidOperationException("Total edges of all nodes exceeded 6");
            }

            for (int x = 0; x < n1.keys.Count; x++)
            {
                string k = n1.keys[x];
                this.Push(k);
            }
        }
コード例 #9
0
 public ArbolBusqueda()
 {
     Root = null;
 }