本文整理汇总了C#中RedBlackTree类的典型用法代码示例。如果您正苦于以下问题:C# RedBlackTree类的具体用法?C# RedBlackTree怎么用?C# RedBlackTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RedBlackTree类属于命名空间,在下文中一共展示了RedBlackTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildStorageEntry
private static int BuildStorageEntry(DirectoryEntry storageEntry)
{
// direct members of each storage are organised in a separate red-black tree
RedBlackTree<DirectoryEntry> rbTree = new RedBlackTree<DirectoryEntry>();
foreach (DirectoryEntry entry in storageEntry.Members.Values)
{
rbTree.Add(entry);
}
foreach (RedBlackTreeNode<DirectoryEntry> node in rbTree.InorderTreeWalk(rbTree.Root))
{
DirectoryEntry entry = node.Data;
entry.NodeColor = GetNodeColor(node.Color);
entry.LeftChildDID = GetNodeID(node.Left);
entry.RightChildDID = GetNodeID(node.Right);
if (entry.Members.Count > 0)
{
entry.EntryType = EntryType.Storage;
entry.MembersTreeNodeDID = BuildStorageEntry(entry);
}
else
{
entry.EntryType = EntryType.Stream;
entry.MembersTreeNodeDID = -1;
}
}
return rbTree.Root.Data.ID;
}
示例2: EmptyTreeTest
public void EmptyTreeTest()
{
var tree = new RedBlackTree<Int32>();
Assert.IsTrue(tree.IsEmpty);
Assert.IsFalse(tree.BredthContains(2));
}
示例3: ExceptionNullKey
public void ExceptionNullKey()
{
var redBlackTree = new RedBlackTree<object, string>
{
{null, "a"}
};
}
示例4: RotationTests
public void RotationTests()
{
var tree = new RedBlackTree<Int32>();
tree.Insert(35);
tree.Insert(25);
tree.Insert(70);
tree.Insert(10);
tree.Insert(100);
tree.Insert(20);
tree.Insert(5);
tree.Insert(85);
tree.Insert(55);
tree.Insert(40);
Assert.AreEqual(35, tree.RootNode.NodeValue);
Assert.AreEqual(20, tree.RootNode.LeftNode.NodeValue);
Assert.AreEqual(25, tree.RootNode.LeftNode.RightNode.NodeValue);
Assert.AreEqual(10, tree.RootNode.LeftNode.LeftNode.NodeValue);
Assert.AreEqual(5, tree.RootNode.LeftNode.LeftNode.LeftNode.NodeValue);
Assert.AreEqual(85, tree.RootNode.RightNode.NodeValue);
Assert.AreEqual(100, tree.RootNode.RightNode.RightNode.NodeValue);
Assert.AreEqual(55, tree.RootNode.RightNode.LeftNode.NodeValue);
Assert.AreEqual(40, tree.RootNode.RightNode.LeftNode.LeftNode.NodeValue);
Assert.AreEqual(70, tree.RootNode.RightNode.LeftNode.RightNode.NodeValue);
}
示例5: NonIComparable
public void NonIComparable()
{
var redBlackTree = new RedBlackTree<NonComparableTClass, string>();
for (var i = 0; i < 100; i++)
{
redBlackTree.Add(new NonComparableTClass(i), i.ToString());
}
var newTree = SerializeUtil.BinarySerializeDeserialize(redBlackTree);
Assert.AreNotSame(redBlackTree, newTree);
Assert.AreEqual(redBlackTree.Count, newTree.Count);
var redBlackTreeEnumerator = redBlackTree.GetEnumerator();
var newTreeEnumerator = newTree.GetEnumerator();
while (redBlackTreeEnumerator.MoveNext())
{
Assert.IsTrue(newTreeEnumerator.MoveNext());
Assert.AreEqual(redBlackTreeEnumerator.Current.Key.Number, newTreeEnumerator.Current.Key.Number);
Assert.AreEqual(redBlackTreeEnumerator.Current.Value, newTreeEnumerator.Current.Value);
Assert.IsTrue(newTree.ContainsKey(redBlackTreeEnumerator.Current.Key));
Assert.AreEqual(newTree[redBlackTreeEnumerator.Current.Key], redBlackTreeEnumerator.Current.Value);
}
Assert.IsFalse(newTreeEnumerator.MoveNext());
}
示例6: ExceptionKeyAlreadyInTree
public void ExceptionKeyAlreadyInTree()
{
var redBlackTree = new RedBlackTree<int, string>
{
{0, "50"},
{0, "20"}
};
}
示例7: AddExample
public void AddExample()
{
// Create a simple tree
var tree = new RedBlackTree<string, int> {{"cat", 1}, {"dog", 2}, {"canary", 3}};
// There should be 3 items in the tree.
Assert.AreEqual(3, tree.Count);
}
示例8: RootNodeTest
public void RootNodeTest()
{
var tree = new RedBlackTree<Int32>();
tree.Insert(10);
Assert.IsFalse(tree.IsEmpty);
Assert.IsTrue(tree.BredthContains(10));
Assert.AreEqual(10, tree.RootNode.NodeValue);
}
示例9: ContainsFailureTest
public void ContainsFailureTest()
{
var tree = new RedBlackTree<Int32>();
tree.Insert(10);
tree.Insert(25);
tree.Insert(7);
Assert.IsFalse(tree.BredthContains(2));
}
示例10: ContainsSuccessTest
public void ContainsSuccessTest()
{
var tree = new RedBlackTree<Int32>();
tree.Insert(10);
tree.Insert(25);
tree.Insert(7);
Assert.IsTrue(tree.BredthContains(25));
}
示例11: Simple
public void Simple()
{
var redBlackTree = new RedBlackTree<int, string>();
Assert.AreEqual(redBlackTree.Count, 0);
Assert.IsTrue(redBlackTree.Comparer is KeyValuePairComparer<int, string>);
redBlackTree = new RedBlackTree<int, string>(new ReverseComparer<int>());
Assert.AreEqual(redBlackTree.Count, 0);
Assert.IsTrue(redBlackTree.Comparer.GetType().IsAssignableFrom(typeof(KeyValuePairComparer<int, string>)));
}
示例12: GetTestTree
internal static RedBlackTree<int, string> GetTestTree(int noOfItems)
{
var redBlackTree = new RedBlackTree<int, string>();
for (var i = 0; i < noOfItems; i++)
{
redBlackTree.Add(i, i.ToString());
}
return redBlackTree;
}
示例13: AddItem
public void AddItem()
{
RedBlackTree<int, int> tree = new RedBlackTree<int, int>();
int temp;
tree.Add(1, 1);
Assert.AreEqual(tree.Count, 1);
Assert.IsTrue(tree.TryGetValue(1, out temp));
Assert.IsFalse(tree.TryGetValue(2, out temp));
}
示例14: Simple
public void Simple()
{
var redBlackTree = GetTestTree();
Assert.IsFalse(redBlackTree.IsEmpty);
redBlackTree.Clear();
Assert.IsTrue(redBlackTree.IsEmpty);
redBlackTree = new RedBlackTree<int, string>();
Assert.IsTrue(redBlackTree.IsEmpty);
}
示例15: AddTest
public void AddTest()
{
//Arrange
var bst = new RedBlackTree<int, string>();
// 33 28 63 20 31 39 79 10 35 60 ( red links = 10 39 )
var testData = new[]
{
new KeyValuePair<int, string>(33, "33"),
new KeyValuePair<int, string>(28, "28"),
new KeyValuePair<int, string>(63, "63"),
new KeyValuePair<int, string>(20, "20"),
new KeyValuePair<int, string>(31, "31"),
new KeyValuePair<int, string>(39, "39"),
new KeyValuePair<int, string>(79, "79"),
new KeyValuePair<int, string>(10, "10"),
new KeyValuePair<int, string>(35, "35"),
new KeyValuePair<int, string>(60, "60"),
};
//Act
Array.ForEach(testData, t =>
{
bst.Add(t.Key, t.Value);
});
bst.Add(75, "75");
bst.Add(38, "38");
bst.Add(12, "12");
var result = bst.LevelOrderTraversal();
//Assert
Assert.AreEqual(result.ElementAt(0).Key, 33);
Assert.AreEqual(result.ElementAt(1).Key, 28);
Assert.AreEqual(result.ElementAt(2).Key, 63);
Assert.AreEqual(result.ElementAt(3).Key, 12);
Assert.AreEqual(result.ElementAt(4).Key, 31);
Assert.AreEqual(result.ElementAt(5).Key, 39);
Assert.AreEqual(result.ElementAt(6).Key, 79);
Assert.AreEqual(result.ElementAt(7).Key, 10);
Assert.AreEqual(result.ElementAt(8).Key, 20);
Assert.AreEqual(result.ElementAt(9).Key, 38);
Assert.AreEqual(result.ElementAt(10).Key, 60);
Assert.AreEqual(result.ElementAt(11).Key, 75);
Assert.AreEqual(result.ElementAt(12).Key, 35);
//Assert are red nodes
Assert.IsTrue(result.ElementAt(3).IsRed);
Assert.IsTrue(result.ElementAt(5).IsRed);
Assert.IsTrue(result.ElementAt(11).IsRed);
}