private bool HandleRemovingRootNode(T element) { if (this.root == null) { return(false); } if (this.root.Value.CompareTo(element) == 0) { if (this.root.RightNode == null) { this.root = this.root.LeftNode; return(true); } else { if (this.root.RightNode.LeftNode == null) { this.root.RightNode.LeftNode = this.root.LeftNode; this.root = this.root.RightNode; return(true); } OrderedSetNode <T> replaceNode = this.root.RightNode.FindReplacingNode(); this.root.Value = replaceNode.Value; return(true); } } else { return(this.root.Remove(element)); } }
private static bool HandleRemovingNode(ref OrderedSetNode <T> node, T element) { if (node == null) { return(false); } if (node.Value.CompareTo(element) == 0) { if (node.RightNode == null) { node = node.LeftNode; return(true); } OrderedSetNode <T> replaceNode; if (node.RightNode.LeftNode == null) { node.RightNode.LeftNode = node.LeftNode; node = node.RightNode; return(true); } replaceNode = node.RightNode.FindReplacingNode(); node.Value = replaceNode.Value; return(true); } else { return(node.Remove(element)); } }
public static void BalanceNode(ref OrderedSetNode <T> node, int balance) { if (balance < 0) { if (node.RightNode.Balance < 0) { OrderedSetNode <T> root = node.PerformLeftRotation(); node = root; } else { OrderedSetNode <T> rightSubtreeRoot = node.RightNode.PerformRightRotation(); node.RightNode = rightSubtreeRoot; OrderedSetNode <T> root = node.PerformLeftRotation(); node = root; } } else { if (node.LeftNode.Balance < 0) { OrderedSetNode <T> leftSubtreeRoot = node.LeftNode.PerformLeftRotation(); node.LeftNode = leftSubtreeRoot; OrderedSetNode <T> root = node.PerformRightRotation(); node = root; } else { OrderedSetNode <T> root = node.PerformRightRotation(); node = root; } } }
private void CheckBalance() { int balance = this.root.Balance; if (Math.Abs(balance) > 1) { OrderedSetNode <T> .BalanceNode(ref this.root, balance); } }
private OrderedSetNode <T> PerformRightRotation() { OrderedSetNode <T> root = this.LeftNode; this.LeftNode = root.RightNode; root.RightNode = this; return(root); }
public void Add(T element) { if (this.root == null) { this.root = new OrderedSetNode <T>(element); } else { this.root.Add(element); } this.Count++; CheckBalance(); }