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);
 }
示例#2
0
        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);
            }
        }
示例#3
0
        //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);
        }
示例#4
0
        public void Delete(int idx)
        {
            ValidateIdx(idx);

            treapTree = Delete(idx, treapTree);
            count--;
        }
示例#5
0
        internal BaseTreapNode <T> InsertNodeInternal(int idx, T data)
        {
            BaseTreapNode <T> node = CreateNode(data);

            treapTree = InsertNode(idx, node, treapTree);
            count++;
            return(node);
        }
示例#6
0
        public virtual BaseTreapNode <T> RotateLeft()
        {
            BaseTreapNode <T> temp = Right;

            Right     = Right.Left;
            temp.Left = this;
            return(temp);
        }
示例#7
0
        public virtual BaseTreapNode <T> RotateRight()
        {
            BaseTreapNode <T> temp = Left;

            Left       = Left.Right;
            temp.Right = this;
            return(temp);
        }
示例#8
0
        public override BaseTreapNode <T> RotateRight()
        {
            ParentTreapNode <T> parent1 = parent;
            BaseTreapNode <T>   node    = base.RotateRight();

            ((ParentTreapNode <T>)node).Parent = parent1;
            return(node);
        }
示例#9
0
        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;
        }
示例#10
0
        protected override void RightSetter(BaseTreapNode <T> node)
        {
            base.RightSetter(node);
            ParentTreapNode <T> treapNode = node as ParentTreapNode <T>;

            if (treapNode != null)
            {
                treapNode.Parent = this;
            }
        }
示例#11
0
        public override BaseTreapNode <T> RotateRight()
        {
            BaseTreapNode <T> node      = base.RotateRight();
            TreapNode <T>     treapNode = node as TreapNode <T>;

            if (treapNode != null)
            {
                treapNode.UpdateAggregate();
            }

            return(node);
        }
示例#12
0
        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);
        }
示例#13
0
        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();
        }
示例#14
0
        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));
        }
示例#15
0
        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);
            }
        }
示例#16
0
        public void Insert(int idx, T value)
        {
            BaseTreapNode <T> node = treap.InsertNodeInternal(idx, value);

            dictionary.Add(value, (ParentTreapNode <T>)node);
        }
示例#17
0
        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);
        }
示例#18
0
 protected virtual void RightSetter(BaseTreapNode <T> node)
 {
     right = node;
     Update();
 }
示例#19
0
 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);
 }
示例#21
0
 protected int GetTreeLeftCount(BaseTreapNode <T> tNode)
 {
     return(tNode.Left != null ? tNode.Left.Count : 0);
 }