Ejemplo n.º 1
0
 public Treap(int x)
 {
     this.x = x;
     y      = rand.Next(500);
     Left   = null;
     Right  = null;
 }
Ejemplo n.º 2
0
        public void Split(int x, out Treap L, out Treap R)
        {
            Treap newTree = null;

            if (this.x <= x)
            {
                if (Right == null)
                {
                    R = null;
                }
                else
                {
                    Right.Split(x, out newTree, out R);
                }
                L = new Treap(this.x, y, Left, newTree);
            }
            else
            {
                if (Left == null)
                {
                    L = null;
                }
                else
                {
                    Left.Split(x, out L, out newTree);
                }
                R = new Treap(this.x, y, newTree, Right);
            }
        }
Ejemplo n.º 3
0
 private Treap(int x, int y, Treap left = null, Treap right = null)
 {
     this.x = x;
     this.y = y;
     Left   = left;
     Right  = right;
 }
Ejemplo n.º 4
0
 public void Update(Treap final)
 {
     x     = final.x;
     y     = final.y;
     Left  = final.Left;
     Right = final.Right;
 }
Ejemplo n.º 5
0
        public Treap Add(int x)
        {
            Treap final;
            Treap l, r;

            Split(x, out l, out r);
            Treap m = new Treap(x, rand.Next(500));

            final = Merge(Merge(l, m), r);
            Update(final);
            return(final);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            Treap treap = new Treap(5);

            treap.Add(7);
            treap.Add(8);
            treap.Add(10);
            treap.Add(11);
            treap.Add(9);
            Console.WriteLine(treap);
            treap.Remove(10);
            Console.WriteLine("\n\n");
            Console.WriteLine(treap);
            Console.ReadKey();
        }
Ejemplo n.º 7
0
        private void Traverse(Treap node, ref string info, int n = 0)
        {
            if (node == null)
            {
                return;
            }

            Traverse(node.Right, ref info, n + 5);

            string temp = "";

            for (int i = 0; i < n; ++i)
            {
                temp += "  ";
            }
            info += temp + node.x + ";" + node.y + " <" + "\n";


            Traverse(node.Left, ref info, n + 5);
        }
Ejemplo n.º 8
0
        public static Treap Merge(Treap L, Treap R)
        {
            if (L == null)
            {
                return(R);
            }
            if (R == null)
            {
                return(L);
            }

            if (L.y > R.y)
            {
                var newR = Merge(L.Right, R);
                return(new Treap(L.x, L.y, L.Left, newR));
            }
            else
            {
                var newL = Merge(L, R.Left);
                return(new Treap(R.x, R.y, newL, R.Right));
            }
        }