public Tuple <RbstNode, RbstNode> Split(X k) { //if this needs to go left if (this.val.CompareTo(k) < 0) { if (this.right == null) { return(Tuple.Create(this, (RbstNode)null)); } var x = this.right.Split(k); this.right = x.Item1; this.Update(); return(Tuple.Create(this, x.Item2)); } else { if (this.left == null) { return(Tuple.Create((RbstNode)null, this)); } var x = this.left.Split(k); this.left = x.Item2; this.Update(); return(Tuple.Create(x.Item1, this)); } }
public RbstNode Merge(RbstNode other) { if (other == null) { return(this); } if (PriorityOver(other)) { if (this.right == null) { this.right = other; } else { this.right = this.right.Merge(other); } Update(); return(this); } else { //no need to check if left is null other.left = this.Merge(other.left); other.Update(); return(other); } }
public RbstNode Remove(X k) { int comp = k.CompareTo(this.val); if (comp == 0) { if (this.left == null) { return(this.right); } else { return(this.left.Merge(this.right)); } } if (comp < 0) { if (this.left != null) { this.left = this.left.Remove(k); } } else { if (this.right != null) { this.right = this.right.Remove(k); } } this.Update(); return(this); }
public void Remove(X val) { if (root == null) { return; } else { root = root.Remove(val); } }
public void Insert(X val) { if (root == null) { root = new RbstNode(val); } else { root = root.Insert(new RbstNode(val)); } }
public RbstNode Insert(RbstNode other) { var x = this.Split(other.val); return((x.Item1 != null ? x.Item1.Merge(other) : other).Merge(x.Item2)); }
public bool PriorityOver(RbstNode other) { return((rnd.Next() % (this.size + other.size)) < this.size); }