public void GivenEmptyTreapWhenCallingContainsThenReturnFalse()
        {
            var treap = new Treap <int>();

            Assert.False(treap.Contains(1));
            Assert.False(treap.Contains(-1));
        }
        public void GivenTreapWhenLookingForNonExistingElementThenContainsFalse()
        {
            var collection = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
            };
            var treap = new Treap <int>(collection);

            Assert.False(treap.Contains(0));
            Assert.False(treap.Contains(-1));
            Assert.False(treap.Contains(21));
        }
示例#3
0
    public static void Main()
    {
        int         n     = int.Parse(Console.ReadLine());
        Treap <int> treap = new Treap <int>();

        for (int i = 0; i < n; i++)
        {
            var query = Console.ReadLine().Split();
            if (query[0] == "print")
            {
                Console.WriteLine(treap);
                continue;
            }
            var key = int.Parse(query[1]);
            if (query[0] == "find")
            {
                Console.WriteLine(treap.Contains(key) ? "yes" : "no");
                continue;
            }
            if (query[0] == "delete")
            {
                treap.Remove(key);
                continue;
            }
            if (query[0] == "insert")
            {
                treap.Insert(key, int.Parse(query[2]));
            }
        }
    }
示例#4
0
    static void Main()
    {
        var n = int.Parse(Console.ReadLine());

        var r   = new List <int>();
        var set = new Treap <int>();

        for (int i = 0; i < n; i++)
        {
            var q = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            if (q[0] == 0)
            {
                set.Add(q[1]);
                r.Add(set.Count);
            }
            else if (q[0] == 1)
            {
                r.Add(set.Contains(q[1]) ? 1 : 0);
            }
            else if (q[0] == 2)
            {
                set.Remove(q[1]);
            }
            else
            {
                r.AddRange(set.GetItems(x => x >= q[1], x => x <= q[2]));
            }
        }
        Console.WriteLine(string.Join("\n", r));
    }
        public void GivenTreapWhenLookingForExistingElementThenContainsTrue()
        {
            var collection = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
            };

            var treap = new Treap <int>(collection);

            foreach (var item in collection)
            {
                Assert.True(treap.Contains(item));
            }
        }
示例#6
0
		public static void Test(){
			const int Count = 1*1000;

			var treap = new Treap<int>();
			Assert.AreEqual(treap.Count, 0);

			var rand = new Random(1);
			var items = Enumerable.Range(0, Count *2).Select(n=> rand.Next()).Distinct().Take(Count).ToArray();


			for(int i=0;i<items.Length;i++){
				treap.Add(items[i]);
				Assert.AreEqual(i+1, treap.Count);
				for(int j= 0;j<=i;j++)
					Assert.IsTrue(treap.Contains(items[j]));
			}
			Assert.LessOrEqual(treap.Count, Count);

			var x = treap.First();
			int count =1;
			foreach(var item in treap.Skip(1)){
				Assert.Greater(item, x);
				x = item;
				count++;
			}
			Assert.AreEqual(count, treap.Count);

			foreach(var item in items){
				int v;
				Assert.IsTrue(treap.TryGetValue(item, out v));
				Assert.AreEqual(item, v);
			}

			count = treap.Count;
			for(int i=0;i<items.Length;i++){
				treap.Remove(items[i]);
				count--;

				Assert.AreEqual(count, treap.Count);
				for(int j= 0;j<=i;j++)
					Assert.IsFalse(treap.Contains(items[j]));
			}

		}
示例#7
0
		public static void TestPerformance(){
			const int Count = 1*1000*1000;
			var rand = new Random(1);
			var w = Stopwatch.StartNew();
			for(int i=0;i<Count;i++)
				rand.Next();
			Console.WriteLine("rand " + w.Elapsed);

			var items = Enumerable.Range(0, Count).Select(n=>rand.Next()).ToArray();
			var treap = new Treap<int>();
			w = Stopwatch.StartNew();
			foreach(var item in items)
				treap.Add(item);
			Console.WriteLine("insert " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items)
				treap.Contains(item);
			Console.WriteLine("lookup " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items){
				int v;
				treap.TryGetValue(item, out v);
			}
			Console.WriteLine("tryget " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items)
				treap.Remove(item);
			Console.WriteLine("remove " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items)
			{ //
			}
			Console.WriteLine("iterate " + w.Elapsed);

		}