private RedBlackNode <T> add(RedBlackNode <T> current, T value) { if (current == null) { return(new RedBlackNode <T>(value)); } //if a 4-node is found, split it if (current.Left?.Red == true && current.Right?.Red == true) { FlipColor(current); } //if the current's value is greater than that of the Tvalue, Tvalue will continue along Left if (current.Value.CompareTo(value) >= 0) { current.Left = add(current.Left, value); } //if the current's value is less than that of the Tvalue, Tvalue will continue along Right else { current.Right = add(current.Right, value); } //rotate left to make the 3-node left leaning if (isBlack(current.Left) && current.Right?.Red == true) { current = RotateLeft(current); } //rotate right to correct the unbalanced 4-node if (current.Left?.Red == true && current.Left?.Left?.Red == true) { current = RotateRight(current); } return(current); }
private RedBlackNode <T> remove(RedBlackNode <T> current, T value) { if (Root == null) { return(null); } else { //searching left if (current.Value.CompareTo(value) > 0) { if (current.Left != null) { if (isBlack(current.Left) == true && isBlack(current.Left.Left) == true) { current = MoveRedLeft(current); } current.Left = remove(current.Left, value); } } //searching right/value found else { if (isBlack(current.Left) == false) { current = RotateRight(current); } if (current.Value.CompareTo(value) == 0 && current.Left == null && current.Right == null) { return(null); } if (isBlack(current.Right) == true && isBlack(current.Right?.Left) == true) { current = MoveRedRight(current); } if (current.Value.CompareTo(value) == 0) { RedBlackNode <T> temp = current.Right; while (temp.Left != null) { temp = temp.Left; } current.Value = temp.Value; current.Right = remove(current.Right, temp.Value); } else if (current?.Right != null) { current.Right = remove(current.Right, value); } } } return(FixUp(current)); }
public void Add(T value) { Root = add(Root, value); Root.Red = false; }
public void Remove(T value) { Root = remove(Root, value); Root.Red = false; }