本文整理汇总了C#中Tree.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# Tree.Insert方法的具体用法?C# Tree.Insert怎么用?C# Tree.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tree
的用法示例。
在下文中一共展示了Tree.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SplitRootNodeTest
public void SplitRootNodeTest ()
{
// Insert too much at root node that it overvlow
var tree = new Tree<int, string>(new TreeMemoryNodeManager<int, string>(2, Comparer<int>.Default));
tree.Insert (0, "00");
tree.Insert (1, "11");
tree.Insert (2, "22");
tree.Insert (3, "33");
tree.Insert (4, "44");
// Validate the tree
Assert.IsNull (tree.Get(8));
Assert.IsNull (tree.Get(-1));
Assert.IsNull (tree.Get(99));
Assert.NotNull (tree.Get (0));
Assert.NotNull (tree.Get (1));
Assert.NotNull (tree.Get (2));
Assert.NotNull (tree.Get (3));
Assert.NotNull (tree.Get (4));
Assert.IsTrue ((from tpl in tree.LargerThanOrEqualTo(4) select tpl.Item1).SequenceEqual(new int[]{ 4 }));
Assert.IsTrue ((from tpl in tree.LargerThan(1) select tpl.Item1).SequenceEqual(new int[]{ 2, 3, 4 }));
Assert.IsTrue ((from tpl in tree.LessThan(3) select tpl.Item1).SequenceEqual(new int[]{ 2,1,0 }));
Assert.IsTrue ((from tpl in tree.LessThanOrEqualTo(3) select tpl.Item1).SequenceEqual(new int[]{ 3, 2,1,0 }));
}
示例2: NonFullRootNodeTest
public void NonFullRootNodeTest ()
{
var tree = new Tree<int, string>(new TreeMemoryNodeManager<int, string>(2, Comparer<int>.Default));
tree.Insert (1, "1");
Assert.AreEqual (0, (from node in tree.LargerThan(7) select node).Count());
Assert.AreEqual (0, (from node in tree.LargerThanOrEqualTo(7) select node).Count());
Assert.AreEqual (1, (from node in tree.LessThan(7) select node).FirstOrDefault().Item1);
Assert.AreEqual (1, (from node in tree.LessThanOrEqualTo(7) select node).FirstOrDefault().Item1);
Assert.AreEqual (1, (from node in tree.LessThanOrEqualTo(1) select node).FirstOrDefault().Item1);
Assert.AreEqual (0, (from node in tree.LessThan(1) select node).Count());
tree.Insert (5, "2");
tree.Insert (9, "9");
// 1,5,9
Assert.IsTrue ((from node in tree.LessThanOrEqualTo(9) select node.Item1).SequenceEqual(new int[] { 9, 5, 1 }));
Assert.IsTrue ((from node in tree.LessThan(9) select node.Item1).SequenceEqual(new int[] { 5, 1 }));
Assert.IsTrue ((from node in tree.LargerThanOrEqualTo(5) select node.Item1).SequenceEqual(new int[] { 5, 9 }));
Assert.IsTrue ((from node in tree.LargerThan(5) select node.Item1).SequenceEqual(new int[] { 9 }));
Assert.Throws<TreeKeyExistsException>(delegate {
tree.Insert (9, "9");
});
}
示例3: NonFullRootNodeTest
public void NonFullRootNodeTest ()
{
var tree = new Tree<int, string>(new TreeMemoryNodeManager<int, string>(2, Comparer<int>.Default),
allowDuplicateKeys: true);
tree.Insert (1, "1");
Assert.AreEqual (0, (from node in tree.LargerThan(7) select node).Count());
Assert.AreEqual (0, (from node in tree.LargerThanOrEqualTo(7) select node).Count());
Assert.AreEqual (1, (from node in tree.LessThan(7) select node).FirstOrDefault().Item1);
Assert.AreEqual (1, (from node in tree.LessThanOrEqualTo(7) select node).FirstOrDefault().Item1);
Assert.AreEqual (1, (from node in tree.LessThanOrEqualTo(1) select node).FirstOrDefault().Item1);
Assert.AreEqual (0, (from node in tree.LessThan(1) select node).Count());
tree.Insert (5, "5");
tree.Insert (9, "9");
// 1,5,9
Assert.IsTrue ((from node in tree.LessThanOrEqualTo(9) select node.Item1).SequenceEqual(new int[] { 9, 5, 1 }));
Assert.IsTrue ((from node in tree.LessThan(9) select node.Item1).SequenceEqual(new int[] { 5, 1 }));
Assert.IsTrue ((from node in tree.LargerThanOrEqualTo(5) select node.Item1).SequenceEqual(new int[] { 5, 9 }));
Assert.IsTrue ((from node in tree.LargerThan(5) select node.Item1).SequenceEqual(new int[] { 9 }));
Assert.DoesNotThrow(delegate {
tree.Insert (5, "5.1");
});
// Iterating test
var found = (from node in tree.LargerThanOrEqualTo(5) select node.Item2).ToList ();
Assert.AreEqual (3, found.Count);
Assert.IsTrue (found.Contains("5.1"));
Assert.IsTrue (found.Contains("5"));
}
示例4: Main
static void Main()
{
var bst = new Tree<int>();
for (int i = 0; i < 20; i++)
{
bst.Insert(i + 1);
}
Console.WriteLine("Initially: {0}", bst);
bst.BalanceTree();
Console.WriteLine("Balanced: {0}", bst);
Console.WriteLine("Traverse and print (DFS)");
bst.TraverseDFS();
var bst2 = new Tree<int>();
for (int i = 0; i < 20; i++)
{
bst2.Insert(i + 1);
}
bst2.BalanceTree();
Console.WriteLine("Second tree (balanced): {0}", bst2);
// returns true only if both trees have the same number of nodes,
// nodes hold equal values, and trees are balanced in the same way
Console.WriteLine("First tree equals second tree? {0}", bst.Equals(bst2));
var bst3 = (Tree<int>)bst2.Clone();
Console.WriteLine("Third tree, cloned from second: {0}", bst3);
Console.WriteLine("Traverse and print cloned tree (DFS)");
bst3.TraverseDFS();
}
示例5: NonUniqueTreeTestWithNoDuplicateKey
public void NonUniqueTreeTestWithNoDuplicateKey ()
{
// Unique tree
var expectedRemain = new List<double>();
var tree = new Tree<double, string>(
new TreeMemoryNodeManager<double, string>(2, Comparer<double>.Default),
true
);
// 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, keyToDelete.ToString());
var remain = (from entry in tree.LargerThanOrEqualTo(0) select entry.Item1).ToArray();
Assert.IsTrue (remain.SequenceEqual (expectedRemain));
}
}
示例6: 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);
}
示例7: FromLinearDecomposition
public static Decomposition FromLinearDecomposition(LinearDecomposition ld)
{
Tree tree = new Tree();
tree.Insert(ld.Graph.Vertices);
BitSet parent = ld.Graph.Vertices;
foreach (int item in ld.Sequence.Take(ld.Sequence.Count - 1))
{
tree.InsertWithParent(parent * item, parent);
tree.InsertWithParent(parent - item, parent);
parent = parent - item;
}
return new Decomposition(ld.Graph, tree)
{
// TODO: speed up using this
//maxNeighborhoodSize = ld.MaxNeighborhoodSize
};
}
示例8: 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));
}
}
示例9: Read
// Parses a decomposition given the filename of the decomposition and the graph.
public static Decomposition Read(TextReader reader, Graph graph)
{
string line;
Tree tree = new Tree();
// Each line is simply an integer, which is the sequence of the linear decomposition
while ((line = reader.ReadLine()) != null)
{
// Skip comments
if (line.StartsWith("c ")) continue;
string[] s = line.Trim().Split(' ');
BitSet node = new BitSet(0, graph.Size);
foreach (string vertex in s)
node += int.Parse(vertex) - 1; // -1 because internally we work with [0,...,n)
tree.Insert(node);
}
return new Decomposition(graph, tree);
}
示例10: BreakNodeTest
public void BreakNodeTest ()
{
// Generate a random sequence
var seq = new List<double>();
var rnd = new Random ();
for (var i = 0; i < 1000; i++) {
seq.Add (rnd.Next(0, 10));
}
// Start inserting
var tree = new Tree<double, string>(
new TreeMemoryNodeManager<double, string>(2, Comparer<double>.Default),
allowDuplicateKeys: true
);
foreach (var t in seq) {
tree.Insert (t, t.ToString());
}
// Test 1
{
var keys = (from t in tree.LargerThanOrEqualTo(0) select t.Item1).ToArray();
Assert.AreEqual (seq.Count, keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, seq), OccurencesInList(key, keys));
}
}
// Test 2
{
var keys = (from t in tree.LargerThanOrEqualTo(7) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t >= 7 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t >= 7 select t), OccurencesInList(key, keys));
}
}
// Test 3
{
var keys = (from t in tree.LargerThan(7) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t > 7 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t > 7 select t), OccurencesInList(key, keys));
}
}
// Test 4
{
var keys = (from t in tree.LargerThanOrEqualTo(7.5) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t >= 7.5 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t >= 7 select t), OccurencesInList(key, keys));
}
}
// Test 5
{
var keys = (from t in tree.LargerThan(7.5) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t > 7.5 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t > 7.5 select t), OccurencesInList(key, keys));
}
}
// Test 6
{
var keys = (from t in tree.LessThan(7) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t < 7 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t < 7 select t), OccurencesInList(key, keys));
}
}
// Test 7
{
var keys = (from t in tree.LessThan(7.5) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t < 7.5 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t < 7.5 select t), OccurencesInList(key, keys));
}
}
// Test 8
{
var keys = (from t in tree.LessThanOrEqualTo(7) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t <= 7 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t <= 7 select t), OccurencesInList(key, keys));
}
}
// Test 9
{
var keys = (from t in tree.LessThanOrEqualTo(7.5) select t.Item1).ToArray();
Assert.AreEqual ((from t in seq where t <= 7.5 select t).Count(), keys.Length);
foreach (var key in keys) {
Assert.AreEqual (OccurencesInList(key, from t in seq where t <= 7.5 select t), OccurencesInList(key, keys));
}
}
}
示例11: Test_Tree_Insert_Reverse
public void Test_Tree_Insert_Reverse()
{
var tree = new Tree<int, int>(3);
for (int i = 9; i >= 1; i--)
tree.Insert(i, i);
var root = tree.Root as InnerNode<int, int>;
Assert.IsNotNull(root);
Assert.AreEqual(1, root.Keys.Count);
Assert.AreEqual(6, root.Keys[0]);
}
示例12: Open
public void Open(string path)
{
_archivePath = path;
_archive = ArchiveFactory.Open(path);
var isRar = _archive is RarArchive;
Slash = isRar ? BACKSLASH : SLASH;
_fileStructure = new Tree<FileSystemItem>();
foreach (var entry in _archive.Entries.OrderBy(e => e.FilePath))
{
var entryPath = entry.FilePath;
if (isRar) entryPath += BACKSLASH;
_fileStructure.Insert(entryPath, CreateModel(entry), name => CreateModel(name, true, DateTime.MinValue, null));
}
}
示例13: RandomTest
public void RandomTest ()
{
var tree = new Tree<int, string>(new TreeMemoryNodeManager<int, string>(2, Comparer<int>.Default));
// Generate a random sequence
var seq = new List<int>();
var rnd = new Random (198);
// Insert random sequence
for (var i = 0; i < 1000; i++)
{
var n = rnd.Next (0, 2000);
while (seq.Contains(n) == true) {
n = rnd.Next (0, 2000);
}
tree.Insert (n, n.ToString());
seq.Add (n);
}
// Validate result
var sortedSeq = seq.FindAll (t => true);
sortedSeq.Sort (Comparer<int>.Default);
Assert.IsTrue (sortedSeq.SequenceEqual(from tuple in tree.LargerThanOrEqualTo(0) select tuple.Item1));
// Randomly query
for (var n = 0; n <= 100; n++)
{
var number = n = rnd.Next (0, 2000);
var lastD = 0;
foreach (var d in from tuple in tree.LargerThanOrEqualTo(number) select tuple.Item1)
{
Assert.IsTrue (d >= number);
Assert.IsTrue (d > lastD);
lastD = d;
}
lastD = 0;
foreach (var d in from tuple in tree.LargerThan(number) select tuple.Item1)
{
Assert.IsTrue (d > number);
Assert.IsTrue (d > lastD);
lastD = d;
}
lastD = 999999;
foreach (var d in from tuple in tree.LessThan(number) select tuple.Item1)
{
Assert.IsTrue (d < number);
Assert.IsTrue (d < lastD);
lastD = d;
}
lastD = 999999;
foreach (var d in from tuple in tree.LessThanOrEqualTo(number) select tuple.Item1)
{
Assert.IsTrue (d <= number);
Assert.IsTrue (d < lastD);
lastD = d;
}
}
}
示例14: SplitChildNodeTest
public void SplitChildNodeTest ()
{
// Insert too much at root node that it overvlow
var tree = new Tree<int, string>(new TreeMemoryNodeManager<int, string>(2, Comparer<int>.Default));
for (var i = 0; i <= 100; i++)
{
tree.Insert (i, i.ToString());
var result = (from tuple in tree.LargerThanOrEqualTo(0) select tuple.Item1).ToList();
Assert.AreEqual (i + 1, result.Count);
}
}
示例15: Test_Tree_Insert
public void Test_Tree_Insert()
{
var tree = new Tree<int, int>(3);
for (int i = 1; i <= 9; i++)
tree.Insert(i, i);
// root
var root = tree.Root as InnerNode<int, int>;
Assert.IsNotNull(root);
Assert.AreEqual(2, root.Children.Count);
Assert.AreEqual(1, root.Keys.Count);
Assert.AreEqual(5, root.Keys[0]);
// level 1 first child
var firstChild1 = root.Children[0] as InnerNode<int, int>;
Assert.AreEqual(2, firstChild1.Children.Count);
Assert.AreEqual(1, firstChild1.Keys.Count);
Assert.AreEqual(3, firstChild1.Keys[0]);
// level 1 second child
var secondChild1 = root.Children[1] as InnerNode<int, int>;
Assert.AreEqual(2, secondChild1.Children.Count);
Assert.AreEqual(1, secondChild1.Keys.Count);
Assert.AreEqual(7, secondChild1.Keys[0]);
// level 2 first child
var firstChild2 = firstChild1.Children[0] as InnerNode<int, int>;
Assert.AreEqual(2, firstChild2.Children.Count);
Assert.AreEqual(1, firstChild2.Keys.Count);
Assert.AreEqual(2, firstChild2.Keys[0]);
// level 2 second child
var secondChild2 = firstChild1.Children[1] as InnerNode<int, int>;
Assert.AreEqual(2, secondChild2.Children.Count);
Assert.AreEqual(1, secondChild2.Keys.Count);
Assert.AreEqual(4, secondChild2.Keys[0]);
// level 2 third child
var thirdChild2 = secondChild1.Children[0] as InnerNode<int, int>;
Assert.AreEqual(2, thirdChild2.Children.Count);
Assert.AreEqual(1, thirdChild2.Keys.Count);
Assert.AreEqual(6, thirdChild2.Keys[0]);
// level 2 fourth child
var fourthChild2 = secondChild1.Children[1] as InnerNode<int, int>;
Assert.AreEqual(2, fourthChild2.Children.Count);
Assert.AreEqual(1, fourthChild2.Keys.Count);
Assert.AreEqual(8, fourthChild2.Keys[0]);
// level 3 first child
var firstChild3 = firstChild2.Children[0] as LeafNode<int, int>;
Assert.IsNotNull(firstChild3);
Assert.AreEqual(1, firstChild3.Keys.Count);
Assert.AreEqual(1, firstChild3.Keys[0]);
// level 3 second child
var secondChild3 = firstChild2.Children[1] as LeafNode<int, int>;
Assert.IsNotNull(secondChild3);
Assert.AreEqual(1, secondChild3.Keys.Count);
Assert.AreEqual(2, secondChild3.Keys[0]);
// level 3 third child
var thirdChild3 = secondChild2.Children[0] as LeafNode<int, int>;
Assert.IsNotNull(thirdChild3);
Assert.AreEqual(1, thirdChild3.Keys.Count);
Assert.AreEqual(3, thirdChild3.Keys[0]);
// level 3 fourth child
var fourthdChild3 = secondChild2.Children[1] as LeafNode<int, int>;
Assert.IsNotNull(fourthdChild3);
Assert.AreEqual(1, fourthdChild3.Keys.Count);
Assert.AreEqual(4, fourthdChild3.Keys[0]);
// level 3 fifth child
var fifthChild3 = thirdChild2.Children[0] as LeafNode<int, int>;
Assert.IsNotNull(fifthChild3);
Assert.AreEqual(1, fifthChild3.Keys.Count);
Assert.AreEqual(5, fifthChild3.Keys[0]);
// level 3 sixth child
var sixthChild3 = thirdChild2.Children[1] as LeafNode<int, int>;
Assert.IsNotNull(sixthChild3);
Assert.AreEqual(1, sixthChild3.Keys.Count);
Assert.AreEqual(6, sixthChild3.Keys[0]);
// level 3 seventh child
var seventhChild3 = fourthChild2.Children[0] as LeafNode<int, int>;
Assert.IsNotNull(seventhChild3);
Assert.AreEqual(1, seventhChild3.Keys.Count);
Assert.AreEqual(7, seventhChild3.Keys[0]);
// level 3 eighth child
var eighthChild3 = fourthChild2.Children[1] as LeafNode<int, int>;
Assert.IsNotNull(eighthChild3);
Assert.AreEqual(2, eighthChild3.Keys.Count);
Assert.AreEqual(8, eighthChild3.Keys[0]);
Assert.AreEqual(9, eighthChild3.Keys[1]);
}