private void InsertionBalance(TreeSet <T> .Node current, ref TreeSet <T> .Node parent, TreeSet <T> .Node grandParent, TreeSet <T> .Node greatGrandParent) { bool flag = grandParent.Right == parent; bool flag2 = parent.Right == current; TreeSet <T> .Node node; if (flag == flag2) { node = (flag2 ? TreeSet <T> .RotateLeft(grandParent) : TreeSet <T> .RotateRight(grandParent)); } else { node = (flag2 ? TreeSet <T> .RotateLeftRight(grandParent) : TreeSet <T> .RotateRightLeft(grandParent)); parent = greatGrandParent; } grandParent.IsRed = true; node.IsRed = false; this.ReplaceChildOfNodeOrRoot(greatGrandParent, grandParent, node); }
public bool Remove(T item) { if (this.root == null) { return(false); } TreeSet <T> .Node node = this.root; TreeSet <T> .Node node2 = null; TreeSet <T> .Node node3 = null; TreeSet <T> .Node node4 = null; TreeSet <T> .Node parentOfMatch = null; bool flag = false; while (node != null) { if (TreeSet <T> .Is2Node(node)) { if (node2 == null) { node.IsRed = true; } else { TreeSet <T> .Node node5 = TreeSet <T> .GetSibling(node, node2); if (node5.IsRed) { if (node2.Right == node5) { TreeSet <T> .RotateLeft(node2); } else { TreeSet <T> .RotateRight(node2); } node2.IsRed = true; node5.IsRed = false; this.ReplaceChildOfNodeOrRoot(node3, node2, node5); node3 = node5; if (node2 == node4) { parentOfMatch = node5; } node5 = ((node2.Left == node) ? node2.Right : node2.Left); } if (TreeSet <T> .Is2Node(node5)) { TreeSet <T> .Merge2Nodes(node2, node, node5); } else { TreeRotation treeRotation = TreeSet <T> .RotationNeeded(node2, node, node5); TreeSet <T> .Node node6 = null; switch (treeRotation) { case TreeRotation.LeftRotation: node5.Right.IsRed = false; node6 = TreeSet <T> .RotateLeft(node2); break; case TreeRotation.RightRotation: node5.Left.IsRed = false; node6 = TreeSet <T> .RotateRight(node2); break; case TreeRotation.RightLeftRotation: node6 = TreeSet <T> .RotateRightLeft(node2); break; case TreeRotation.LeftRightRotation: node6 = TreeSet <T> .RotateLeftRight(node2); break; } node6.IsRed = node2.IsRed; node2.IsRed = false; node.IsRed = true; this.ReplaceChildOfNodeOrRoot(node3, node2, node6); if (node2 == node4) { parentOfMatch = node6; } } } } int num = flag ? -1 : this.comparer.Compare(item, node.Item); if (num == 0) { flag = true; node4 = node; parentOfMatch = node2; } node3 = node2; node2 = node; if (num < 0) { node = node.Left; } else { node = node.Right; } } if (node4 != null) { this.ReplaceNode(node4, parentOfMatch, node2, node3); this.count--; } if (this.root != null) { this.root.IsRed = false; } this.version++; return(flag); }