Ejemplo n.º 1
0
        public override bool Equals(Object ob)
        {
            NodeCell <T> B = ob as NodeCell <T>;

            if (ob == null)
            {
                return(false);
            }
            if (this.Dept != B.Dept)
            {
                return(false);
            }
            if (this.LeftMostChild != B.LeftMostChild)
            {
                return(false);
            }
            if (this.RightSibling != B.RightSibling)
            {
                return(false);
            }
            if (this.Parent != B.Parent)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        private void __Move()
        {
            NodeCell <T>[] nb = new NodeCell <T> [_capacity];
            nb[0] = _cellspace[0];//ROOT
            Int32 k = 0;

            CSharpDataStructures.Structures.Lists.Stack <NodeCell <T> > S = new CSharpDataStructures.Structures.Lists.Stack <NodeCell <T> >(_capacity);
            STACK2 S2 = new STACK2(_capacity);

            S.Push(nb[0]);
            S2.Push(0);
            k = __MoveChildren(nb[0], 0, nb, 0, S, S2, k);


            while (k < _capacity && !S.IsEmpty() && !S2.IsEmpty())
            {
                Int32        idx = S2.Top();
                NodeCell <T> c   = S.Top();
                k = __MoveChildren(c, k, nb, idx, S, S2, k);
            }
            for (Int32 ii = 0; ii < nb.Length; ii++)
            {
                if (nb[ii] != null)
                {
                    nb[ii].Idx = ii;
                }
            }
            this._cellspace = null;
            this._cellspace = nb;
            this._lastnode  = k;
        }
Ejemplo n.º 3
0
        ///<summary>Получить список детей указанного узла дерева.</summary>
        public IList <Node <T> > GetChildren(Node <T> n)
        {
            /*
             * Node<T> c = LeftMostChild(n);
             * List<Node<T>> l = new List<Node<T>>();
             * while(c != null){
             *  l.Add(c);
             *  c = RightSibling(c);
             * }*/

            NodeCell <T> c = n as NodeCell <T>;

            List <Node <T> > l = new List <Node <T> >();

            if (c.LeftMostChild == -1)
            {
                return(l);
            }
            c = _cellspace[c.LeftMostChild];
            l.Add(c);
            while (c.RightSibling != -1)
            {
                c = _cellspace[c.RightSibling];
                l.Add(c);
            }
            return(l);
        }
Ejemplo n.º 4
0
        ///<summary>Получить поддерево с корнем n узла дерева</summary>
        ///<param name="n">Корень поддерева. Является узлом дерева.</param>
        //ERROR IN ARRAYS.
        public IPositionalTree <T> GetSubTree(Node <T> n)
        {
            NodeCell <T> nc   = n as NodeCell <T>;
            List <Int32> idxs = new List <Int32>();

            _visitor.PreOrder(this, n, (p) => __GetSubTreeIdxs(p, ref idxs));
            NodeCell <T>[] ar = new NodeCell <T> [_count];
            for (Int32 i = 0; i < idxs.Count; i++)
            {
                ar[idxs[i]] = _cellspace[idxs[i]];
            }
            Int32 ln     = idxs[idxs.Count - 1];
            Int32 co     = idxs.Count;
            Int32 offset = ar[idxs[0]].Dept;

            for (Int32 i = 0; i < idxs.Count; i++)
            {
                ar[idxs[i]].Dept = ar[idxs[i]].Dept - offset;
            }

            ar[0]              = ar[idxs[0]];//move sub_root to the root position.
            ar[0].Parent       = -1;
            ar[idxs[0]].Parent = -1;
            IList <Node <T> > rc = GetChildren(ar[0]);

            for (Int32 ii = 0; ii < rc.Count; ii++)
            {
                ((NodeCell <T>)rc[ii]).Parent = 0;
            }
            return(new ArrayTree <T>(ar, co, ln, _visitor));
        }
Ejemplo n.º 5
0
        public void Add(Int32 dept, T item)//HANDLE PARAMS
        {
            NodeCell <T> n = new NodeCell <T>(dept);

            n.Value         = item;
            n.LeftMostChild = -1;
            n.RightSibling  = -1;
            Insert2(dept, 0, n);//add to the first element of the dept-1 yield
        }
Ejemplo n.º 6
0
        public void Add(Int32 dept, Int32 np, T item) //HANDLE PARAMS
        {
            NodeCell <T> n = new NodeCell <T>(dept);  //np is cursor to the nodeparent located at the height == dept - 1

            n.Value         = item;
            n.LeftMostChild = -1;
            n.RightSibling  = -1;
            Insert2(dept, np, n);//Add to the np element of the dept-1 yield.
        }
Ejemplo n.º 7
0
        public void Add(T item)
        {
            NodeCell <T> n = new NodeCell <T>(1);

            n.Value         = item;
            n.LeftMostChild = -1;
            n.RightSibling  = -1;
            Insert2(1, 0, n);//add to the root.
        }
Ejemplo n.º 8
0
        private void __CheckMinDept(Node <T> n, ref Int32 q)
        {
            NodeCell <T> nc = n as NodeCell <T>;

            if (nc.LeftMostChild == -1 && nc.Parent != -1)
            {
                q = q == 0 ? nc.Dept : Math.Min(q, nc.Dept);
            }
        }
Ejemplo n.º 9
0
        ///<summary>Родитель узла. (NodeCell<typeparamref name="T"/>)</summary>
        public Node <T> Parent(Node <T> node)
        {
            NodeCell <T> np = node as NodeCell <T>;

            if (np == null || np.Parent == -1)
            {
                return(null);
            }
            return(_cellspace[np.Parent]);
        }
Ejemplo n.º 10
0
        ///<summary>Первый сын узла (NodeCell<typeparamref name="T"/>)</summary>
        public Node <T> LeftMostChild(Node <T> node)
        {
            NodeCell <T> np = node as NodeCell <T>;

            if (np == null || np.LeftMostChild == -1)
            {
                return(null);
            }
            return(_cellspace[np.LeftMostChild]);
        }
Ejemplo n.º 11
0
        ///<summary>Правый брат узла (NodeCell<typeparamref name="T"/>)</summary>
        public Node <T> RightSibling(Node <T> node)
        {
            NodeCell <T> np = node as NodeCell <T>;

            if (np == null || np.RightSibling == -1)
            {
                return(null);
            }
            return(_cellspace[np.RightSibling]);
        }
Ejemplo n.º 12
0
 private void __GetParent(Node <T> n, ref Int32 q, Int32 d, ref NodeCell <T> _pr)
 {
     if (_pr != null)
     {
         return;
     }
     q = ((NodeCell <T>)n).Dept;//REPLACED S1 -> S2.
     if (q == d - 1)
     {
         _pr = n as NodeCell <T>;//REPLACED S1 -> S2.
     }
 }
Ejemplo n.º 13
0
        //USE INTERFACE ITree<T> for Adding.
        ///<summary>Добавить к указанному узлу дерева новый узел.</summary>
        ///<param name="p">Родитель нового узла</param>
        ///<param name="item">Содержимое нового узла.</param>
        public void AddTo(Node <T> p, T item)
        {
            NodeCell <T> pr = p as NodeCell <T>;

            if (p == null)
            {
                //Console.WriteLine("error");
                return;
            }
            Int32 idxp = 0;

            if (pr.Parent != -1)
            {
                NodeCell <T> cont = (NodeCell <T>)Parent(p);
                idxp = cont.LeftMostChild;
                cont = _cellspace[idxp];
                while (cont.RightSibling != -1 && !cont.Equals(p))//Compute idxp for np.
                {
                    idxp = cont.RightSibling;
                    cont = _cellspace[idxp];
                }
            }
            this._lastnode = this._lastnode + 1;
            this._count   += 1;
            __CheckCapacity(_lastnode);


            if (pr.LeftMostChild != -1)
            {
                NodeCell <T> rb = _cellspace[pr.LeftMostChild];
                while (rb != null && rb.RightSibling != -1)
                {
                    rb = _cellspace[rb.RightSibling];
                }
                rb.RightSibling = _lastnode;
            }
            else
            {
                pr.LeftMostChild = _lastnode;
            }
            NodeCell <T> n = new NodeCell <T>(pr.Dept + 1);

            n.Value               = item;
            n.LeftMostChild       = -1;
            n.RightSibling        = -1;
            n.Parent              = idxp;
            n.Idx                 = _lastnode;
            _cellspace[_lastnode] = n;
        }
Ejemplo n.º 14
0
        ///<summary>Получить последнего сына указанного узла дерева.</summary>
        public Node <T> RightMostChild(Node <T> n)
        {
            NodeCell <T> c = n as NodeCell <T>;

            if (c == null || c.LeftMostChild == -1)
            {
                return(null);
            }
            c = _cellspace[c.LeftMostChild];
            while (c.RightSibling != -1)
            {
                c = _cellspace[c.RightSibling];
            }
            return(c);
        }
Ejemplo n.º 15
0
 ///<summary>Создать дерево с корнем на <c>capacity</c> ячеек.</summary>
 public ArrayTree(Int32 capacity)
 {
     this._capacity  = capacity;
     this._cellspace = new NodeCell <T> [capacity];
     this._count     = 1;
     this._dept      = 0;
     this._lastnode  = 0;
     this._mh        = 0;
     this._visitor   = new NRVisitor <T>();
     _cellspace[0]   = new NodeCell <T>(0) //root.
     {
         Value         = default(T),
         LeftMostChild = -1,
         RightSibling  = -1,
         Parent        = -1
     };
 }
Ejemplo n.º 16
0
 //MAKENULL
 ///<summary>Удалить все узлы дерева. (Корень сохранится, но не будет ничего содержать)</summary>
 public void Clear()
 {
     for (Int32 i = 1; i < _cellspace.Length; i++)
     {
         _cellspace[i] = null;
     }
     this._count    = 1;
     this._dept     = 0;
     this._lastnode = 0;
     _cellspace[0]  = new NodeCell <T>(0) //root.
     {
         Value         = default(T),
         LeftMostChild = -1,
         RightSibling  = -1,
         Parent        = -1
     };
 }
Ejemplo n.º 17
0
 private void __CheckCapacity(Int32 dest)
 {
     if (dest >= _capacity)
     {
         NodeCell <T>[] nb = new NodeCell <T> [_capacity * 2];
         this._capacity *= 2;
         for (Int32 i = 0; i < _cellspace.Length; i++)
         {
             nb[i] = _cellspace[i];
             if (_cellspace[i] != null)
             {
                 nb[i].Idx = i;
             }
         }
         _cellspace = nb;
     }
 }
Ejemplo n.º 18
0
        //Assuming that delete operation iterates from the left end...
        private void __DeleteNode(Node <T> n)
        {
            NodeCell <T> nc  = n as NodeCell <T>;//REPLACE S1 -> S2
            NodeCell <T> p   = _cellspace[nc.Parent];
            Int32        idx = p.LeftMostChild;
            NodeCell <T> ln  = _cellspace[idx];

            //Console.WriteLine(nc.Value);
            if (nc.RightSibling != -1)
            {
                p.LeftMostChild = nc.RightSibling;
            }
            else
            {
                p.LeftMostChild = -1;
            }
            _cellspace[idx] = null;
            _count         -= 1;
        }
Ejemplo n.º 19
0
        private Int32 __MoveChildren(NodeCell <T> n, Int32 i, NodeCell <T>[] nb, Int32 pp, CSharpDataStructures.Structures.Lists.Stack <NodeCell <T> > S, STACK2 S2, Int32 def_i)
        {
            if (S.IsEmpty() && S2.IsEmpty())
            {
                return(def_i);//work is over. stack is empty.
            }
            NodeCell <T> s = n;

            if (s.LeftMostChild == -1)//leaf found.
            {
                S.Pop();
                S2.Pop();
                return(def_i);//return last index of the last element of the nb.
            }
            s               = _cellspace[s.LeftMostChild];
            i              += 1;
            s.Parent        = pp;
            n.LeftMostChild = i;
            nb[i]           = s;
            S2.Pop();
            S.Pop();
            S.Push(nb[i]);                            //Pop Parent And Push his children.
            S2.Push(i);
            while (s != null && s.RightSibling != -1) //Align all children from old array in new array.
            {
                i    += 1;
                nb[i] = _cellspace[s.RightSibling];
                nb[i - 1].RightSibling = i;
                nb[i - 1].Parent       = pp;
                nb[i].Parent           = pp;
                S.Push(nb[i]);
                S2.Push(i);
                s = _cellspace[s.RightSibling];
            }
            return(i);
        }
Ejemplo n.º 20
0
        //Delete leaf
        private void __DeleteLeaf(Node <T> n)
        {
            NodeCell <T> p = Parent(n) as NodeCell <T>;
            NodeCell <T> c = _cellspace[p.LeftMostChild];

            if (c.Equals(n))
            {
                Int32 t = p.LeftMostChild;
                _count         -= 1;
                p.LeftMostChild = c.RightSibling;
                _cellspace[t]   = null;
            }
            else
            {
                while (c.RightSibling != -1 && !_cellspace[c.RightSibling].Equals(n))
                {
                    c = _cellspace[c.RightSibling];
                }
                Int32 t = c.RightSibling == -1 ? c.Idx : c.RightSibling;
                c.RightSibling = -1;
                _cellspace[t]  = null;
                _count        -= 1;
            }
        }
Ejemplo n.º 21
0
        public void Insert2(Int32 dept, Int32 np, NodeCell <T> node)//MAKE PRIVATE
        {
            if (dept <= 0)
            {
                dept = 1;
            }
            if (np < 0)
            {
                np = 0;
            }

            Int32        idxp   = 0;
            NodeCell <T> parent = null;
            Int32        q      = 0;

            this._visitor.PreOrder(this, (n) => __GetParent(n, ref q, dept, ref parent));
            q = 0;
            if (parent == null)
            {
                Console.WriteLine("dept is out of range");
                return;
            }
            //Compute idxp and check np parameter.
            while (parent.RightSibling != -1 && q < np)
            {
                idxp   = parent.RightSibling;
                parent = _cellspace[parent.RightSibling];
                q     += 1;
            }
            if (q < np)
            {
                Console.WriteLine("parent in the dept {0} is not exists. Parameter np {1} is out of range", dept, q);
                np = q;//IDXP has been computed as the rightmost element in (dept - 1)th level.
                //return;
            }
            if (Parent(parent) == null)
            {
                idxp = 0;     //root.
            }
            else if (np == 0) //get idxp for np == 0

            {
                NodeCell <T> cont = (NodeCell <T>)Parent(parent);
                idxp = cont.LeftMostChild;

                /*
                 * cont = _cellspace[idxp];
                 * while(cont.RightSibling != -1 && !cont.Equals(parent)){//Compute idxp for np.
                 *  idxp = cont.RightSibling;
                 *  cont = _cellspace[idxp];
                 * }*/
            }
            this._lastnode = this._lastnode + 1;
            this._count   += 1;
            __CheckCapacity(_lastnode);

            //parent without any child.
            if (parent.LeftMostChild == -1)
            {
                parent.LeftMostChild  = _lastnode;//make as the first child.
                node.Parent           = idxp;
                node.Idx              = _lastnode;
                _cellspace[_lastnode] = node;
            }

            //parent with single child.
            else if (_cellspace[parent.LeftMostChild].RightSibling == -1)
            {
                _cellspace[parent.LeftMostChild].RightSibling = _lastnode;//make as a right brother.
                node.Parent           = idxp;
                node.Idx              = _lastnode;
                _cellspace[_lastnode] = node;
            }
            else //parent with children.
            {
                NodeCell <T> qq = _cellspace[_cellspace[parent.LeftMostChild].RightSibling];//second son.
                while (qq.RightSibling != -1)         //while second son has right brothers...
                {
                    qq = _cellspace[qq.RightSibling]; //move to this brother, and check whether it has another right brother.
                }
                node.Parent           = idxp;         //qq.Parent -> idxp.
                qq.RightSibling       = _lastnode;    //add new brother to children.
                node.Idx              = _lastnode;
                _cellspace[_lastnode] = node;
            }
            __ComputeH();
        }
Ejemplo n.º 22
0
        ///<summary>Сохранить в списке idxs индексы узлов
        ///в массиве ячеек дерева (_cellspace)</summary>
        private void __GetSubTreeIdxs(Node <T> p, ref List <Int32> idxs)
        {
            NodeCell <T> c = p as NodeCell <T>;

            idxs.Add(c.Idx);
        }