Пример #1
0
        public TwoThreeTree <T> SymmetricDifference(TwoThreeTree <T> B)
        {
            TwoThreeTree <T> ANB = Difference(B);      //A\B
            TwoThreeTree <T> BNA = B.Difference(this); //B\A

            return(ANB.Union(BNA));
        }
Пример #2
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);
        }
Пример #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.
        }
Пример #4
0
        public SortedLinkedList <T> SymmetricDifferenceList(TwoThreeTree <T> B)
        {
            SortedLinkedList <T> ANB = UnionList(B);        //A OR B
            SortedLinkedList <T> BNA = IntersectionList(B); //A AND B.

            return(ANB.Difference(BNA));                    //A OR B - (A AND B)
        }
Пример #5
0
 public SortedLinkedList <T> MergeList(TwoThreeTree <T> B)
 {
     if (IntersectionList(B).Count != 0)
     {
         return(null);//UNDEFINED
     }
     return(UnionList(B));
 }
Пример #6
0
 public TwoThreeTree <T> Merge(TwoThreeTree <T> B)
 {
     if (Intersection(B)._count != 0)
     {
         return(null);//UNDEFINED
     }
     return(Union(B));
 }
Пример #7
0
        public bool Overlaps(IEnumerable <T> other)
        {
            TwoThreeTree <T> B = new TwoThreeTree <T>(_comp);

            B.AddRange(other);
            B = Intersection(B);//A AND B

            return(B._count != 0);
        }
Пример #8
0
        public bool SetEquals(IEnumerable <T> other)
        {
            if (other == null)
            {
                return(false);
            }
            TwoThreeTree <T> B = new TwoThreeTree <T>(_comp);

            B.AddRange(other);
            return(Equals(B));
        }
Пример #9
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);
        }
Пример #10
0
        public void SymmetricExceptWith(IEnumerable <T> other)
        {
            if (other.Count() == 0)
            {
                return;
            }
            TwoThreeTree <T> B = new TwoThreeTree <T>(_comp);

            B.AddRange(other);
            B = SymmetricDifference(B);
            Clear();
            AddRange((IEnumerable <T>)B);
        }
Пример #11
0
        public void IntersectWith(IEnumerable <T> other)
        {
            TwoThreeTree <T> B = new TwoThreeTree <T>(_comp);

            B.AddRange(other);
            B = Intersection(B);
            if (B._count == 0)
            {
                Clear();
                return;
            }
            Clear();
            AddRange((IEnumerable <T>)B);
        }
Пример #12
0
 public Boolean Equals(TwoThreeTree <T> B)
 {
     if (B == null)
     {
         return(false);
     }
     if (IntersectionList(B).Count == _count)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #13
0
        public override Boolean Equals(Object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            TwoThreeTree <T> B = obj as TwoThreeTree <T>;

            if (B == null)
            {
                return(false);
            }
            else
            {
                return(Equals(B));
            }
        }
Пример #14
0
        public SortedLinkedList <T> DifferenceList(TwoThreeTree <T> B)
        {
            ITree <T> T2 = (ITree <T>)B;

            CSharpDataStructures.Structures.Lists.LinkedList <T> C = new CSharpDataStructures.Structures.Lists.LinkedList <T>();

            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));//POST 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(new SortedLinkedList <T>((IEnumerable <T>)C, _comp));
        }
Пример #15
0
        public bool IsProperSubsetOf(IEnumerable <T> other)
        {
            if (other.Count() == 0)
            {
                return(false);
            }
            TwoThreeTree <T> B = new TwoThreeTree <T>(_comp);

            B.AddRange(other);
            TwoThreeTree <T> C = Difference(B);//A\B.
            Int32            c = C._count;

            if (c == 0 && B._count > _count)
            {
                return(true);
            }
            return(false);
        }
Пример #16
0
        public bool IsSupersetOf(IEnumerable <T> other)
        {
            if (other.Count() == 0)
            {
                return(true);
            }
            TwoThreeTree <T> B = new TwoThreeTree <T>(_comp);

            B.AddRange(other);
            TwoThreeTree <T> C = B.Difference(this);//B\A.
            Int32            c = C._count;

            if (c == 0)
            {
                return(true);
            }
            return(false);
        }
Пример #17
0
        public bool IsSubsetOf(IEnumerable <T> other)
        {
            if (other.Count() == 0 && _count == 0)
            {
                return(true);
            }
            else if (other.Count() == 0 && _count != 0)
            {
                return(false);
            }
            TwoThreeTree <T> B = new TwoThreeTree <T>(_comp);

            B.AddRange(other);
            B = Difference(B);//A\B.
            Int32 c = B._count;

            if (c == 0)
            {
                return(true);
            }
            return(false);
        }
Пример #18
0
        public SortedLinkedList <T> UnionList(TwoThreeTree <T> B)
        {
            ITree <T> T2 = (ITree <T>)B;

            CSharpDataStructures.Structures.Lists.LinkedList <T> C = new CSharpDataStructures.Structures.Lists.LinkedList <T>();

            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));//POST 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(new SortedLinkedList <T>((IEnumerable <T>)C, _comp));
        }
Пример #19
0
        //RETURNS SortedLinkedList<T> INSTEAD OF 2-3 TREE
        public SortedLinkedList <T> IntersectionList(TwoThreeTree <T> B)
        {
            if (B == null || B.Count == 0)
            {
                return(new SortedLinkedList <T>(_comp));
            }
            ITree <T> T2 = (ITree <T>)B;


            CSharpDataStructures.Structures.Lists.LinkedList <T> C = new CSharpDataStructures.Structures.Lists.LinkedList <T>();

            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));//POST ORDER ()
            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(new SortedLinkedList <T>((IEnumerable <T>)C, _comp));
        }