Example #1
0
 protected void DeleteNode(BinaryKeyValueNode <Tkey, Tvalue> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException();
     }
     if (node.LeftChild != null && node.RightChild != null) //2 childs
     {
         BinaryKeyValueNode <Tkey, Tvalue> replaceBy = random.NextDouble() > .5 ? InOrderSuccesor(node) : InOrderPredecessor(node);
         DeleteNode(replaceBy);
         node.Value = replaceBy.Value;
         node.Key   = replaceBy.Key;
     }
     else //1 or less childs
     {
         var child = node.LeftChild == null ? node.RightChild : node.LeftChild;
         if (node.Parent.RightChild == node)
         {
             node.Parent.RightChild = child;
         }
         else
         {
             node.Parent.LeftChild = child;
         }
     }
     Count--;
 }
Example #2
0
        public void Insert(Tkey key, Tvalue value)
        {
            BinaryKeyValueNode <Tkey, Tvalue> parent  = null;
            BinaryKeyValueNode <Tkey, Tvalue> current = Root;
            int compare = 0;

            while (current != null)
            {
                parent  = current;
                compare = current.Key.CompareTo(key);
                current = compare < 0 ? current.RightChild : current.LeftChild;
            }
            BinaryKeyValueNode <Tkey, Tvalue> newNode = new BinaryKeyValueNode <Tkey, Tvalue>(key, value);

            if (parent != null)
            {
                if (compare < 0)
                {
                    parent.RightChild = newNode;
                }
                else
                {
                    parent.LeftChild = newNode;
                }
            }
            else
            {
                Root = newNode;
            }
            newNode.Parent = parent;
            Count++;
        }
Example #3
0
        protected BinaryKeyValueNode <Tkey, Tvalue> FindNode(Tkey key, bool ExceptionIfKeyNotFound = true)
        {
            BinaryKeyValueNode <Tkey, Tvalue> current = Root;

            while (current != null)
            {
                int compare = current.Key.CompareTo(key);
                if (compare == 0)
                {
                    return(current);
                }
                if (compare < 0)
                {
                    current = current.RightChild;
                }
                else
                {
                    current = current.LeftChild;
                }
            }
            if (ExceptionIfKeyNotFound)
            {
                throw new KeyNotFoundException();
            }
            else
            {
                return(null);
            }
        }
Example #4
0
        protected BinaryKeyValueNode <Tkey, Tvalue> InOrderPredecessor(BinaryKeyValueNode <Tkey, Tvalue> node)
        {
            BinaryKeyValueNode <Tkey, Tvalue> succesor = node.LeftChild;

            while (succesor.RightChild != null)
            {
                succesor = succesor.RightChild;
            }
            return(succesor);
        }
        private BinaryKeyValueNode <Tkey> InOrderPredecessor(BinaryKeyValueNode <Tkey> node)
        {
            BinaryKeyValueNode <Tkey> succesor = node.LeftChild;

            while (succesor.RightChild != null)
            {
                succesor = succesor.RightChild;
            }
            return(succesor);
        }
        private BinaryKeyValueNode <Tkey> FindNode(Tkey key, bool ExceptionIfKeyNotFound = true)
        {
            BinaryKeyValueNode <Tkey> current = Root;

            while (current != null)
            {
                int compare = current.Key.CompareTo(key);
                if (compare == 0)
                {
                    return(current);
                }
                if (compare < 0)
                {
                    current = current.RightChild;
                }
                else
                {
                    current = current.LeftChild;
                }
            }
            return(null);
        }
Example #7
0
        protected IEnumerable <Tvalue> TraverseNode(BinaryKeyValueNode <Tkey, Tvalue> node, DepthFirstTraversalMethod method)
        {
            IEnumerable <Tvalue> TraverseLeft  = node.LeftChild == null ? new Tvalue[0] : TraverseNode(node.LeftChild, method),
                                 TraverseRight = node.RightChild == null ? new Tvalue[0] : TraverseNode(node.RightChild, method),
                                 Self          = new Tvalue[1] {
                node.Value
            };

            switch (method)
            {
            case DepthFirstTraversalMethod.PreOrder:
                return(Self.Concat(TraverseLeft).Concat(TraverseRight));

            case DepthFirstTraversalMethod.InOrder:
                return(TraverseLeft.Concat(Self).Concat(TraverseRight));

            case DepthFirstTraversalMethod.PostOrder:
                return(TraverseLeft.Concat(TraverseRight).Concat(Self));

            default:
                throw new ArgumentException();
            }
        }
        /// <summary>
        /// Finds and deletes an element.
        /// </summary>
        /// <param name="node">Key to be deleted </param>
        /// <returns>void<Tkey></returns>
        public void DeleteKey(Tkey key)
        {
            BinaryKeyValueNode <Tkey> node = FindNode(key);

            DeleteNode(node);
        }