Ejemplo n.º 1
0
        public void Delete(BSTreeNode <TKey> node)
        {
            BSTreeNode <TKey> y = Leaf;

            if (node.Left == Leaf)
            {
                Transplant(node, node.Right);
            }
            else if (node.Right == Leaf)
            {
                Transplant(node, node.Left);
            }
            else
            {
                y = Minimum(node.Right);
            }
            if (y.Parent != node)
            {
                Transplant(y, y.Right);
                y.Right        = node.Right;
                y.Right.Parent = y;
            }
            Transplant(node, y);
            y.Left        = node.Left;
            y.Left.Parent = y;
        }
Ejemplo n.º 2
0
        private void Insert(BSTreeNode <TKey> node)
        {
            BSTreeNode <TKey> y = Leaf;
            BSTreeNode <TKey> x = Root;

            while (x != Leaf)
            {
                y = x;
                if (node.CompareTo(x) < 0)
                {
                    x = x.Left;
                }
                else
                {
                    x = x.Right;
                }
            }
            node.Parent = y;
            if (IsEmpty())
            {
                Root = node;
            }
            else if (node.CompareTo(y) < 0)
            {
                y.Left = node;
            }
            else
            {
                y.Right = node;
            }
        }
Ejemplo n.º 3
0
 public BSTreeNode <TKey> Minimum(BSTreeNode <TKey> node)
 {
     while (node.Left != Leaf)
     {
         node = node.Left;
     }
     return(node);
 }
Ejemplo n.º 4
0
 public BSTreeNode <TKey> Maximum(BSTreeNode <TKey> node)
 {
     while (node.Right != Leaf)
     {
         node = node.Right;
     }
     return(node);
 }
Ejemplo n.º 5
0
 protected void InOrderWalk(BSTreeNode <TKey> node, List <BSTreeNode <TKey> > nodesList)
 {
     if (node != Leaf)
     {
         InOrderWalk(node.Left, nodesList);
         nodesList.Add(node);
         InOrderWalk(node.Right, nodesList);
     }
 }
Ejemplo n.º 6
0
        protected BSTreeNode <TKey> Search(BSTreeNode <TKey> node, TKey inputKey)
        {
            int c = 0;

            if (node == Leaf || (c = inputKey.CompareTo(node.Key)) == 0)
            {
                return(node);
            }
            return(c < 0 ? Search(node.Left, inputKey) : Search(node.Right, inputKey));
        }
Ejemplo n.º 7
0
 public BSTreeNode <TKey> GetNode(BSTreeNode <TKey> node, TKey inputKey)
 {
     try
     {
         return(Search(node, inputKey));
     }
     catch (NullReferenceException)
     {
         throw new KeyNotFoundException("Key not found.");
     }
 }
Ejemplo n.º 8
0
 public bool ContainsKey(TKey key)
 {
     try
     {
         BSTreeNode <TKey> node = GetNode(key);
         return(node != null);
     }
     catch (KeyNotFoundException)
     {
         return(false);
     }
 }
Ejemplo n.º 9
0
        public BSTreeNode <TKey> Predecessor(BSTreeNode <TKey> thisNode)
        {
            if (thisNode.Left != Leaf)
            {
                return(Maximum(thisNode.Left));
            }
            BSTreeNode <TKey> node = thisNode.Parent;

            while (node != Leaf && thisNode == node.Left)
            {
                thisNode = node;
                node     = node.Parent;
            }
            return(node);
        }
Ejemplo n.º 10
0
        public BSTreeNode <TKey> Successor(BSTreeNode <TKey> thisNode)
        {
            if (thisNode.Right != Leaf)
            {
                return(Minimum(thisNode.Right));
            }
            BSTreeNode <TKey> node = thisNode.Parent;

            while (node != Leaf && thisNode == node.Right)
            {
                thisNode = node;
                node     = node.Parent;
            }
            return(node);
        }
Ejemplo n.º 11
0
 public void Transplant(BSTreeNode <TKey> first, BSTreeNode <TKey> second)
 {
     if (first.Parent == Leaf)
     {
         Root = second;
     }
     else if (first == first.Parent.Left)
     {
         first.Parent.Left = second;
     }
     else
     {
         first.Parent.Right = second;
     }
     if (second != Leaf)
     {
         second.Parent = first.Parent;
     }
 }
Ejemplo n.º 12
0
        public BSTreeNode <TKey> GetNode(TKey inputKey)
        {
            BSTreeNode <TKey> node = Root;

            try
            {
                do
                {
                    if (Compare(inputKey, node.Key) == 0)
                    {
                        return(node);
                    }
                    node = Compare(inputKey, node.Key) > 0 ? node.Right : node.Left;
                } while (true);
            }
            catch (NullReferenceException)
            {
                throw new KeyNotFoundException("Key not found.");
            }
        }
Ejemplo n.º 13
0
        private int Rank(TKey key, BSTreeNode <TKey> node, ref int nodeRank)
        {
            if (node == Leaf)
            {
                return(0);
            }

            int cmp = key.CompareTo(node.Key);

            if (cmp < 0)
            {
                nodeRank++;
                return(Rank(key, node.Left, ref nodeRank));
            }
            else if (cmp > 0)
            {
                nodeRank++;
                return(Rank(key, node.Right, ref nodeRank));
            }
            else
            {
                return(nodeRank);
            }
        }
Ejemplo n.º 14
0
 public BSTreeNode(TKey key) : this()
 {
     Key    = key;
     Parent = Left = Right = BSTree <TKey> .Leaf;
 }
Ejemplo n.º 15
0
        public void Add(TKey key)
        {
            BSTreeNode <TKey> newNode = BSTreeNode <TKey> .CreateNode(key);

            Insert(newNode);
        }
Ejemplo n.º 16
0
 public BSTree()
 {
     Leaf = new BSTreeNode <TKey>();
     Root = Leaf;
 }
Ejemplo n.º 17
0
 public int CompareTo(BSTreeNode <TKey> node)
 {
     return(this.Key.CompareTo(node.Key));
 }