Advertisement
myname0

дераимда

Apr 6th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1.  class Program
  2.     {
  3.        
  4.         public class Treap
  5.         {
  6.             public class Node
  7.             {
  8.                 int key, prior;
  9.                 Node l, r;
  10.                 public Node(int key, int prior)
  11.                 {
  12.                     this.key = key;
  13.                     this.prior = prior;
  14.                     l = null;
  15.                     r = null;
  16.                 }
  17.  
  18.  
  19.  
  20.                 public static void Split(Node t, int key, ref Node l, ref Node r)
  21.                 {
  22.                     if (t == null)
  23.                         l = r = null;
  24.                     else if (key < t.key)
  25.                     {
  26.                         Split(t.l, key, ref l, ref t.l); r = t;
  27.                     }
  28.                     else
  29.                     {
  30.                         Split(t.r, key, ref t.r, ref r); l = t;
  31.                     }
  32.                 }
  33.  
  34.                 public static void Add(ref Node t, Node it)
  35.                 {
  36.                     if (t == null)
  37.                         t = it;
  38.                     else if (it.prior > t.prior)
  39.                     {
  40.                         Split(t, it.key, ref it.l, ref  it.r); t = it;
  41.                     }
  42.                     else
  43.                     {
  44.                         if (it.key < t.key) Add(ref t.l, it);
  45.                         else Add(ref t.r, it);
  46.                         //insert(  it.key < t.key ?  t.l :  t.r, it);
  47.                     }
  48.                 }
  49.  
  50.                 public static void Merge( ref Node t, Node l, Node r)
  51.                 {
  52.                     if (l == null || r == null)
  53.                         t = l == null ? l : r;
  54.                     else if (l.prior > r.prior)
  55.                     {
  56.                         Merge(ref l.r, l.r, r); t = l;
  57.                     }
  58.                     else
  59.                     {
  60.                         Merge(ref r.l, l, r.l); t = r;
  61.                     }
  62.                 }
  63.  
  64.                 public static void Remove(ref Node t, int key)
  65.                 {
  66.                     if (t.key == key)
  67.                         Merge(ref t, t.l, t.r);
  68.                     else
  69.                     {
  70.                         if (key < t.key) Remove(ref t.l, key);
  71.                         else Remove(ref t.r, key);
  72.                         //erase(key < t.key ? t.l : t.r, key);
  73.                     }
  74.                 }
  75.  
  76.                 //public Node Unite(Node l, Node r)
  77.                 //{
  78.                 //    if (l == null || r == null) return l != null ? l : r;
  79.                 //    if (l.prior < r.prior)
  80.                 //    {
  81.                 //        Node tmp = l;
  82.                 //        l = r;
  83.                 //        r = tmp;
  84.                 //    }
  85.                 //    Node lt = null, rt = null;
  86.                 //    Split(r, l.key, ref lt, ref rt);
  87.                 //    l.l = unite(l.l, lt);
  88.                 //    l.r = unite(l.r, rt);
  89.                 //    return l;
  90.                 //}
  91.                 public static void Print (Node tmp)
  92.                 {
  93.                     if (tmp != null)
  94.                     {
  95.                         Console.WriteLine("{0} {1}", tmp.key, tmp.prior);
  96.                         Print(tmp.l);
  97.                         Print(tmp.r);
  98.                            
  99.                     }
  100.  
  101.                 }
  102.             }
  103.  
  104.             Node tree;
  105.             public Treap()
  106.             {
  107.                 tree = null;
  108.             }
  109.  
  110.             public void Add (int x, int y)
  111.             {
  112.                 Node tmp =  new Node(x, y);
  113.                 Node.Add(ref tree, tmp);
  114.             }
  115.  
  116.             public void Remove(int x)
  117.             {
  118.                 Node.Remove(ref tree, x);
  119.             }
  120.             public void Print()
  121.             {
  122.                 Node.Print(tree);
  123.             }
  124.  
  125.         }
  126.         static void Main(string[] args)
  127.         {
  128.             StreamReader fin = new StreamReader("input.txt");
  129.             string temp = fin.ReadToEnd();
  130.             string[] numbers = temp.Split(' ');
  131.             fin.Close();
  132.             Random rand = new Random();
  133.             Treap tr = new Treap();
  134.             foreach (string item in numbers)
  135.                 tr.Add(int.Parse(item),rand.Next(200));
  136.             tr.Print();
  137.  
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement