本文整理汇总了C#中BinarySearchTree.ToList方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.ToList方法的具体用法?C# BinarySearchTree.ToList怎么用?C# BinarySearchTree.ToList使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.ToList方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BinaryTreeMustSortTheSameAsSortedDictionary
public void BinaryTreeMustSortTheSameAsSortedDictionary()
{
// Arrange
var asm = Assembly.GetExecutingAssembly();
var importer = new Importer(asm.GetManifestResourceStream("ClientImport.Tests.records.txt"), ',');
var dictionary = new SortedDictionary<string, IPerson>();
var tree = new BinarySearchTree<string, IPerson>();
//Act
importer.Data
.ToList()
.ForEach(record =>
{
var person = new Person
{
FirstName = record.FirstName,
Surname = record.Surname,
Age = Convert.ToInt16(record.Age)
};
var key = PersonRepository.BuildKey(person, SortKey.SurnameFirstNameAge);
if (tree.Find(key) == null)
tree.Add(key, person);
}
);
tree
.ToList()
.Shuffle() //Shuffle result from binary tree, to test sorting
.ForEach(r => dictionary.Add(PersonRepository.BuildKey(r.KeyValue.Value, SortKey.SurnameFirstNameAge), r.KeyValue.Value));
var expected = dictionary.Select(r => r.Value).ToList();
var actual = tree.Select(n => n.KeyValue.Value).ToList();
// Assert
CollectionAssert.AreEqual(expected, actual);
}
示例2: TreeTraversalIsSortedInAscendingOrder
public void TreeTraversalIsSortedInAscendingOrder()
{
// Arrange
var tree = new BinarySearchTree<int, int>();
var keys = Enumerable.Range(1, 50).ToList();
keys.Shuffle().ForEach(k => tree.Add(k, k));
var expected = keys.OrderBy(k => k).ToList();
//Act
var traversed = tree.ToList();
//Assert
CollectionAssert.AreEqual(expected, traversed.Select(r => r.KeyValue.Key).ToList());
}
示例3: TreeIsNotDegenerateWhenGivenUnSortedData
public void TreeIsNotDegenerateWhenGivenUnSortedData()
{
// Arrange
var tree = new BinarySearchTree<int, int>();
var count = 0;
var keys = Enumerable.Range(1, 50).ToList();
keys.Shuffle().ForEach(k => tree.Add(k, k));
//Act
var traversed = tree.ToList();
//Assert
traversed.ForEach(currentNode =>
{
if (currentNode.Parent == null) return;
if (currentNode.Left != null)
count++;
});
Assert.IsTrue(count > 0);
}
示例4: RighttKeyIsAlwaysGreaterThanParentKey
public void RighttKeyIsAlwaysGreaterThanParentKey()
{
// Arrange
var tree = new BinarySearchTree<int, int>();
var keys = Enumerable.Range(1, 50).ToList();
keys.Shuffle().ForEach(k => tree.Add(k, k));
//Act
var traversed = tree.ToList();
//Assert
traversed.ForEach(currentNode =>
{
if (currentNode.Parent == null) return;
if (currentNode.Parent.Right == currentNode)
Assert.IsTrue(currentNode.KeyValue.Key > currentNode.Parent.KeyValue.Key);
});
}
示例5: DefaultRootNodeIsInitializedEvenWhenEmpty
public void DefaultRootNodeIsInitializedEvenWhenEmpty()
{
// Arrange
var data = new BinarySearchTree<string, IPerson>();
//Act
var result = data.ToList();
//Assert
Assert.AreEqual(result.Count, 0);
}
示例6: TestGetValues
public void TestGetValues()
{
var tree = new BinarySearchTree<int>();
var size = 10000;
var random = new Random();
var numbers = new int[size];
for (int i = 0; i < size; i++)
{
numbers[i] = random.Next();
tree.Add(numbers[i]);
}
var values = tree.ToList();
Assert.AreEqual(values.Count, numbers.Length);
foreach (var n in numbers)
{
Assert.IsTrue(values.Contains(n));
values.Remove(n);
}
Assert.AreEqual(values.Count, 0);
}