public void CartesianTreeMustWork() { var rnd = new Random(123); int[] pr = new int[1000000]; for (int i = 0; i < 1000000; ++i) { pr[i] = i; int u = rnd.Next(i + 1); int tmp = pr[i]; pr[i] = pr[u]; pr[u] = tmp; } int prid = 0; CartesianTree tree = null; var list = new List <int>(); for (int i = 0; i < 2000; ++i) { var value = rnd.Next(); var pos = rnd.Next(list.Count + 1); CartesianTree.Insert(ref tree, value, pr[prid++], pos); list.Insert(pos, value); var testList = new List <int>(); CartesianTree.Dfs(tree, t => testList.Add(t.Data)); Assert.AreEqual(list.Count, testList.Count); for (int j = 0; j < list.Count; ++j) { Assert.AreEqual(list[j], testList[j]); } } for (int i = 0; i < list.Count; ++i) { Assert.AreEqual(list[i], CartesianTree.Find(tree, i).Data); } for (int i = 0; i < 2000; ++i) { var pos = rnd.Next(list.Count); CartesianTree.Erase(ref tree, pos); list.RemoveAt(pos); var testList = new List <int>(); CartesianTree.Dfs(tree, t => testList.Add(t.Data)); Assert.AreEqual(list.Count, testList.Count); for (int j = 0; j < list.Count; ++j) { Assert.AreEqual(list[j], testList[j]); } } }
public void Generate() { compare.Clear(); List <Node> temp = new List <Node>(); for (int i = 0; i < cycnum; i++) { temp.Add(new Node(r.Next(100))); } temp.Sort((node1, node2) => Comparer <int> .Default.Compare(node1.Key, node2.Key)); tree = new CartesianTree <Node, int>(temp) { SupportEquatable = true }; compare.AddRange(temp); Assert.IsTrue(compare.OrderBy(node => node.Key).SequenceEqual(tree)); }