// <summary>Implements collections.UpdatableCollection.take. Time // complexity: O(log n). Takes the least element. @see // collections.UpdatableCollection#take.</summary> // <returns>A T object.</returns> public T Pop() { if (this.countValue != 0) { RBCell p = this.treeValue.leftmost(); T v = p.element(); this.treeValue = p.delete(this.treeValue); this.decCount(); return(v); } return(default(T)); }
private bool remove_(T element, bool allOccurrences) { var ret = false; while (this.countValue > 0) { RBCell p = this.treeValue.find(element, this.cmpValue); if (p != null) { this.treeValue = p.delete(this.treeValue); this.decCount(); ret = true; if (!allOccurrences) { return(ret); } } else { break; } } return(ret); }
// <summary>Delete the current node, and then rebalance the tree it is // in.</summary> // <param name='root'>The root of the current tree.</param> // <returns>The new root of the current tree. Rebalancing can change // the root.</returns> public RBCell delete(RBCell root) { // if strictly internal, swap contents with successor and then delete it if (this.leftValue != null && this.rightValue != null) { RBCell s = this.successor(); this.copyContents(s); return(s.delete(root)); } // Start fixup at replacement node, if it exists RBCell replacement = this.leftValue ?? this.rightValue; if (replacement != null) { // link replacement to parent replacement.parentValue = this.parentValue; if (this.parentValue == null) { root = replacement; } else if (this == this.parentValue.leftValue) { this.parentValue.leftValue = replacement; } else { this.parentValue.rightValue = replacement; } // null out links so they are OK to use by fixAfterDeletion this.leftValue = null; this.rightValue = null; this.parentValue = null; // fix replacement if (this.colorValue) { root = replacement.fixAfterDeletion(root); } return(root); } if (this.parentValue == null) // exit if we are the only node // if no children, use self as phantom replacement // and then unlink { return(null); } if (this.colorValue) { root = this.fixAfterDeletion(root); } // Unlink (Couldn't before since fixAfterDeletion needs parent ptr) if (this.parentValue != null) { if (this == this.parentValue.leftValue) { this.parentValue.leftValue = null; } else if (this == this.parentValue.rightValue) { this.parentValue.rightValue = null; } this.parentValue = null; } return(root); }