public void Move(int source, int dest) { int value = treap[source]; treap.Delete(source); BaseTreapNode<int> node = treap.InsertNodeInternal(dest, value); map.Insert(dest, (ParentTreapNode<int>)node); }
private BaseTreapNode <T> InsertNode(int idx, BaseTreapNode <T> node, BaseTreapNode <T> tree) { if (tree == null) { return(node); } int leftTreeCount = GetTreeLeftCount(tree); if (idx - leftTreeCount <= 0) { tree.Left = InsertNode(idx, node, tree.Left); if (tree.Left.Priority < tree.Priority) { tree = tree.RotateRight(); } return(tree); } { tree.Right = InsertNode(idx - leftTreeCount - 1, node, tree.Right); if (tree.Right.Priority < tree.Priority) { tree = tree.RotateLeft(); } return(tree); } }
//TODO: copy-paste, parent-setter protected override BaseTreapNode <T> Delete(int idx, BaseTreapNode <T> tNode) { int leftTreeCount = GetTreeLeftCount(tNode); if (idx < leftTreeCount) { tNode.Left = Delete(idx, tNode.Left); tNode.UpdateAggregate(); return(tNode); } if (idx > leftTreeCount) { tNode.Right = Delete(idx - leftTreeCount - 1, tNode.Right); tNode.UpdateAggregate(); return(tNode); } ParentTreapNode <T> node = (ParentTreapNode <T>)tNode; ParentTreapNode <T> parent = node.Parent; tNode = tNode.DeleteRoot(); if (tNode != null) { ((ParentTreapNode <T>)tNode).Parent = parent; } return(tNode); }
public void Delete(int idx) { ValidateIdx(idx); treapTree = Delete(idx, treapTree); count--; }
internal BaseTreapNode <T> InsertNodeInternal(int idx, T data) { BaseTreapNode <T> node = CreateNode(data); treapTree = InsertNode(idx, node, treapTree); count++; return(node); }
public virtual BaseTreapNode <T> RotateLeft() { BaseTreapNode <T> temp = Right; Right = Right.Left; temp.Left = this; return(temp); }
public virtual BaseTreapNode <T> RotateRight() { BaseTreapNode <T> temp = Left; Left = Left.Right; temp.Right = this; return(temp); }
public override BaseTreapNode <T> RotateRight() { ParentTreapNode <T> parent1 = parent; BaseTreapNode <T> node = base.RotateRight(); ((ParentTreapNode <T>)node).Parent = parent1; return(node); }
public void Move(int source, int dest) { T value = treap[source]; treap.Delete(source); BaseTreapNode <T> node = treap.InsertNodeInternal(dest, value); dictionary[value] = (ParentTreapNode <T>)node; }
protected override void RightSetter(BaseTreapNode <T> node) { base.RightSetter(node); ParentTreapNode <T> treapNode = node as ParentTreapNode <T>; if (treapNode != null) { treapNode.Parent = this; } }
public override BaseTreapNode <T> RotateRight() { BaseTreapNode <T> node = base.RotateRight(); TreapNode <T> treapNode = node as TreapNode <T>; if (treapNode != null) { treapNode.UpdateAggregate(); } return(node); }
protected virtual BaseTreapNode <T> Delete(int idx, BaseTreapNode <T> tNode) { int leftTreeCount = GetTreeLeftCount(tNode); if (idx < leftTreeCount) { tNode.Left = Delete(idx, tNode.Left); tNode.UpdateAggregate(); return(tNode); } if (idx > leftTreeCount) { tNode.Right = Delete(idx - leftTreeCount - 1, tNode.Right); tNode.UpdateAggregate(); return(tNode); } tNode = tNode.DeleteRoot(); return(tNode); }
private void Set(BaseTreapNode <T> node, int idx, T value) { int leftTreeCount = GetTreeLeftCount(node); if (idx == leftTreeCount) { node.Value = value; node.UpdateAggregate(); return; } if (idx < leftTreeCount) { Set(node.Left, idx, value); node.UpdateAggregate(); return; } Set(node.Right, idx - (leftTreeCount + 1), value); node.UpdateAggregate(); }
private T Aggregate(BaseTreapNode <T> node, int l, int r) { int leftCount = GetTreeLeftCount(node); if (l == r && l == leftCount) { return(node.Value); } if (l < leftCount && r < leftCount) { return(Aggregate(node.Left, l, r)); } if (l > leftCount && r > leftCount) { int shift = (leftCount + 1); return(Aggregate(node.Right, l - shift, r - shift)); } return(AggregateFromNode((TreapNode <T>)node, l, r)); }
public T this[int idx] { get { ValidateIdx(idx); BaseTreapNode <T> treeNode = treapTree; while (treeNode != null) { int leftTreeCount = treeNode.Left != null ? treeNode.Left.Count : 0; if (idx == leftTreeCount) { return(treeNode.Value); } if (idx < leftTreeCount) { treeNode = treeNode.Left; } else { idx -= leftTreeCount + 1; treeNode = treeNode.Right; } } throw (new ArgumentException("Treap key was not found")); } set { ValidateIdx(idx); Set(treapTree, idx, value); } }
public void Insert(int idx, T value) { BaseTreapNode <T> node = treap.InsertNodeInternal(idx, value); dictionary.Add(value, (ParentTreapNode <T>)node); }
private T AggregateFromNode(TreapNode <T> node, int l, int r) { T res = node.Value; BaseTreapNode <T> lNode = node.Left; while (lNode != null) { int treeLeftCount = GetTreeLeftCount(lNode); if (l < treeLeftCount) { if (lNode.Right != null) { res = monoid.Operation(res, ((TreapNode <T>)lNode.Right).Aggregate); } res = monoid.Operation(res, lNode.Value); lNode = lNode.Left; } else if (l == treeLeftCount) { if (lNode.Right != null) { res = monoid.Operation(res, ((TreapNode <T>)lNode.Right).Aggregate); } res = monoid.Operation(res, lNode.Value); break; } else if (l > treeLeftCount) { lNode = lNode.Right; l -= treeLeftCount + 1; } } BaseTreapNode <T> rNode = node.Right; r -= GetTreeLeftCount(node) + 1; while (r >= 0 && rNode != null) { int treeLeftCount = GetTreeLeftCount(rNode); if (r < treeLeftCount) { rNode = rNode.Left; } else if (r == treeLeftCount) { if (rNode.Left != null) { res = monoid.Operation(res, ((TreapNode <T>)rNode.Left).Aggregate); } res = monoid.Operation(res, rNode.Value); break; } else if (r > treeLeftCount) { if (rNode.Left != null) { res = monoid.Operation(res, ((TreapNode <T>)rNode.Left).Aggregate); } res = monoid.Operation(res, rNode.Value); rNode = rNode.Right; r -= treeLeftCount + 1; } } return(res); }
protected virtual void RightSetter(BaseTreapNode <T> node) { right = node; Update(); }
protected virtual void LeftSetter(BaseTreapNode <T> node) { left = node; Update(); }
public void Insert(int idx, int value) { BaseTreapNode<int> node = treap.InsertNodeInternal(idx, value); map.Insert(idx, (ParentTreapNode<int>)node); }
protected int GetTreeLeftCount(BaseTreapNode <T> tNode) { return(tNode.Left != null ? tNode.Left.Count : 0); }