void Solve() { /*var hoge = new List<int>(); * for (var i = 0; i < 20; i++) hoge.Add(Treap<int>.rand.Next(100)); * var tr = new Treap<int>(); * foreach (var x in hoge) tr.Insert(x); * WriteLine("hoge = " + string.Join(", ", hoge)); * WriteLine("tr = " + string.Join(", ", tr)); * hoge.Sort(); * WriteLine("hoge = " + string.Join(", ", hoge)); * WriteLine(tr); * var a = hoge[Treap<int>.rand.Next(20)]; * tr.Remove(a); * WriteLine($"removed {a}"); * WriteLine(tr);*/ var Q = F; var tr = new Treap <int>(); while (Q-- > 0) { var I = G; if (I[0] == 1) { tr.Insert(I[1]); } else { var x = tr.At(I[1] - 1); WriteLine(x.Value); tr.Remove(x.Value); } } }
public static void TestTreapCreation() { var init = new List <int[]> { new int[] { 7, 10 }, new int[] { 4, 6 }, new int[] { 13, 8 }, new int[] { 2, 4 }, new int[] { 3, 3 }, new int[] { 0, 3 }, new int[] { 11, 3 }, new int[] { 5, 1 }, new int[] { 14, 4 }, new int[] { 9, 7 }, new int[] { 6, 2 } }; var treap = new Treap(init); Assert.AreEqual(treap.Root.Key, 7); Assert.AreEqual(treap.Root.Priority, 10); Assert.AreEqual(treap.Root.LeftChild.Key, 4); Assert.AreEqual(treap.Root.LeftChild.Priority, 6); Assert.AreEqual(treap.Root.RightChild.Key, 13); Assert.AreEqual(treap.Root.RightChild.Priority, 8); Assert.AreEqual(treap.Root.RightChild.LeftChild.Key, 9); Assert.AreEqual(treap.Root.RightChild.LeftChild.Priority, 7); Assert.IsNull(treap.Root.RightChild.LeftChild.LeftChild); Assert.AreEqual(treap.Root.RightChild.LeftChild.RightChild.Key, 11); Assert.AreEqual(treap.Root.RightChild.LeftChild.RightChild.Priority, 3); }
public void GivenEmptyTreapWhenCallingContainsThenReturnFalse() { var treap = new Treap <int>(); Assert.False(treap.Contains(1)); Assert.False(treap.Contains(-1)); }
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 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])); } } }
static void Main(string[] args) { Treap treap = new Treap(); using (TextReader reader = File.OpenText(@"archive.in")) { int n, q; string text = reader.ReadLine(); string[] bits = text.Split(' '); n = int.Parse(bits[0]); q = int.Parse(bits[1]); for (int i = 1; i <= n; i++) { treap.Add(i); } for (int i = 0; i < q; i++) { text = reader.ReadLine(); bits = text.Split(' '); int left = int.Parse(bits[0]); int right = int.Parse(bits[1]); treap.Request(left, right); } } treap.WriteAnswer(); using (StreamWriter file = new StreamWriter(@"archive.out")) { foreach(int number in ints) { file.Write(number + " "); } } }
public StatnyUrad(string nazov) { this.Nazov = nazov; TreapObyvatelia = new Treap<Obcan, string>(); TreapKatastralneUzemia = new Treap<KatastralneUzemie, int>(); TreapKatastralneUzemia2 = new Treap<KatastralneUzemie, string>(); }
// Public Remove // Removes the given item from the Treap // Calls Private Remove to carry out the actual removal // Expected time complexity: O(log n) // Union operator takes two treaps and returns the union as a treap // expected time complexity O(m log(n/m)) public Treap <T> Union(Treap <T> t1, Treap <T> t2) { if (t1.Root == null) { return(t2); } if (t2.Root == null) { return(t1); } if (t1.Root.Priority < t2.Root.Priority) { Node <T> temp; temp = t2.Root; t2.Root = t1.Root; t1.Root = temp; } t2.Remove(t1.Root.Item); Tuple <Treap <T>, Treap <T> > myTreaps = t2.Split(t1.Root.Item); Treap <T> temp1 = new Treap <T>(); Treap <T> temp2 = new Treap <T>(); temp1.Root = t1.Root.Left; temp2.Root = t1.Root.Right; t1.Root.Left = Union(temp1, myTreaps.Item2).Root; t1.Root.Right = Union(temp2, myTreaps.Item1).Root; return(t1); }
public void TestExercise7ab() { var t = new Treap(); t.Insert(8, 2); t.Insert(9, 3); t.Insert(2, 1); t.Insert(5, 7); t.Insert(3, 8); t.Insert(1, 4); t.Insert(4, 5); t.Insert(6, 6); // Insert (7,0) Console.WriteLine("Before Insert of (7,0)"); t.Print(); Console.WriteLine(); t.Insert(7, 0); Console.WriteLine("After Insert of (7,0)"); t.Print(); Console.WriteLine(); // Delete (8,2) Console.WriteLine("Delete of (8,2)"); t.Delete(8); t.Print(); Console.WriteLine(); }
public void Deletepriotesttreevomscript() { var t = new Treap(); t.Insert(7, 6); t.Insert(5, 9); t.Insert(11, 10); t.Insert(3, 11); t.Insert(6, 13); t.Insert(1, 18); t.Insert(9, 12); t.Insert(14, 14); t.Insert(8, 15); t.Insert(12, 15); t.Insert(15, 17); //before Delete Console.WriteLine("Before Delete (7)"); t.Print(); t.Delete(7); Console.WriteLine("After Delete (7)"); //after delete t.Print(); }
public static int Solve(int[] a, int d) { int result = 0; Treap treap = new Treap(); for (int i = 0; i < a.Length && i < d; i++) { treap.Add(a[i]); } int median; for (int i = d; i < a.Length; i++) { if (d % 2 == 1) { median = treap.Get(d / 2 + 1) * 2; } else { median = treap.Get(d / 2) + treap.Get(d / 2 + 1); } if (a[i] >= median) { result++; } treap.Add(a[i]); treap.Remove(a[i - d]); } return(result); }
public KatastralneUzemie(int cisloUzemia, string meno, StatnyUrad s) { TreapListyVlastnictva = new Treap<ListVlastnictva, int>(); TreapNehnutelnosti = new Treap<Nehnutelnost, int>(); aCisloUzemia = cisloUzemia; aMenoUzemia = meno; m_StatnyUrad = s; }
public static string ConvertTreeToString(this Treap <char> treap) { if (treap == null) { return(string.Empty); } return($"({ConvertTreeToString(treap.Left)}{treap.Data}/{treap.Priority}{ConvertTreeToString(treap.Right)})"); }
public void Inserttest() { var t = new Treap(); Assert.IsTrue(t.Insert(10)); Assert.IsTrue(t.Insert(8)); Assert.IsTrue(t.Insert(3)); Assert.IsFalse(t.Insert(3)); }
public void GivenTreapWhenCallWrongIndexerThenArgumentRangeException() { var treap = new Treap <int>(); var list = new List <int>(); Assert.Throws <ArgumentOutOfRangeException>(() => treap[-1]); Assert.Throws <ArgumentOutOfRangeException>(() => treap[0]); Assert.Throws <ArgumentOutOfRangeException>(() => treap[1]); }
// MakeEmpty // Creates an empty Treap // Split returns a tuple where item 1 is all values greater than x // and item 2 is all values less than x // expected time complexity is O(log(n)) public Tuple <Treap <T>, Treap <T> > Split(T x) { this.AddMax(x); Treap <T> t1 = new Treap <T>(); Treap <T> t2 = new Treap <T>(); t1.Root = this.Root.Right; t2.Root = this.Root.Left; return(new Tuple <Treap <T>, Treap <T> >(t1, t2)); }
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)); }
public void Deletetest() { var t = new Treap(); t.Insert(10); t.Insert(1); t.Insert(15); t.Insert(7); t.Insert(8); t.Insert(4); Assert.IsTrue(t.Delete(10)); }
public void printtest() { var t = new Treap(); t.Insert(10); t.Insert(1); t.Insert(15); t.Insert(7); t.Insert(8); t.Insert(4); t.Print(); }
public void GivenCollectionWhenCreateTreapWithItThenIndexerReturnRightValues() { var collection = new List <int> { 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; var treap = new Treap <int>(collection); for (int i = 0; i < collection.Count; i++) { Assert.Equal(collection[i], treap[i]); } }
public void GivenNonEmptyCollectionWhenSpecifyMaxComparerThenOrdererMaxToMin() { var comparer = Comparer <int> .Create((x, y) => y.CompareTo(x)); var collection = new List <int> { 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; var treap = new Treap <int>(collection, comparer); Assert.Equal(20, treap[0]); Assert.Equal(1, treap[treap.Count - 1]); }
public void GivenEmptyCollectionWhenSpecifyMaxComparerThenOrdererMaxToMin() { var comparer = Comparer <int> .Create((x, y) => y.CompareTo(x)); var treap = new Treap <int>(comparer); treap.Add(1); treap.Add(20); treap.Add(10); treap.Add(5); Assert.Equal(20, treap[0]); Assert.Equal(1, treap[treap.Count - 1]); }
public void GivenEmptyTreapWhenAddElementsThenElementsAdded() { var treap = new Treap <int>(); treap.Add(1); treap.Add(2); treap.Add(3); treap.Add(4); Assert.Equal(1, treap[0]); Assert.Equal(2, treap[1]); Assert.Equal(3, treap[2]); Assert.Equal(4, treap[3]); }
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)); } }
public void GivenTreapWhenRemoveThenReturnProperResult() { var treap = new Treap <int>(new List <int> { 1, 2, 3, 3, 3, 4, 5, 6, 7, 8, 11, 100, 1000 }); Assert.True(treap.Remove(3)); Assert.True(treap.Remove(3)); Assert.True(treap.Remove(3)); Assert.False(treap.Remove(3)); Assert.True(treap.Remove(11)); Assert.True(treap.Remove(1000)); }
public void GivenTreapWhenAddingAndRemovingElementsThenCountStaysRight() { var treap = new Treap <int>(); Assert.Empty(treap); treap.Add(4); Assert.Single(treap); treap.Add(1); Assert.Equal(2, treap.Count); treap.Remove(4); Assert.Single(treap); treap.Remove(1); Assert.Empty(treap); }
public Treap <int> TestTreap() { Treap <int> keep = new Treap <int>(); for (int i = 0; i < count; ++i) { keep.Add(i); } for (int i = 0; i < count; ++i) { keep.Add(~i); } return(keep); }
static void TreapTest() { Treap <int> nums = new Treap <int>(); Random rand = new Random(); for (int i = 0; i < 100; i++) { int x = rand.Next(0, 10); nums.add(x); } nums.printInOrder(); Console.WriteLine(); Console.WriteLine("Size: " + nums.size()); Console.WriteLine("Height: " + nums.height()); Console.WriteLine("Width: " + nums.width()); Console.WriteLine(); nums.remove(9); nums.removeAll(3); nums.invert(); nums.printInOrder(); Console.WriteLine(); Console.WriteLine("Size: " + nums.size()); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Treap <char> tree = new Treap <char>(); tree.add('2'); tree.add('+'); tree.add('3'); tree.add('*'); tree.add('6'); tree.printPreOrder(); Console.WriteLine(); tree.printInOrder(); Console.WriteLine(); tree.printPostOrder(); Console.WriteLine(); }
public static void Main(string[] args) { Treap treap = new Treap(); treap.Merge(ref treap.root, treap.root, new Node(1)); treap.Merge(ref treap.root, treap.root, new Node(2)); treap.Merge(ref treap.root, treap.root, new Node(3)); treap.Merge(ref treap.root, treap.root, new Node(4)); Treap left = new Treap(); Treap right = new Treap(); treap.Split(treap.root, ref left.root, ref right.root, 2); treap.Merge(ref treap.root, right.root, left.root); treap.Read(treap.root); }
public void TestExercise7c() { var t = new Treap(); t.Insert(7, 0); t.Insert(9, 3); t.Insert(2, 1); t.Insert(5, 7); t.Insert(3, 8); t.Insert(1, 4); t.Insert(4, 5); t.Insert(6, 6); t.Print(); }
public void GivenCollectionWhenCreateTreapWithItThenCanEnumerate() { 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.Equal(20, treap.Count); var pos = 0; foreach (var item in treap) { Assert.Equal(collection[pos++], item); } }
public void GivenTreapWhenWalkingInReverseOrderGetCorrectResults() { 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.Equal(20, treap.Count); var pos = collection.Count - 1; foreach (var item in treap.Reverse()) { Assert.Equal(collection[pos--], item); } }
public void MoveTest() { Treap <int> treap = new Treap <int>(); treap.Insert(0, 1); treap.Insert(1, 2); treap.Insert(2, 3); treap.Insert(3, 4); treap.Insert(4, 5); treap.Move(1, 3); Assert.AreEqual(treap[1], 3); Assert.AreEqual(treap[2], 4); Assert.AreEqual(treap[3], 2); }
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])); } }
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); }
public Test() { _tree = new Treap<int, int>(); _added = new List<int>(); }
public string RunTests() { Treap<int, double> _treetree = new Treap<int, double>(); var vystup = ""; double[] linked = new double[1000]; vystup += "\nPridanie 100 prvkov"; Random r = new Random(); for (int a = 0; a < 1000; a++) { double a2 = r.Next(0, 100000000); _treetree.Add(a, a2); linked[a] = a2; } var velkostPredVymazanim = _treetree.Size; vystup += "\nBolo pridanych prvokov v treape (size of treap): " + _treetree.Size; vystup += "\nBolo pridanych prvokov v liste (size of liste - o polovicu menej): " + linked.Length; vystup += "\nVymazanie kazdy treti prvok\n "; int vymazanych = 0; for (int a = 0; a < 100; a++) { int cislo = random.Next(1, linked.Length); _treetree.Remove(linked[cislo]); vymazanych++; } vystup += "\nBolo vymazanych prvokov v treape (size of treap): " + vymazanych; vystup += "\nRozdiel prvokov v treape pred - terajsia velkost: " + velkostPredVymazanim + " , " + _treetree.Size ; return vystup; }
public string VypisPrvky() { if (_tree == null) { _tree = new Treap<int, int>(); } var textBox1 = ""; textBox1 += "\n\n Traversal InOrder: \n\n"; int size = 0; /* foreach (var item in _tree.TreapToList()) { textBox1 += " " + item + " "; } */ if (_tree != null) { textBox1 = _tree.TreapToList() .Aggregate(textBox1, (current, item) => current + (" n: " + ++size + ". " + item + " || ")); textBox1 += "\n\n Size of Treap is: " + _tree.Size + " -----: \n\n"; } else { _tree = new Treap<int, int>(); return "Strom nie je inicalizovany;"; } return textBox1; }
public string VygenerujPrvky() { if (_tree == null) { _tree = new Treap<int, int>(); } var vystup = "Vygeneruje sa 1000 prvkov:\n"; Random rand = new Random(); var size = 1000; var skutocnePridanych = 0; if (_tree != null) { var predoslaVelkost = _tree.Size; for (int i = 1; i <= size; i++) { var random = rand.Next(3, 50000000); _tree.Add(rand.Next(0, 1600), random); //vystup += "||- i: " + i + ": " + random + " -|| "; } skutocnePridanych = _tree.Size - predoslaVelkost; } else { _tree = new Treap<int, int>(); } if ((size - skutocnePridanych) == 0) { vystup += "test prebehol uspesne. Z " + size + " bolo pridanych prave tolko prvkov. "; vystup += "\n USPESNE \n"; } else { vystup += "Pocty nesedia, rozdiel je : " + (size - skutocnePridanych); vystup += "\n NEUSPESNE \n"; } return vystup; }
public void VynulujStrom() { _tree = null; }