本文整理汇总了C#中Tree.Delete方法的典型用法代码示例。如果您正苦于以下问题:C# Tree.Delete方法的具体用法?C# Tree.Delete怎么用?C# Tree.Delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tree
的用法示例。
在下文中一共展示了Tree.Delete方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UniqueTreeTest
public void UniqueTreeTest ()
{
// Unique tree
var expectedRemain = new List<double>();
var tree = new Tree<double, string>(
new TreeMemoryNodeManager<double, string>(2, Comparer<double>.Default)
);
// Insert random numbers
for (var i = 0; i < 1000; i++) {
tree.Insert (i, i.ToString());
expectedRemain.Add (i);
}
// Start deleting randomly
var rnd = new Random ();
for (var i = 0; i < 1000; i++) {
var deleteAt = rnd.Next (0, expectedRemain.Count);
var keyToDelete = expectedRemain[deleteAt];
expectedRemain.RemoveAt (deleteAt);
tree.Delete (keyToDelete);
var remain = (from entry in tree.LargerThanOrEqualTo(0) select entry.Item1).ToArray();
Assert.IsTrue (remain.SequenceEqual (expectedRemain));
}
Assert.Throws<InvalidOperationException>(delegate {
tree.Delete (888, "888");
});
}
示例2: NonFullRootNodeTest
public void NonFullRootNodeTest ()
{
// Unique tree
var tree = new Tree<double, string>(
new TreeMemoryNodeManager<double, string>(2, Comparer<double>.Default)
);
tree.Insert (1, "1");
tree.Insert (3, "3");
tree.Insert (6, "6");
tree.Insert (8, "8");
tree.Delete (6);
Assert.IsTrue ((from t in tree.LargerThanOrEqualTo(0) select t.Item1).SequenceEqual(new double[] { 1, 3, 8 }));
tree.Delete (3);
Assert.IsTrue ((from t in tree.LargerThanOrEqualTo(0) select t.Item1).SequenceEqual(new double[] { 1, 8 }));
tree.Delete (1);
Assert.IsTrue ((from t in tree.LargerThanOrEqualTo(0) select t.Item1).SequenceEqual(new double[] { 8 }));
tree.Delete (8);
Assert.IsTrue ((from t in tree.LargerThanOrEqualTo(0) select t.Item1).Count() == 0);
}
示例3: DeleteLeaf_RightChild
public void DeleteLeaf_RightChild()
{
var t = new Tree<int>();
t.Add(23);
t.Add(20);
t.Add(25);
t.Add(24);
t.Add(33);
t.Add(10);
t.Add(7);
t.Add(8);
t.Delete(33);
Assert.AreEqual(23, t.Root.Item);
Assert.AreEqual(20, t.Root.Left.Item);
Assert.AreEqual(10, t.Root.Left.Left.Item);
Assert.AreEqual(8, t.Root.Left.Left.Left.Right.Item);
Assert.AreEqual(7, t.Root.Left.Left.Left.Item);
Assert.AreEqual(25, t.Root.Right.Item);
Assert.AreEqual(24, t.Root.Right.Left.Item);
Assert.IsNull(t.Find(33));
}
示例4: Test_Tree_Delete
public void Test_Tree_Delete()
{
var root = new InnerNode<int, int>(3);
root.Keys.Add(15);
var level1Child1 = new InnerNode<int, int>(3);
level1Child1.Keys.Add(9);
level1Child1.Keys.Add(12);
var level2Child1 = new LeafNode<int, int>(3);
level2Child1.Add(1, 1);
level2Child1.Add(4, 4);
level1Child1.Children.Add(level2Child1);
var level2Child2 = new LeafNode<int, int>(3);
level2Child2.Add(9, 9);
level2Child2.Add(10, 10);
level1Child1.Children.Add(level2Child2);
var level2Child3 = new LeafNode<int, int>(3);
level2Child3.Add(12, 12);
level2Child3.Add(13, 13);
level1Child1.Children.Add(level2Child3);
root.Children.Add(level1Child1);
var level1Child2 = new InnerNode<int, int>(3);
level1Child2.Keys.Add(20);
var level2Child4 = new LeafNode<int, int>(3);
level2Child4.Add(15, 15);
level2Child4.Add(16, 16);
level1Child2.Children.Add(level2Child4);
var level2Child5 = new LeafNode<int, int>(3);
level2Child5.Add(20,20);
level2Child5.Add(25,25);
level1Child2.Children.Add(level2Child5);
root.Children.Add(level1Child2);
var tree = new Tree<int, int>(root);
tree.Delete(13);
Assert.AreEqual(1, root.Keys.Count);
Assert.AreEqual(15, root.Keys[0]);
var delete13Parent = (root.Children[0] as InnerNode<int, int>);
Assert.AreEqual(2, delete13Parent.Keys.Count);
Assert.AreEqual(09, delete13Parent.Keys[0]);
Assert.AreEqual(12, delete13Parent.Keys[1]);
Assert.AreEqual(3, delete13Parent.Children.Count);
var delete13Child3 = delete13Parent.Children[2];
Assert.AreEqual(1, delete13Child3.Keys.Count);
Assert.AreEqual(12, delete13Child3.Keys[0]);
tree.Delete(15);
var delete15Parent = (root.Children[1] as InnerNode<int, int>);
Assert.AreEqual(1, delete15Parent.Keys.Count);
Assert.AreEqual(20, delete15Parent.Keys[0]);
Assert.AreEqual(2, delete15Parent.Children.Count);
var delete15Child1 = delete15Parent.Children[0];
Assert.AreEqual(1, delete15Child1.Keys.Count);
Assert.AreEqual(16, delete15Child1.Keys[0]);
tree.Delete(12);
var delete12Parent = (root.Children[0] as InnerNode<int, int>);
Assert.AreEqual(2, delete12Parent.Keys.Count);
Assert.AreEqual(9, delete12Parent.Keys[0]);
Assert.AreEqual(10, delete12Parent.Keys[1]);
Assert.AreEqual(3, delete12Parent.Children.Count);
var delete12Child2 = delete12Parent.Children[1];
Assert.AreEqual(1, delete12Child2.Keys.Count);
Assert.AreEqual(9, delete12Child2.Keys[0]);
var delete12Child3 = delete12Parent.Children[2];
Assert.AreEqual(1, delete12Child3.Keys.Count);
Assert.AreEqual(10, delete12Child3.Keys[0]);
tree.Delete(16);
var delete16Parent = (root.Children[1] as InnerNode<int, int>);
Assert.AreEqual(2, delete16Parent.Children.Count);
Assert.AreEqual(1, delete16Parent.Keys.Count);
Assert.AreEqual(25, delete16Parent.Keys[0]);
var delete16Child1 = delete16Parent.Children[0];
Assert.AreEqual(1, delete16Child1.Keys.Count);
Assert.AreEqual(20, delete16Child1.Keys[0]);
var delete16Child2 = delete16Parent.Children[1];
Assert.AreEqual(1, delete16Child2.Keys.Count);
Assert.AreEqual(25, delete16Child2.Keys[0]);
tree.Delete(25);
tree.Delete(10);
}
示例5: TestSavingFullTree
public void TestSavingFullTree ()
{
// Create a tree with random elements,
// presist it in a memory stream and then reconstruct it
BasicConfigurator.Configure ();
var stream = new MemoryStream ();
var tree = new Tree<int, long>(
new TreeDiskNodeManager<int, long> (
new TreeIntSerializer(),
new TreeLongSerializer(),
new RecordStorage(
new BlockStorage(
stream,
4096,
48
)
)
),
true
);
// Generate 10000 elements
var sampleData = new List<Tuple<int, long>>();
var rnd = new Random ();
for (var i = 0; i < 1000; i++)
{
sampleData.Add (new Tuple<int, long>(
rnd.Next(Int32.MinValue, Int32.MaxValue),
(long)rnd.Next(Int32.MinValue, Int32.MaxValue)
));
}
// Insert these elements into the tree
foreach (var d in sampleData) {
tree.Insert (d.Item1, d.Item2);
// Now create new tree see if changes were saved to the underlying stream
var tree2 = new Tree<int, long>(
new TreeDiskNodeManager<int, long> (
new TreeIntSerializer(),
new TreeLongSerializer(),
new RecordStorage(
new BlockStorage(
stream,
4096,
48
)
)
),
true
);
var actual = (from i in tree2.LargerThanOrEqualTo (Int32.MinValue) select i).ToList();
var expected = (from i in tree.LargerThanOrEqualTo(Int32.MinValue) select i).ToList();
Assert.IsTrue (actual.SequenceEqual (expected));
}
// Test deletion of some lemenets
for (var i = 0; i < sampleData.Count; i++) {
var deleteAt = rnd.Next (0, sampleData.Count);
var deleteKey = sampleData[deleteAt];
sampleData.RemoveAt (deleteAt);
tree.Delete (deleteKey.Item1, deleteKey.Item2);
// Now create new tree see if changes were saved to the underlying stream
var tree2 = new Tree<int, long>(
new TreeDiskNodeManager<int, long> (
new TreeIntSerializer(),
new TreeLongSerializer(),
new RecordStorage(
new BlockStorage(
stream,
4096,
48
)
)
),
true
);
var actual = (from ii in tree2.LargerThanOrEqualTo (Int32.MinValue) select ii.Item1).ToList();
var expected = (from ii in tree.LargerThanOrEqualTo(Int32.MinValue) select ii.Item1).ToList();
Assert.IsTrue (actual.SequenceEqual (expected));
}
}
示例6: DeleteNodeWithSingleChild_LeftParentLeftChild
public void DeleteNodeWithSingleChild_LeftParentLeftChild()
{
var t = new Tree<int>();
t.Add(22);
t.Add(20);
t.Add(25);
t.Add(24);
t.Add(33);
t.Add(10);
t.Add(7);
t.Add(8);
t.Add(23);
t.Delete(24);
Assert.AreEqual(22, t.Root.Item);
Assert.AreEqual(20, t.Root.Left.Item);
Assert.AreEqual(10, t.Root.Left.Left.Item);
Assert.AreEqual(8, t.Root.Left.Left.Left.Right.Item);
Assert.AreEqual(25, t.Root.Right.Item);
Assert.AreEqual(23, t.Root.Right.Left.Item);
Assert.AreEqual(7, t.Root.Left.Left.Left.Item);
Assert.AreEqual(33, t.Root.Right.Right.Item);
Assert.IsNull(t.Find(24));
}
示例7: DeleteRootWithNoChildrenShouldSetRootToNull
public void DeleteRootWithNoChildrenShouldSetRootToNull()
{
var t = new Tree<int>();
t.Add(22);
t.Delete(22);
Assert.IsNull(t.Root);
}
示例8: DeleteRootTwoChildren
public void DeleteRootTwoChildren()
{
var t = new Tree<int>();
t.Add(23);
t.Add(20);
t.Add(25);
t.Add(24);
t.Add(33);
t.Add(10);
t.Add(7);
t.Add(8);
t.Delete(23);
Assert.AreEqual(20, t.Root.Item);
Assert.AreEqual(10, t.Root.Left.Item);
Assert.AreEqual(7, t.Root.Left.Left.Item);
Assert.AreEqual(8, t.Root.Left.Left.Right.Item);
Assert.AreEqual(25, t.Root.Right.Item);
Assert.AreEqual(24, t.Root.Right.Left.Item);
Assert.AreEqual(33, t.Root.Right.Right.Item);
}
示例9: DeleteRootOnlyOneChildSetsChildAsRoot_Right
public void DeleteRootOnlyOneChildSetsChildAsRoot_Right()
{
var t = new Tree<int>();
t.Add(23);
t.Add(28);
t.Delete(23);
Assert.AreEqual(28, t.Root.Item);
Assert.IsNull(t.Root.Left);
Assert.IsNull(t.Root.Right);
}
示例10: DeleteNodeWithTwoChildren
public void DeleteNodeWithTwoChildren()
{
var t = new Tree<int>();
t.Add(22);
t.Add(20);
t.Add(25);
t.Add(24);
t.Add(33);
t.Add(10);
t.Add(7);
t.Add(8);
t.Add(23);
t.Add(35);
t.Delete(25);
Assert.AreEqual(22, t.Root.Item);
Assert.AreEqual(20, t.Root.Left.Item);
Assert.AreEqual(10, t.Root.Left.Left.Item);
Assert.AreEqual(8, t.Root.Left.Left.Left.Right.Item);
Assert.AreEqual(7, t.Root.Left.Left.Left.Item);
Assert.AreEqual(24, t.Root.Right.Item);
Assert.AreEqual(23, t.Root.Right.Left.Item);
Assert.AreEqual(33, t.Root.Right.Right.Item);
Assert.AreEqual(35, t.Root.Right.Right.Right.Item);
Assert.IsNull(t.Find(25));
}
示例11: NonUniqueTreeTestWithDuplicateKeys
public void NonUniqueTreeTestWithDuplicateKeys ()
{
// Unique tree
var expectedRemain = new List<Tuple<double, string>>();
var tree = new Tree<double, string>(
new TreeMemoryNodeManager<double, string>(2, Comparer<double>.Default),
true
);
/*
tree.Insert (1, "A");
tree.Insert (1, "B");
tree.Insert (1, "C");
tree.Insert (1, "D");
tree.Insert (1, "E");
tree.Insert (1, "F");
tree.Insert (1, "G");
tree.Insert (1, "H");
tree.Delete (1, "E");
var remain = (from entry in tree.LargerThanOrEqualTo(0) select entry.Item2).ToArray();
Assert.IsTrue (remain.SequenceEqual(
new List<string>{
"A",
"B",
"C",
"D",
"F",
"G",
"H"
}
)); */
// Insert random numbers
var rnd = new Random ();
for (var i = 0; i < 1000; i++) {
tree.Insert (i, "A");
expectedRemain.Add (new Tuple<double, string>(i, "A"));
var dupCount = rnd.Next (0, 3);
for (var t = 0; t < dupCount; t++) {
if (t == 0) {
tree.Insert (i, "B");
expectedRemain.Add (new Tuple<double, string>(i, "B"));
} else if (t == 1) {
tree.Insert (i, "C");
expectedRemain.Add (new Tuple<double, string>(i, "C"));
} else {
throw new Exception ();
}
}
}
// Start deleting randomly
for (var i = 0; i < 1000; i++) {
var deleteAt = rnd.Next (0, expectedRemain.Count);
var keyToDelete = expectedRemain[deleteAt];
expectedRemain.RemoveAt (deleteAt);
tree.Delete (keyToDelete.Item1, keyToDelete.Item2);
var remain = (from entry in tree.LargerThanOrEqualTo(0) orderby entry.Item1, entry.Item2 select entry).ToArray();
Assert.IsTrue (remain.SequenceEqual (expectedRemain));
}
}