本文整理汇总了C#中BinarySearchTree.Search方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Search方法的具体用法?C# BinarySearchTree.Search怎么用?C# BinarySearchTree.Search使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Search方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IterativeInsertTest
public void IterativeInsertTest()
{
IBinarySearchTree<int> tree = new BinarySearchTree<int>(100);
tree.IterativeInsert(50);
tree.IterativeInsert(150);
tree.IterativeInsert(200);
Assert.IsTrue(tree.Search(150), "150 should be in the tree");
Assert.IsFalse(tree.Search(125), "125 should not be in the tree");
}
示例2: InsertRecursivelyTest
public void InsertRecursivelyTest()
{
IBinarySearchTree<int> tree = new BinarySearchTree<int>(100);
tree.InsertRecursively(50);
tree.InsertRecursively(150);
tree.InsertRecursively(200);
Assert.IsTrue(tree.Search(150), "150 should be in the tree");
Assert.IsFalse(tree.Search(125), "125 should not be in the tree");
}
示例3: Main
static void Main()
{
BinarySearchTree<int> tree = new BinarySearchTree<int>();
tree.Insert(23);
tree.Insert(45);
tree.Insert(16);
tree.Insert(37);
tree.Insert(3);
tree.Insert(99);
tree.Insert(22);
Console.Write("Inorder traversal: \nTree: ");
foreach (var node in tree)
Console.Write(node + " ");
Console.WriteLine("-> Root: {0}", tree.Root);
/* -------------- */
Console.WriteLine("\nSearch [9]: {0}", tree.Search(9)); // does not exist
Console.WriteLine("Search [Root]: {0}\n", tree.Search(tree.Root.Value));
/* -------------- */
BinarySearchTree<int> cloneTree = (BinarySearchTree<int>)tree.Clone();
Console.WriteLine("\nClone tree: {0} -> Root: {1}", cloneTree, cloneTree.Root);
/* -------------- */
Console.WriteLine("\n\ntree.Equals(cloneTree): {0}", tree.Equals(cloneTree));
Console.WriteLine("tree == cloneTree: {0}", tree == cloneTree);
Console.WriteLine("tree != cloneTree: {0}", tree != cloneTree);
/* -------------- */
tree.Insert(1); // this will change hash code of the tree
cloneTree.Insert(2); // this will change hash code of the tree
Console.WriteLine("\n\ntree.GetHashCode(): {0}", tree.GetHashCode());
Console.WriteLine("cloneTree.GetHashCode(): {0}\n", cloneTree.GetHashCode());
/* -------------- */
Console.WriteLine("\nTest TreeNode<T> internal class:");
var treeNode = new BinarySearchTree<int>.TreeNode<int>(25);
treeNode.LeftChild = new BinarySearchTree<int>.TreeNode<int>(23);
treeNode.RightChild = new BinarySearchTree<int>.TreeNode<int>(28);
Console.WriteLine("- Root value: {0}", treeNode.Value);
Console.WriteLine("- LeftChild: {0}", treeNode.LeftChild);
Console.WriteLine("- RightChild: {0}\n", treeNode.RightChild);
}
示例4: BinarySearchTreeTests_InsertTest
public void BinarySearchTreeTests_InsertTest()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
int key = 10;
string value = "Irsli";
tree.Insert(key, value);
Assert.IsTrue(tree.Count == 1);
Assert.IsNotNull(tree.Search(key));
Assert.IsTrue(tree.Search(key).Value == value);
}
示例5: BinarySearchTreeTests_InsertTest2
public void BinarySearchTreeTests_InsertTest2()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
tree.Insert(10, "John");
tree.Insert(5, "Clark");
tree.Insert(13, "Pitty");
int key = 103;
string value = "Irsli";
tree.Insert(key, value);
Assert.IsTrue(tree.Count == 4);
Assert.IsNotNull(tree.Search(key));
Assert.IsTrue(tree.Search(key).Value == value);
}
示例6: DeleteTest
public void DeleteTest()
{
BinarySearchTree target = new BinarySearchTree(); // TODO: Initialize to an appropriate value
int key = 0;
BinarySearchTree.Node delNode = target.Search(key);
target.Delete(delNode);
Assert.Inconclusive("Verify the correctness of this test method.");
}
示例7: RunBST
private static void RunBST()
{
var values = new[] { 15, 6, 20, 3, 7, 17, 19, 22, 2, 4, 13, 9 };
Console.WriteLine($"Building binary tree from: {Utils.FormatArray(values)} =>\n");
var tree = new BinarySearchTree<int>();
foreach (var v in values)
{
tree.Add(v);
}
Utils.PrintBinaryTree(tree.Root);
Console.WriteLine($"Minimum : {tree.Minimum().Value}");
Console.WriteLine($"Maximum : {tree.Maximum().Value}");
var searchValues = new[] { 9, 3, 100, 20, 2 };
Console.WriteLine($"Searching values {Utils.FormatArray(searchValues)}");
foreach(var v in searchValues)
{
var node = tree.Search(v);
Console.Write("The tree {0} value {1}. ",
node == null ? "does not contain" : "contains", v);
if (node != null)
{
var successor = BinarySearchTree<int>.Successor(node);
Console.Write("Successor {0}. ", successor != null ? successor.Value.ToString() : "N/A");
var predecessor = BinarySearchTree<int>.Predecessor(node);
Console.Write("Predecessor {0}.", predecessor != null ? predecessor.Value.ToString() : "N/A");
}
Console.WriteLine();
}
var deleteValues = new [] { 15 };
foreach (var v in deleteValues)
{
var node = tree.Search(v);
Console.WriteLine($"Deleting value: {v}");
tree.Delete(node);
Utils.PrintBinaryTree(tree.Root);
Console.WriteLine();
}
}
示例8: InsertRecursivelyTestHelper
private static void InsertRecursivelyTestHelper(int quantity)
{
IBinarySearchTree<int> tree = new BinarySearchTree<int>();
var listGenerator = new ListOfIntGenerator(quantity);
foreach (var randomInt in listGenerator.RandomInts)
{
tree.InsertRecursively(randomInt);
}
// test for presence
var randomEntry = listGenerator.FindRandomIntFromList();
Assert.IsTrue(tree.Search(randomEntry), message: $"{randomEntry} should be in the tree");
// test for absence
var entryThatIsNotInTheList = listGenerator.FindRandomIntThatIsNotInList();
Assert.IsFalse(tree.Search(entryThatIsNotInTheList),
message: $"{entryThatIsNotInTheList} should not be in the tree");
}
示例9: Main
static void Main()
{
BinarySearchTree tree = new BinarySearchTree();
tree.Add(2);
tree.Add(5);
tree.Add(10);
tree.Add(6);
tree.Add(4);
tree.Add(15);
tree.Delete(10);
tree.Delete(15);
Console.WriteLine(tree.Search(6));
Console.WriteLine(tree.Search(10));
foreach (var item in tree)
{
Console.WriteLine(item);
}
Console.WriteLine(tree);
BinarySearchTree secondTree = new BinarySearchTree();
secondTree.Add(2);
secondTree.Add(5);
secondTree.Add(10);
secondTree.Add(6);
secondTree.Add(4);
secondTree.Add(15);
secondTree.Delete(10);
secondTree.Delete(15);
Console.WriteLine(tree.Equals(secondTree));
Console.WriteLine(Object.ReferenceEquals(tree, secondTree));
Console.WriteLine(tree.GetHashCode() == secondTree.GetHashCode());
Console.WriteLine();
BinarySearchTree clonedTree = (BinarySearchTree)tree.Clone();
Console.WriteLine(tree.Equals(clonedTree));
Console.WriteLine(Object.ReferenceEquals(tree, clonedTree));
}
示例10: Main
static void Main(string[] args)
{
BinarySearchTree<int> tree = new BinarySearchTree<int>();
tree.Add(4);
tree.Add(3);
tree.Add(1);
tree.Add(100);
tree.Add(5);
BinarySearchTree<int> newTree = (BinarySearchTree<int>)tree.Clone();
tree.Delete(4);
Console.WriteLine(tree);
Console.WriteLine(newTree);
Console.WriteLine(tree.Search(100));
Console.WriteLine(tree.Equals(newTree));
}
示例11: Main
static void Main()
{
BinarySearchTree b=new BinarySearchTree(30);
//Node root=new Node(50);
b.Insert(b.root,70);
b.Insert(b.root,20);
b.Insert(b.root,40);
b.Insert(b.root,60);
//b.InOrder(b.root);
if(b.Search(b.root,5))
{
Console.WriteLine("found");
}
else
{
Console.WriteLine("not found");
}
}
示例12: BinarySearchTreeTests_SimpleTree_SearchTest
public void BinarySearchTreeTests_SimpleTree_SearchTest()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
tree.Insert(10, "John");
tree.Insert(5, "Clark");
tree.Insert(13, "Pitty");
IBinarySearchTreeNode<int, string> actual = tree.Search(5);
IBinarySearchTreeNode<int, string> actual2 = tree.Search(13);
IBinarySearchTreeNode<int, string> actual3 = tree.Search(10);
IBinarySearchTreeNode<int, string> actual4 = tree.Search(0);
Assert.IsTrue(actual.Key == 5 && actual.Value == "Clark");
Assert.IsTrue(actual2.Key == 13 && actual2.Value == "Pitty");
Assert.IsTrue(actual3.Key == 10 && actual3.Value == "John");
Assert.IsNull(actual4);
}
示例13: BinarySearchTreeTests_SearchTest
public void BinarySearchTreeTests_SearchTest()
{
IBinarySearchTree<int, int> tree = new BinarySearchTree<int, int>();
IBinarySearchTreeNode<int, int> actual = tree.Search(100);
Assert.IsNull(actual);
}
示例14: Search
public void Search()
{
var tree = new BinarySearchTree<int>();
var firstNode = new TNode<int>(7);
tree.Insert(firstNode);
var secondNode = new TNode<int>(4);
tree.Insert(secondNode);
var thirdNode = new TNode<int>(5);
tree.Insert(thirdNode);
var fourthNode = new TNode<int>(9);
tree.Insert(fourthNode);
Assert.AreEqual(secondNode, tree.Search(4),"#D01");
Assert.AreEqual(fourthNode, tree.Search(9), "#D02");
}
示例15: Main
public static void Main()
{
//Create binary search tree
BinarySearchTree<int> firstTree = new BinarySearchTree<int>();
firstTree.Root = new TreeNode<int>(10);
firstTree.Root.Left = new TreeNode<int>(5);
firstTree.Root.Right = new TreeNode<int>(20);
firstTree.Root.Left.Left = new TreeNode<int>(1);
firstTree.Root.Right.Right = new TreeNode<int>(24);
firstTree.Root.Left.Left.Right = new TreeNode<int>(3);
firstTree.Root.Right.Right.Right = new TreeNode<int>(35);
firstTree.Root.Right.Right.Right.Right = new TreeNode<int>(50);
//Test ToString() method
Console.WriteLine("Elements of firstTree");
Console.WriteLine(firstTree.ToString());
//Create another binary search tree
BinarySearchTree<int> secondTree = new BinarySearchTree<int>();
secondTree.Root = new TreeNode<int>(10);
secondTree.Root.Left = new TreeNode<int>(5);
secondTree.Root.Right = new TreeNode<int>(20);
secondTree.Root.Left.Left = new TreeNode<int>(1);
secondTree.Root.Right.Right = new TreeNode<int>(24);
secondTree.Root.Left.Left.Right = new TreeNode<int>(3);
secondTree.Root.Right.Right.Right = new TreeNode<int>(35);
secondTree.Root.Right.Right.Right.Right = new TreeNode<int>(49);
//Test Equals() method
Console.WriteLine("firstTree is equal to secondTree?:");
Console.WriteLine(firstTree.Equals(secondTree));
//Test Clone() method
Console.WriteLine("Elements of the clone of firstTree");
BinarySearchTree<int> clone = (BinarySearchTree<int>)firstTree.Clone();
Console.WriteLine(clone.ToString());
//Test Search() method
Console.WriteLine("firstTree contains 50?:");
Console.WriteLine(firstTree.Search(50));
Console.WriteLine("secondTree contains 50?:");
Console.WriteLine(secondTree.Search(50));
//Test Add() method
Console.WriteLine("Elements of firstTree after adding 22");
firstTree.Add(22);
Console.WriteLine(firstTree.ToString());
//Test Delete() method
Console.WriteLine("Elements of secondTree after deleting 24");
Console.WriteLine(secondTree.Delete(24));
Console.WriteLine(secondTree.ToString());
//Use foreach to test the implementation of IEnumerable<T> interface
clone.Add(100);
Console.WriteLine("firstTree after adding 100 to clone");
foreach (var item in firstTree)
{
Console.Write("{0} ", item);
}
Console.WriteLine("clone after adding 100 to clone");
foreach (var item in clone)
{
Console.Write("{0} ", item);
}
}