Example #1
0
        public TwoThreeTree <T> Union(TwoThreeTree <T> B)
        {
            if (B == null || B.Count == 0)
            {
                TwoThreeTree <T> R = new TwoThreeTree <T>(_comp);
                R._r     = this._r;
                R._count = this._count;
                R._h     = this._h;
                return(R);
            }
            ITree <T>        T2 = (ITree <T>)B;
            TwoThreeTree <T> C  = new TwoThreeTree <T>(_comp);

            LinkedStack <T> S1 = new LinkedStack <T>();//CHECK FOR S = Stack<T>
            LinkedStack <T> S2 = new LinkedStack <T>();

            this._visitor.PostOrder(this, (n) => __NodesVals(n, ref S1));//SYMMETRIC ORDER (INORDER)
            this._visitor.PostOrder(T2, (n) => __NodesVals(n, ref S2));
            while (!S1.IsEmpty())
            {
                C.Add(S1.Top());
                S1.Pop();
            }
            while (!S2.IsEmpty())
            {
                C.Add(S2.Top());
                S2.Pop();
            }
            return(C);
        }
Example #2
0
        public TwoThreeTree <T> Difference(TwoThreeTree <T> B)
        {
            if (B == null || B.Count == 0)
            {
                TwoThreeTree <T> R = new TwoThreeTree <T>(_comp);
                R._r     = this._r;
                R._count = this._count;
                R._h     = this._h;
                return(R);
            }
            ITree <T>        T2 = (ITree <T>)B;
            TwoThreeTree <T> C  = new TwoThreeTree <T>(_comp);

            LinkedStack <T> S1 = new LinkedStack <T>();//CHECK FOR S = Stack<T>
            LinkedStack <T> S2 = new LinkedStack <T>();

            this._visitor.PostOrder(this, (n) => __NodesVals(n, ref S1));//SYMMETRIC ORDER (INORDER)
            this._visitor.PostOrder(T2, (n) => __NodesVals(n, ref S2));
            bool flag = true;

            while (!S1.IsEmpty())
            {
                if (flag && S2.IsEmpty())
                {
                    flag = false;
                }
                if (flag && _comp.Compare(S1.Top(), S2.Top()) == 0)
                {
                    S1.Pop();
                    S2.Pop();
                }
                else if (flag && _comp.Compare(S1.Top(), S2.Top()) > 0)
                {
                    C.Add(S1.Top());
                    S1.Pop();
                }
                else
                {
                    if (flag)
                    {
                        S2.Pop();
                    }
                    else
                    {
                        C.Add(S1.Top());
                        S1.Pop();
                    }
                }
            }

            return(C);
        }
Example #3
0
        //CHECK STATEMENTS(_h -= 1; h += 1;) IN METHODS __Delete0, __Insert0
        //IF DISTINCT(__ComputeH, THE STATEMENTS) DELETE STATEMENTS AND UNCOMMENT METHOD __ComputeH
        //ELSE DELETE METHOD __ComputeH

        /*
         * private void __ComputeH(){
         *  if(_count <= 1){
         *      _h = 0;
         *  }
         *  else if(_count > 1 && _count <= 3){
         *      _h = 1;
         *  }
         *  else {
         *      TwoThreeNode<T> node = _r;
         *      Int32 h = 0;
         *      while(node.FirstSon != null){
         *          node = node.FirstSon;
         *          h++;
         *      }
         *      _h = h;
         *  }
         * }*/
        #region setOperators

        public TwoThreeTree <T> Intersection(TwoThreeTree <T> B)
        {
            if (B == null || B.Count == 0)
            {
                return(new TwoThreeTree <T>(_comp));//EMPTY TREE
            }
            ITree <T>        T2 = (ITree <T>)B;
            TwoThreeTree <T> C  = new TwoThreeTree <T>(_comp);

            LinkedStack <T> S1 = new LinkedStack <T>();//CHECK FOR S = Stack<T>
            LinkedStack <T> S2 = new LinkedStack <T>();

            this._visitor.PostOrder(this, (n) => __NodesVals(n, ref S1));//SYMMETRIC ORDER (INORDER)
            this._visitor.PostOrder(T2, (n) => __NodesVals(n, ref S2));
            while (!S1.IsEmpty() && !S2.IsEmpty())
            {
                if (_comp.Compare(S1.Top(), S2.Top()) == 0)
                {
                    C.Add(S1.Top());
                    S1.Pop();
                    S2.Pop();
                }
                else if (_comp.Compare(S1.Top(), S2.Top()) < 0)
                {
                    S2.Pop();
                }
                else
                {
                    S1.Pop();
                }
            }

            return(C);//CHANGE TYPE -> MAKE NEW METHOD.
        }