/// <summary> ///getting enumerator /// </summary> /// <returns></returns> public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator() { if (RBroot != RBLeaf.Instance()) { RBNode elem = RBroot; Stack <RBNode> nodes = new Stack <RBNode>(); bool elemsOnLeft = true; nodes.Push(elem); while (nodes.Count > 0) { if (elemsOnLeft) { while (elem.left != RBLeaf.Instance()) { nodes.Push(elem); elem = elem.left; } } yield return(new KeyValuePair <TKey, TValue>(elem.key, elem.value)); if (elem.right == RBLeaf.Instance()) { elem = nodes.Pop(); elemsOnLeft = false; } else { elem = elem.right; elemsOnLeft = true; } } } }
/// <summary> /// searching minimum element in tree. /// </summary> /// <param name="elem"></param> /// <returns></returns> private RBNode RBMin(RBNode elem) { while (elem.left != RBLeaf.Instance()) { elem = elem.right; } return(elem); }
/// <summary> /// adding key and value /// </summary> /// <param name="elemKey"></param> /// <param name="elemVal"></param> public void Add(TKey elemKey, TValue elemVal) { RBNode newNode = new RBNode(); newNode.key = elemKey; newNode.value = elemVal; newNode.left = RBLeaf.Instance(); newNode.right = RBLeaf.Instance(); Add(newNode); count++; entries.Add(newNode); }
/// <summary> /// Moving two nodes /// </summary> /// <param name="v1"></param> /// <param name="v2"></param> private void RBMove(RBNode v1, RBNode v2) { if (v1.parent == RBLeaf.Instance()) { RBroot = v2; } else if (v1 == v1.parent.left) { v1.parent.left = v2; } else { v1.parent.right = v2; } v2.parent = v1.parent; }
/// <summary> /// adding node /// </summary> /// <param name="elem"></param> private void Add(RBNode elem) { RBNode temp1 = RBLeaf.Instance(); RBNode temp2 = RBroot; while (temp2 != RBLeaf.Instance()) { temp1 = temp2; if (elem.key.CompareTo(temp2.key) < 0) { temp2 = temp2.left; } else if (elem.key.CompareTo(temp2.key) > 0) { temp2 = temp2.right; } else if (elem.key.CompareTo(temp2.key) == 0) { return; } } elem.parent = temp1; if (temp1 == RBLeaf.Instance()) { RBroot = elem; } else if (elem.key.CompareTo(temp1.key) < 0) { temp1.left = elem; } else if (elem.key.CompareTo(temp1.key) > 0) { temp1.right = elem; } else if (elem.key.CompareTo(temp1.key) == 0) { return; } elem.left = RBLeaf.Instance(); elem.right = RBLeaf.Instance(); elem.color = Color.red; elem = RBFixInsertion(elem); }
/// <summary> /// deleting node /// </summary> /// <param name="elem"></param> private void RBDelete(RBNode elem) { RBNode temp1; RBNode temp2 = elem; Color tempColor = temp2.color; if (elem.left == RBLeaf.Instance()) { temp1 = elem.right; RBMove(elem, elem.right); } else if (elem.right == RBLeaf.Instance()) { temp1 = elem.left; RBMove(elem, elem.left); } else { temp2 = RBMin(elem.right); tempColor = temp2.color; temp1 = temp2.right; if (temp2.parent == elem) { temp1.parent = temp2; } else { RBMove(temp1, temp2.right); temp2.right = elem.right; temp2.right.parent = temp2; } RBMove(elem, temp2); temp2.left = elem.left; temp2.left.parent = temp2; temp2.color = elem.color; } if (tempColor == Color.black) { RBFixDeletion(temp1); } }
/// <summary> /// Getting node with key /// </summary> /// <param name="elemKey"></param> /// <returns></returns> private RBNode GetNode(TKey elemKey) { RBNode node = RBroot; while (node != RBLeaf.Instance()) { if (elemKey.CompareTo(node.key) < 0) { node = node.left; } else if (elemKey.CompareTo(node.key) > 0) { node = node.right; } else { return(node); } } return(null); }
/// <summary> /// parameterless constructor /// </summary> public RBTree() { RBroot = RBLeaf.Instance(); entries = new List <RBNode>(count); }