Exemple #1
0
 public BinNode(ref T data)
 {
     Data      = data;
     Key       = Data.GetHashCode();
     Height    = 1;
     LeftChild = RightChild = null;
 }
Exemple #2
0
        private BinNode <Q> Insert <Q>(BinNode <Q> node, ref Q data, out bool treeModified)
        {
            if (node == null)
            {
                treeModified = true;
                return(new BinNode <Q>(ref data));
            }
            var newNodeKey = data.GetHashCode();

            if (node.Key > newNodeKey)
            {
                node.LeftChild = Insert <Q>(node.LeftChild, ref data, out treeModified);
            }
            else if (node.Key < newNodeKey)
            {
                node.RightChild = Insert <Q>(node.RightChild, ref data, out treeModified);
            }
            else
            {
                treeModified = false;
            }
            if (treeModified)
            {
                return(Balance <Q>(node));
            }
            return(node);
        }
Exemple #3
0
 private BinNode <Q> RemoveMin <Q>(BinNode <Q> p)
 {
     if (p.LeftChild == null)
     {
         return(p.RightChild);
     }
     p.LeftChild = RemoveMin <Q>(p.LeftChild);
     return(Balance <Q>(p));
 }
Exemple #4
0
        private BinNode <Q> LeftTurn <Q>(BinNode <Q> p)
        {
            var q = p.RightChild;

            p.RightChild = q.LeftChild;
            q.LeftChild  = p;
            p.FixHeight();
            q.FixHeight();

            return(q);
        }
Exemple #5
0
        private BinNode <Q> Delete <Q>(BinNode <Q> node, ref Q data, out bool treeModified)
        {
            if (node == null)
            {
                treeModified = false;
                return(null);
            }
            var dataKey = data.GetHashCode();

            if (dataKey < node.Key)
            {
                node.LeftChild = Delete <Q>(node.LeftChild, ref data, out treeModified);
            }
            else if (dataKey > node.Key)
            {
                node.RightChild = Delete <Q>(node.RightChild, ref data, out treeModified);
            }
            else
            {
                if (data.Equals(node.Data))
                {
                    treeModified = true;
                    var l = node.LeftChild;
                    var r = node.RightChild;
                    if (r == null)
                    {
                        return(l);
                    }
                    var min = FindMin <Q>(r);
                    min.RightChild = RemoveMin <Q>(r);
                    min.LeftChild  = l;
                    return(Balance <Q>(min));
                }
                treeModified = false;
                return(node);
            }
            if (treeModified)
            {
                return(Balance <Q>(node));
            }
            return(node);
        }
Exemple #6
0
 private BinNode <Q> Balance <Q>(BinNode <Q> p)
 {
     p.FixHeight();
     if (p.BalanceFactor == 2)
     {
         if (p.RightChild.BalanceFactor < 0)
         {
             p.RightChild = RightTurn <Q>(p.RightChild);
         }
         p = LeftTurn <Q>(p);
     }
     if (p.BalanceFactor == -2)
     {
         if (p.LeftChild.BalanceFactor > 0)
         {
             p.LeftChild = LeftTurn <Q>(p.LeftChild);
         }
         p = RightTurn <Q>(p);
     }
     return(p);
 }
Exemple #7
0
        public bool Add(T data)
        {
            bool result = false;

            if (_root != null)
            {
                bool modified;
                _root = Insert <T>(_root, ref data, out modified);
                if (modified)
                {
                    result = true;
                    Count++;
                }
            }
            else
            {
                _root  = new BinNode <T>(ref data);
                result = true;
                Count++;
            }
            return(result);
        }
Exemple #8
0
        private BinNode <Q> Find <Q>(BinNode <Q> p, ref Q data)
        {
            if (p == null)
            {
                return(null);
            }
            var dataKey = data.GetHashCode();

            if (dataKey < p.Key)
            {
                return(Find <Q>(p.LeftChild, ref data));
            }
            else if (dataKey > p.Key)
            {
                return(Find <Q>(p.RightChild, ref data));
            }
            else if (data.Equals(p.Data))
            {
                return(p);
            }
            return(null);
        }
Exemple #9
0
 private BinNode <Q> FindMin <Q>(BinNode <Q> p)
 {
     return(p.LeftChild == null ? p : FindMin <Q>(p.LeftChild));
 }