Ejemplo n.º 1
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.º 2
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;
            }
        }