Exemple #1
0
 public BinarySearchTree(IComparer <T> comp)
 {
     this._comp    = comp;
     this._count   = 0;
     this._h       = 0;
     this._r       = null;
     this._visitor = new BinaryNRVisitor <T>();//CHECK
 }
Exemple #2
0
        public Node <T> LeftMostChild(Node <T> node)
        {
            LinkedBinaryNode <T> np = node as LinkedBinaryNode <T>;

            if (np == null)
            {
                return(null);
            }
            return(np.LeftSon);
        }
Exemple #3
0
        public Node <T> Parent(Node <T> node)
        {
            LinkedBinaryNode <T> np = node as LinkedBinaryNode <T>;

            if (np == null)//OR np.Parent
            {
                return(null);
            }
            //return np.Parent;
            return(__GetParent(np));//OR np.Parent
        }
Exemple #4
0
        public Node <T> RightSibling(Node <T> node)
        {
            LinkedBinaryNode <T> np = node as LinkedBinaryNode <T>;

            //np = np.Parent;
            np = __GetParent(np);                                                          //OR np.Parent;
            if (np == null || np.RightSon == null || np.RightSon.Value.Equals(node.Value)) //ROOT HAS NO ONE BROTHERS.
            {
                return(null);
            }
            return(np.RightSon);
        }
Exemple #5
0
        public T Max()
        {
            LinkedBinaryNode <T> p    = _r;
            LinkedBinaryNode <T> node = _r;

            while (node.RightSon != null)
            {
                p    = node;
                node = node.RightSon;
            }
            T m = node.Value;

            return(m);
        }
Exemple #6
0
        private T __DeleteMin(LinkedBinaryNode <T> node)
        {
            LinkedBinaryNode <T> p = _r;

            if (_count == 0)
            {
                return(default(T));
            }
            while (node.LeftSon != null)
            {
                p    = node;
                node = node.LeftSon;
            }
            T m = node.Value;

            p.LeftSon = node.RightSon;
            _count   -= 1;
            return(m);
        }
Exemple #7
0
        //DO: DELETE(MAKE ptr IN Node) OR REPLACE
        private LinkedBinaryNode <T> __GetParent(LinkedBinaryNode <T> c)
        {
            if (c == null)
            {
                return(null);
            }
            LinkedBinaryNode <T> node = _r;
            LinkedBinaryNode <T> p    = null;

            if (node != null)
            {
                if (_comp.Compare(c.Value, node.Value) == 0)
                {
                    return(null);//PARENT OF ROOT ISNULL
                }
            }
            while (node != null)
            {
                if (_comp.Compare(c.Value, node.Value) < 0)
                {
                    p    = node;
                    node = node.LeftSon;
                    if (node.Value.Equals(c.Value))
                    {
                        return(p);
                    }
                }
                else
                {
                    p    = node;
                    node = node.RightSon;
                    if (node.Value.Equals(c.Value))
                    {
                        return(p);
                    }
                }
            }
            return(null);
        }
Exemple #8
0
 //MEMBER
 private Boolean __Member(T item, LinkedBinaryNode <T> node)
 {
     if (_count == 0)
     {
         return(false);
     }
     while (node != null)
     {
         if (_comp.Compare(item, node.Value) == 0 || node.Value.Equals(item))
         {
             return(true);
         }
         else if (_comp.Compare(item, node.Value) < 0)
         {
             node = node.LeftSon;
         }
         else
         {
             node = node.RightSon;
         }
     }
     return(false);
 }
Exemple #9
0
 private void __Delete(T item, LinkedBinaryNode <T> node)
 {
     while (node != null)
     {
         if (_comp.Compare(item, node.Value) < 0)
         {
             node = node.LeftSon; //DELETE(node.LeftSon)
         }
         else if (_comp.Compare(item, node.Value) > 0)
         {
             node = node.RightSon;//DELETE(node.RightSon)
         }
         else if (node.LeftSon == null && node.RightSon == null)
         {
             node    = null;
             _count -= 1;
             return;
         }
         else if (node.LeftSon == null)
         {
             node    = node.RightSon;
             _count -= 1;
             return;
         }
         else if (node.RightSon == null)
         {
             node    = node.LeftSon;
             _count -= 1;
             return;
         }
         else
         {
             node.Value = __DeleteMin(node.RightSon);
             return;
         }
     }
 }
Exemple #10
0
 //INSERT
 private void __Add(T item, LinkedBinaryNode <T> node)
 {
     if (_count == 0)
     {
         _r       = new LinkedBinaryNode <T>();
         _r.Dept  = 0;
         _r.Value = item;
         _count  += 1;
         return;
     }
     while (node != null)
     {
         if (_comp.Compare(item, node.Value) < 0)
         {
             if (node.IsLeftLeaf)//REPLACE
             {
                 node.LeftSon       = new LinkedBinaryNode <T>();
                 node.LeftSon.Value = item;
                 _count            += 1;
                 return;
             }
             node = node.LeftSon;
         }
         else if (_comp.Compare(item, node.Value) > 0)
         {
             if (node.IsRightLeaf)//REPLACE
             {
                 node.RightSon       = new LinkedBinaryNode <T>();
                 node.RightSon.Value = item;
                 _count += 1;
                 return;
             }
             node = node.RightSon;
         }
     }
 }
Exemple #11
0
 //MAKENULL
 public void Clear()
 {
     this._count = 0;
     this._h     = 0;
     this._r     = null;
 }