Example #1
0
 public static void Split(FastTreap tree, ref int val, out FastTreap L, out FastTreap R)
 {
     if (tree.Val <= val)
     {
         if (tree.Right == null)
         {
             R = null;
         }
         else
         {
             Split(tree.Right, ref val, out tree.Right, out R);
         }
         L = tree;
     }
     else
     {
         if (tree.Left == null)
         {
             L = null;
         }
         else
         {
             Split(tree.Left, ref val, out L, out tree.Left);
         }
         R = tree;
     }
 }
Example #2
0
        public static void Remove(ref FastTreap tree, int val)
        {
            if (tree == null || !Contains(tree, val))
            {
                return;
            }

            Split(tree, ref val, out tree, out FastTreap R);
            if (tree.Val == val)
            {
                tree = Merge(tree.Left, R);
                return;
            }
            else
            {
                FastTreap deleted = tree;
                while (deleted.Right.Val != val)
                {
                    deleted = deleted.Right;
                }
                deleted.Right = null;

                tree = Merge(tree, R);
            }
        }
Example #3
0
 public static bool Contains(FastTreap tree, int val)
 {
     while (tree != null)
     {
         if (tree.Val == val)
         {
             return(true);
         }
         tree = val < tree.Val? tree.Left : tree.Right;
     }
     return(false);
 }
Example #4
0
        public static void Add(ref FastTreap tree, int val)
        {
            if (tree == null)
            {
                tree = new FastTreap(val);
                return;
            }
            if (Contains(tree, val))
            {
                return;
            }
            Split(tree, ref val, out tree, out FastTreap R);
            FastTreap node = new FastTreap(val);

            tree = Merge(tree, node);
            tree = Merge(tree, R);
        }
Example #5
0
        private void TestCartesianTree(int[] vals)
        {
            if (dataGridView1.Rows.Count < 2)
            {
                dataGridView1.Rows.Add();
            }
            else
            {
                dataGridView1.Rows[1].Cells[0].Value = "";
                dataGridView1.Rows[1].Cells[1].Value = "";
                dataGridView1.Rows[1].Cells[2].Value = "";
                dataGridView1.Rows[1].Cells[3].Value = "";
            }

            dataGridView1.Rows[1].Cells[0].Value = "Декартовое дерево";

            Stopwatch stopWatch = new Stopwatch();
            FastTreap test = null;

            stopWatch.Restart();
            for (int i = 0; i < vals.Length; i++)
            {
                FastTreap.Add(ref test, vals[i]);
            }
            stopWatch.Stop();
            dataGridView1.Rows[1].Cells[1].Value = stopWatch.ElapsedTicks.ToString();

            stopWatch.Restart();
            for (int i = 0; i < vals.Length; i++)
            {
                FastTreap.Contains(test, vals[i]);
            }
            stopWatch.Stop();
            dataGridView1.Rows[1].Cells[2].Value = stopWatch.ElapsedTicks.ToString();

            stopWatch.Restart();
            for (int i = 0; i < vals.Length; i++)
            {
                FastTreap.Remove(ref test, vals[i]);
            }
            stopWatch.Stop();
            dataGridView1.Rows[1].Cells[3].Value = stopWatch.ElapsedTicks.ToString();
        }
Example #6
0
 public static FastTreap Merge(FastTreap L, FastTreap R)
 {
     if (L == null)
     {
         return(R);
     }
     if (R == null)
     {
         return(L);
     }
     if (L.Priority > R.Priority)
     {
         L.Right = Merge(L.Right, R);
         return(L);
     }
     else
     {
         R.Left = Merge(L, R.Left);
         return(R);
     }
 }