myname0

ExampleTreap

Mar 21st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Example_treap
  9. {
  10.     class Program
  11.     {
  12.         static Random rand = new Random();
  13.         public class Treap
  14.         {
  15.             private class Node
  16.             {
  17.                 //public int x;
  18.                 //public int y;
  19.                 public object x;
  20.                 public object y;
  21.  
  22.                 public Node Left;
  23.                 public Node Right;
  24.  
  25.                 public Node(object x, object y, Node left = null, Node right = null)
  26.                 {
  27.                     this.x = x;
  28.                     this.y = y;
  29.                     this.Left = left;
  30.                     this.Right = right;
  31.                 }
  32.            
  33.                 public Node()
  34.                 {      
  35.                     x = null;
  36.                     y = null;
  37.                     Left = null;
  38.                     Right = null;
  39.                 }
  40.            
  41.                 //public Treap (int a, int b)
  42.                 //{
  43.                 //    x = a;
  44.                 //    y = b;
  45.                 //    Left = null;
  46.                 //    Right = null;
  47.                 //}
  48.  
  49.                 public static Node Merge(Node L, Node R)
  50.                 {
  51.                     if (L == null) return R;
  52.                     if (R == null) return L;
  53.  
  54.                     int cmp = ((IComparable)(L.y)).CompareTo(R.y);
  55.                     //if (L.y > R.y)
  56.                     if (cmp > 0)
  57.                     {
  58.                         var newR = Merge(L.Right, R);
  59.                         return new Node(L.x, L.y, L.Left, newR);
  60.                     }
  61.                     else
  62.                     {
  63.                         var newL = Merge(L, R.Left);
  64.                         return new Node(R.x, R.y, newL, R.Right);
  65.                     }
  66.                 }
  67.  
  68.                 public void Split(object x, out Node L, out Node R)
  69.                 {
  70.                     Node newTree = null;
  71.                     int cmp = ((IComparable)(this.x)).CompareTo(x);
  72.                     //if (this.x <= x)
  73.                     if(cmp <= 0)
  74.                     {
  75.                         if (Right == null)
  76.                             R = null;
  77.                         else
  78.                             Right.Split(x, out newTree, out R);
  79.                         L = new Node(this.x, y, Left, newTree);
  80.                     }
  81.                     else
  82.                     {
  83.                         if (Left == null)
  84.                             L = null;
  85.                         else
  86.                             Left.Split(x, out L, out newTree);
  87.                         R = new Node(this.x, y, newTree, Right);
  88.                     }
  89.                 }
  90.  
  91.                 public Node Add(object x)
  92.                 {
  93.                     Node l, r;
  94.                     Split(x, out l, out r);
  95.                     Node m = new Node(x, rand.Next(200));
  96.                     return Merge(Merge(l, m), r);
  97.                 }
  98.  
  99.                 public  Node Remove(object x)
  100.                 {
  101.                     Node l, m, r;
  102.                     Split((int)x - 1, out l, out r);
  103.                     r.Split(x, out m, out r);
  104.                     return Merge(l, r);
  105.                 }
  106.  
  107.                 public void Preorder(Node r)
  108.                 {
  109.                     if (r != null)
  110.                     {
  111.                         Console.Write("{0} ", r.x);
  112.                         Preorder(Left);
  113.                         Preorder(Right);
  114.                     }
  115.                 }
  116.             }
  117.  
  118.             Node tree;
  119.             public Treap()
  120.             {
  121.                 tree = null;
  122.             }
  123.  
  124.             public void Add (object x)
  125.             {
  126.                 tree.Add(x);
  127.             }
  128.  
  129.             public void Remove (object x)
  130.             {
  131.                 tree.Remove(x);
  132.             }
  133.  
  134.             public void Preorder()
  135.             {
  136.                 tree.Preorder(tree);
  137.             }
  138.  
  139.         }
  140.  
  141.  
  142.              
  143.  
  144.                 //public static Treap Build(int[] xs, int[] ys)
  145.                 //{
  146.                 //    Debug.Assert(xs.Length == ys.Length);
  147.  
  148.                 //    var tree = new Treap(xs[0], ys[0]);
  149.                 //    var last = tree;
  150.  
  151.                 //    for (int i = 1; i < xs.Length; ++i)
  152.                 //    {
  153.                 //        if (last.y > ys[i])
  154.                 //        {
  155.                 //            last.Right = new Treap(xs[i], ys[i], parent: last);
  156.                 //            last = last.Right;
  157.                 //        }
  158.                 //        else
  159.                 //        {
  160.                 //            Treap cur = last;
  161.                 //            while (cur.Parent != null && cur.y <= ys[i])
  162.                 //                cur = cur.Parent;
  163.                 //            if (cur.y <= ys[i])
  164.                 //                last = new Treap(xs[i], ys[i], cur);
  165.                 //            else
  166.                 //            {
  167.                 //                last = new Treap(xs[i], ys[i], cur.Right, null, cur);
  168.                 //                cur.Right = last;
  169.                 //            }
  170.                 //        }
  171.                 //    }
  172.  
  173.                 //    while (last.Parent != null)
  174.                 //        last = last.Parent;
  175.                 //    return last;
  176.                 //}
  177.         //public class BinaryTreap
  178.         //{
  179.  
  180.         //    class Node
  181.         //    {
  182.         //        private static Random random = new Random();
  183.  
  184.         //        public object Priority;
  185.         //        public object Key;
  186.  
  187.  
  188.         //        public Node Left, Right;
  189.  
  190.         //        public Node(object key,
  191.         //            Node left = null, Node right = null)
  192.         //        {
  193.         //            Priority = random.Next(); Key = key;
  194.         //            Left = left; Right = right;
  195.         //        }
  196.         //        public Node()
  197.         //        {
  198.         //            Priority = null;
  199.         //            Key = null;
  200.         //            Left = null;
  201.         //            Right = null;
  202.  
  203.         //        }
  204.         //    }
  205.  
  206.  
  207.         //    private Node root;
  208.  
  209.         //    private Node Set(Node root, Node child)
  210.         //    {
  211.         //        if (root == null) return child;
  212.  
  213.         //        int cmp = ((IComparable)child.Key).CompareTo(root.Key);
  214.         //        if (cmp == 0)
  215.         //            return child;
  216.         //        else if (cmp > 0)
  217.         //        {
  218.         //            root.Right = Set(root.Right, child);
  219.         //            int cmp1 = ((IComparable)root.Right.Priority).CompareTo(root.Priority);
  220.         //            if (cmp1 < 0)
  221.         //                root = LRotate(root);
  222.         //        }
  223.         //        else if (cmp < 0)
  224.         //        {
  225.         //            root.Left = Set(root.Left, child);
  226.         //            int cmp2 = ((IComparable)root.Left.Priority).CompareTo(root.Priority);
  227.         //            if (cmp2 < 0)
  228.         //                root = RRotate(root);
  229.         //        }
  230.         //        return root;
  231.         //    }
  232.  
  233.         //    private Node Delete(Node root, int key)
  234.         //    {
  235.         //        if (root == null) return null;
  236.  
  237.         //        int cmp = key.CompareTo(root.Key);
  238.         //        if (cmp < 0) root.Left = Delete(root.Left, key);
  239.         //        if (cmp > 0) root.Right = Delete(root.Right, key);
  240.         //        if (cmp == 0)
  241.         //        {
  242.         //            if (root.Left != null && root.Right != null)
  243.         //            {
  244.         //                int cmp1 = ((IComparable)root.Left.Priority).CompareTo(root.Right.Priority);
  245.         //                if (cmp1 < 0)
  246.         //                    root = RRotate(root);
  247.         //                else
  248.         //                    root = LRotate(root);
  249.         //            }
  250.         //            else if (root.Left != null) root = RRotate(root);
  251.         //            else if (root.Right != null) root = LRotate(root);
  252.         //            else return null;
  253.         //            root = Delete(root, key);
  254.         //        }
  255.         //        return root;
  256.         //    }
  257.  
  258.         //    private Node LRotate(Node root)
  259.         //    {
  260.         //        Node temp = root.Right;
  261.         //        root.Right = temp.Left;
  262.         //        temp.Left = root;
  263.         //        return temp;
  264.         //    }
  265.         //    private Node RRotate(Node root)
  266.         //    {
  267.         //        Node temp = root.Left;
  268.         //        root.Left = temp.Right;
  269.         //        temp.Right = root;
  270.         //        return temp;
  271.         //    }
  272.     //private IEnumerable<Node> InOrder(Node root)
  273.     //{
  274.     //    if (root == null)
  275.     //        yield break;
  276.  
  277.     //    foreach (Node child in InOrder(root.Left).Append(root).Concat(InOrder(root.Right)))
  278.     //        yield return child;
  279.     //}
  280.  
  281.  
  282.            
  283.         //    Treap treap;
  284.         //    public Tree()
  285.         //    {
  286.         //        treap = null;
  287.         //    }
  288.  
  289.         //    private Tree(Treap r)
  290.         //    {
  291.         //        treap = r;
  292.         //    }
  293.         //    public void Add(int a)
  294.         //    {
  295.         //        treap.Add(a);
  296.         //    }
  297.         //  public static void LevelPrint(Treap r)
  298.         //        {
  299.         //            Queue<Treap> queue = new Queue<Treap>();
  300.         //            Console.WriteLine("{0} ", r.x);
  301.         //            int temp = (int)r.x; ;
  302.         //            queue.Enqueue(r);
  303.         //            while (queue.Count != 0)
  304.         //            {
  305.         //                if (temp > (int)queue.Peek().x)
  306.         //                    Console.WriteLine();
  307.         //                temp = (int)queue.Peek().x;
  308.         //                if (queue.Peek().Left != null)
  309.         //                {
  310.         //                    Console.Write("{0} ", queue.Peek().Left.x);
  311.         //                    queue.Enqueue(queue.Peek().Left);
  312.  
  313.         //                }
  314.         //                if (queue.Peek().Right != null)
  315.         //                {
  316.         //                    Console.Write("{0} ", queue.Peek().Right.x);
  317.         //                    queue.Enqueue(queue.Peek().Right);
  318.         //                }
  319.         //                queue.Dequeue();
  320.         //            }
  321.         //        }
  322.         //}
  323.  
  324.         static void Main(string[] args)
  325.         {
  326.             StreamReader fin = new StreamReader("input.txt");
  327.             string temp = fin.ReadToEnd();
  328.             string[] numbers = temp.Split(' ');
  329.             fin.Close();
  330.             //Treap treap = new Treap(4, 3);
  331.             //foreach (string item in numbers)
  332.             //    treap.Add(int.Parse(item));
  333.             //treap.Preorder(treap);
  334.             ////Treap.LevelPrint(treap);
  335.      //var t = new BinaryTreap <int, int>();
  336.             Treap  tr = new Treap();
  337.             foreach (string item in numbers)
  338.                 tr.Add(int.Parse(item));
  339.             tr.Preorder();
  340.         }
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment