/// <summary> /// Finds a node recursivly and removes it. /// </summary> /// <param name="key">The key of node.</param> /// <param name="rootOfSubtree">The root node of subtree.</param> /// <returns>returns true if the node was deleted.</returns> private bool RemoveRecursivly(TKey key, RedBlackNode <TKey, TValue> rootOfSubtree) { if (key.CompareTo(rootOfSubtree.Key) > 0) { if (rootOfSubtree.RightChild != null) { return(this.RemoveRecursivly(key, rootOfSubtree.RightChild)); } return(false); } if (key.CompareTo(rootOfSubtree.Key) < 0) { if (rootOfSubtree.LeftChild != null) { return(this.RemoveRecursivly(key, rootOfSubtree.LeftChild)); } return(false); } if (rootOfSubtree.LeftChild == null && rootOfSubtree.RightChild == null) { this.RemoveLeaf(rootOfSubtree); return(true); } if (rootOfSubtree.LeftChild == null) { this.RemoveNodeHavingRightChild(rootOfSubtree); this.CheckViolation(rootOfSubtree.RightChild); return(true); } if (rootOfSubtree.RightChild == null) { this.RemoveNodeHavingLeftChild(rootOfSubtree); this.CheckViolation(rootOfSubtree.LeftChild); return(true); } this.RemoveNode(rootOfSubtree); this.CheckViolation(rootOfSubtree); return(true); }
/// <summary> /// Removes a leaf from the tree. /// </summary> /// <param name="node">The node which will be removed.</param> private void RemoveLeaf(RedBlackNode <TKey, TValue> node) { if (node.Parent == null) { node = null; this.Count--; return; } if (node == node.Parent.LeftChild) { node.Parent.LeftChild = null; this.Count--; return; } node.Parent.RightChild = null; this.Count--; return; }
/// <summary> /// Inserts a new node into the tree. /// </summary> /// <param name="node">The node.</param> public void Add(TKey key, TValue value) { RedBlackNode <TKey, TValue> node = new RedBlackNode <TKey, TValue>(key, value); if (this.root == null) { this.root = node; this.root.Color = Color.Black; this.Count++; return; } if (!this.InsertRecursivly(node, this.root)) { throw new CannotUnloadAppDomainException(); } this.Count++; }
/// <summary> /// Tries to get value of a node. /// </summary> /// <param name="key">The key of the node.</param> /// <param name="value">The value.</param> /// <returns>Returns true if key exists.</returns> public bool TryGetValue(TKey key, out TValue value) { if (this.root == null) { value = default(TValue); return(false); } RedBlackNode <TKey, TValue> temporary = this.SearchNodeRecursivly(key, this.root); if (temporary == null) { value = default(TValue); return(false); } value = temporary.Value; return(true); }
/// <summary> /// Check wheather the tree contains the key-value pair. /// </summary> /// <param name="item">The key-value pair.</param> /// <returns>returns true if the key-value pair exists, else returns false.</returns> public bool Contains(KeyValuePair <TKey, TValue> item) { if (this.root == null) { return(false); } RedBlackNode <TKey, TValue> temporary = this.SearchNodeRecursivly(item.Key, this.root); if (temporary == null) { return(false); } if (temporary.Value.Equals(item.Value)) { return(true); } return(false); }
/// <summary> /// Rotates the subtree. /// </summary> /// <param name="node">The root of subtree.</param> private void Rotate(RedBlackNode <TKey, TValue> node) { if (node == node.Parent.LeftChild) { if (node.Parent == node.Parent.Parent.LeftChild) { this.RightRotation(node.Parent.Parent); node.Parent.Color = Color.Black; node.Color = Color.Red; if (node.Parent.RightChild != null) { node.Parent.RightChild.Color = Color.Red; } return; } this.RightLeftRotation(node.Parent.Parent); node.Color = Color.Black; node.LeftChild.Color = Color.Red; node.RightChild.Color = Color.Red; return; } if (node.Parent == node.Parent.Parent.RightChild) { this.LeftRotation(node.Parent.Parent); node.Parent.Color = Color.Black; node.Color = Color.Red; if (node.Parent.LeftChild != null) { node.Parent.LeftChild.Color = Color.Red; } return; } this.LeftRightRotation(node.Parent.Parent); node.Color = Color.Black; node.LeftChild.Color = Color.Red; node.RightChild.Color = Color.Red; return; }
/// <summary> /// Moves the pointer to the next position. /// </summary> /// <returns>Returns true if the pointer was moved successfully.</returns> public bool MoveNext() { if (this.root == null) { return(false); } if (this.current == null) { this.Reset(); return(true); } this.current = this.InOrderSuccessor(this.current); if (current == null) { return(false); } this.currentPair = new KeyValuePair <TKey, TValue>(this.current.Key, this.current.Value); return(true); }
/// <summary> /// Removes a node which has only right child. /// </summary> /// <param name="node">The node which will be removed.</param> private void RemoveNodeHavingRightChild(RedBlackNode <TKey, TValue> node) { if (node.Parent == null) { this.root = node.RightChild; root.Parent = null; this.Count--; return; } if (node == node.Parent.LeftChild) { node.Parent.LeftChild = node.RightChild; node.RightChild.Parent = node.Parent; this.Count--; return; } node.Parent.RightChild = node.RightChild; node.RightChild.Parent = node.Parent; this.Count--; return; }
/// <summary> /// Searches a node in the tree. /// </summary> /// <param name="key">The key of the node.</param> /// <param name="rootOfSubtree">The root node of the subtree.</param> /// <returns>Returns node if it exists, otherwise null.</returns> private RedBlackNode <TKey, TValue> SearchNodeRecursivly(TKey key, RedBlackNode <TKey, TValue> rootOfSubtree) { if (key.CompareTo(rootOfSubtree.Key) < 0) { if (rootOfSubtree.LeftChild == null) { return(null); } return(this.SearchNodeRecursivly(key, rootOfSubtree.LeftChild)); } if (key.CompareTo(rootOfSubtree.Key) > 0) { if (rootOfSubtree.RightChild == null) { return(null); } return(this.SearchNodeRecursivly(key, rootOfSubtree.RightChild)); } return(rootOfSubtree); }
/// <summary> /// Searches the key in the subtree. /// </summary> /// <param name="key">The key.</param> /// <param name="node">The root of the subtree.</param> /// <returns>Returns true if tne node is added, else returns false.</returns> private bool SearchKeyRecursivly(TKey key, RedBlackNode <TKey, TValue> node) { if (key.CompareTo(node.Key) == 0) { return(true); } if (key.CompareTo(node.Key) < 0) { if (node.LeftChild == null) { return(false); } return(this.SearchKeyRecursivly(key, node.LeftChild)); } if (node.RightChild == null) { return(false); } return(this.SearchKeyRecursivly(key, node.RightChild)); }
/// <summary> /// Returns the next node. /// </summary> /// <param name="node">The current node.</param> /// <returns></returns> private RedBlackNode <TKey, TValue> InOrderSuccessor(RedBlackNode <TKey, TValue> node) { RedBlackNode <TKey, TValue> temporary; if (node.RightChild == null) { if (node.Parent == null) { return(null); } temporary = node; while (temporary.Parent != null) { if (temporary == temporary.Parent.RightChild) { temporary = temporary.Parent; } else { break; } } return(temporary.Parent); } else { temporary = node.RightChild; while (temporary.LeftChild != null) { temporary = temporary.LeftChild; } return(temporary); } }
/// <summary> /// Makes two rotations, at first rotate left the nodes right child, then do left the node. /// </summary> /// <param name="node">The node.</param> private void RightLeftRotation(RedBlackNode <TKey, TValue> node) { RightRotation(node.RightChild); LeftRotation(node); }
/// <summary> /// Makes two rotations, at first rotate left the nodes child, then do right the node. /// </summary> /// <param name="node">The node.</param> private void LeftRightRotation(RedBlackNode <TKey, TValue> node) { this.LeftRotation(node.LeftChild); this.RightRotation(node); }
/// <summary> /// Creates a new empty tree. /// </summary> public RedBlackDictionary() { this.root = null; this.Count = 0; }