Esempio n. 1
0
        public static Treap <T> Insert(Treap <T> tree, T data, int priority)
        {
            if (tree == null)
            {
                return(new Treap <T>(data, priority));
            }

            if (data.CompareTo(tree.Data) <= 0)
            {
                tree.Left = Insert(tree.Left, data, priority);

                if (tree.Left.Priority > tree.Priority)
                {
                    tree = RotateRight(tree);
                }
            }
            else if (data.CompareTo(tree.Data) > 0)
            {
                tree.Right = Insert(tree.Right, data, priority);

                if (tree.Right.Priority > tree.Priority)
                {
                    tree = LeftRotate(tree);
                }
            }

            return(tree);
        }
Esempio n. 2
0
        public static Treap <T> RotateRight(Treap <T> y)
        {
            var x = y.Left; var xSubTree = x.Right;

            x.Right = y; y.Left = xSubTree;

            return(x);
        }
Esempio n. 3
0
        public static Treap <T> LeftRotate(Treap <T> x)
        {
            var y = x.Right; var ySubTree = y.Left;

            y.Left  = x;
            x.Right = ySubTree;

            return(y);
        }