本文整理汇总了C#中BinarySearchTree.Find方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Find方法的具体用法?C# BinarySearchTree.Find怎么用?C# BinarySearchTree.Find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Find方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: Main
static void Main(string[] args)
{
BinarySearchTree<int> tree1 = new BinarySearchTree<int>();
// test the add method
tree1.Add(2);
tree1.Add(1);
tree1.Add(3);
tree1.Add(7);
tree1.Add(5);
// test the Clone() method
BinarySearchTree<int> tree2 = (BinarySearchTree<int>)tree1.Clone();
// test the ToString() method
Console.WriteLine("Tree 1: {0}", tree1);
Console.WriteLine("Tree 2: {0}", tree2);
// test the Equals() method
Console.WriteLine("The two trees are equal: {0}",
(tree1 == tree2));
// test the Delete() method
tree2.Delete(7);
Console.WriteLine("After deleting 5 from tree 2: {0}", tree2);
// test the Find() method
var node = tree1.Find(3);
if(node != null)
{
Console.WriteLine("3 was found in tree 1");
}
else
{
Console.WriteLine("3 was NOT found in tree 1");
}
}