本文整理汇总了C#中BinarySearchTree.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Insert方法的具体用法?C# BinarySearchTree.Insert怎么用?C# BinarySearchTree.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanFindSuccessor
public void CanFindSuccessor()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(new KeyValue<int, int>(1, 2));
tree.Insert(new KeyValue<int, int>(4, 5));
tree.Insert(new KeyValue<int, int>(2, 7));
Assert.AreEqual(7, tree.Successor(1));
Assert.AreEqual(5, tree.Successor(2));
}
示例2: ConstructSampleBST
private static BinarySearchTree ConstructSampleBST()
{
var tree = new BinarySearchTree();
tree.Insert(5);
tree.Insert(3);
tree.Insert(1);
tree.Insert(4);
tree.Insert(6);
return tree;
}
示例3: AfterAddingIncreaseCount
public void AfterAddingIncreaseCount()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(1.AsKeyValue());
Assert.AreEqual(1, tree.Count);
tree.Insert(2.AsKeyValue());
Assert.AreEqual(2, tree.Count);
}
示例4: CanFindPredecessor
public void CanFindPredecessor()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(new KeyValue<int, int>(1, 2));
tree.Insert(new KeyValue<int, int>(3, 5));
tree.Insert(new KeyValue<int, int>(2, 7));
Assert.AreEqual(2, tree.Predecessor(2));
Assert.AreEqual(7, tree.Predecessor(3));
}
示例5: Main
// Define the data structure binary search tree with operations for
// "adding new element", "searching element" and "deleting elements".
// Implement the standard methods from System.Object – ToString(), Equals(…), GetHashCode()
// and the operators for comparison == and !=.
// Add and implement the ICloneable interface for deep copy of the tree.
// Implement IEnumerable<T> to traverse the tree.
internal static void Main()
{
// Create new binary search tree
BinarySearchTree<string> tree = new BinarySearchTree<string>();
// Test adding new element
tree.Insert("Telerik");
tree.Insert("Google");
tree.Insert("Microsoft");
tree.Insert("Facebook");
tree.Insert("Twitter");
// Test ToString()
Console.WriteLine("Tree: {0}", tree);
// Test searching element
Console.WriteLine("Tree contains Telerik? -> {0}", tree.Contains("Telerik"));
Console.WriteLine("Tree contains IBM? -> {0}", tree.Contains("IBM"));
// Test deleting element
tree.Remove("Telerik");
Console.WriteLine("Tree: remove Telerik");
Console.WriteLine("Tree: {0}", tree);
// Test GetHashCode
Console.WriteLine("Tree hash code = {0}", tree.GetHashCode());
// Test Equals
var otherTree = new BinarySearchTree<string>();
otherTree.Insert("Google");
Console.WriteLine("\nOther tree: {0}", otherTree);
Console.WriteLine("tree.Equals(otherTree) -> {0}", tree.Equals(otherTree));
// Test '=='
Console.WriteLine("Other tree: insert Microsoft, Facebook, Twitter");
otherTree.Insert("Microsoft");
otherTree.Insert("Facebook");
otherTree.Insert("Twitter");
Console.WriteLine("Other tree: {0}", otherTree);
Console.WriteLine("tree.Equals(otherTree) -> {0}", tree.Equals(otherTree));
// Test IEnumerable<T> to traverse the tree
Console.WriteLine("\nforeach (var node in tree):");
foreach (var node in tree)
{
Console.WriteLine(node);
}
// Test deep copy of the tree.
var treeClone = tree.Clone();
Console.WriteLine("\ntreeClone: {0}", treeClone);
tree.Remove("Google");
Console.WriteLine("Original tree: remove Google");
Console.WriteLine("treeClone: {0}", treeClone);
}
示例6: CanDeleteFromRightLeaf
public void CanDeleteFromRightLeaf()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(1.AsKeyValue());
tree.Insert(2.AsKeyValue());
tree.Insert(3.AsKeyValue());
tree.Delete(3);
CollectionAssert.AreEqual(new[] {1, 2}, tree.BreadthFirstSearchEnumerator().Select(_ => _.Value).ToList());
}
示例7: CanDeleteFromMiddleWithLeftChild
public void CanDeleteFromMiddleWithLeftChild()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(3.AsKeyValue());
tree.Insert(2.AsKeyValue());
tree.Insert(1.AsKeyValue());
tree.Delete(2);
CollectionAssert.AreEqual(new[] {3, 1}, tree.BreadthFirstSearchEnumerator().Select(_ => _.Value).ToList());
}
示例8: CanFindElementInComplexTree
public void CanFindElementInComplexTree()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(new KeyValue<int, int>(1, 2));
tree.Insert(new KeyValue<int, int>(3, 5));
tree.Insert(new KeyValue<int, int>(2, 7));
Assert.AreEqual(2, tree[1]);
Assert.AreEqual(7, tree[2]);
Assert.AreEqual(5, tree[3]);
}
示例9: CountInEnumeratorsMustMatchCountInTree
public void CountInEnumeratorsMustMatchCountInTree()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(1.AsKeyValue());
tree.Insert(3.AsKeyValue());
tree.Insert(2.AsKeyValue());
Assert.AreEqual(3, tree.BreadthFirstSearchEnumerator().Count());
Assert.AreEqual(3, tree.DepthFirstSearchEnumerator().Count());
}
示例10: PreOrderTest
public void PreOrderTest()
{
BinarySearchTree tree = new BinarySearchTree();
tree.Root = new NodeTree(4);
tree.Insert(new NodeTree(2));
tree.Insert(new NodeTree(3));
tree.Insert(new NodeTree(5));
tree.Insert(new NodeTree(6));
tree.Insert(new NodeTree(1));
Utilities.PreOrder(tree.Root);
Assert.IsTrue(true); // refactor it so that the traversal data is dumped to an IEnumerable
}
示例11: BinarySearchTests_GetHeight2
public void BinarySearchTests_GetHeight2()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
tree.Insert(10, "John");
tree.Insert(5, "Clark");
tree.Insert(13, "Pitty");
tree.Insert(17, "Lilly");
int height = tree.GetHeight();
Assert.AreEqual(2, height);
}
示例12: BinarySearchTreeTests_LCA
public void BinarySearchTreeTests_LCA()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
IBinarySearchTreeNode<int, string> john = tree.Insert(10, "John");
IBinarySearchTreeNode<int, string> clark = tree.Insert(5, "Clark");
IBinarySearchTreeNode<int, string> pitty = tree.Insert(13, "Pitty");
IBinarySearchTreeNode<int, string> ancestor = tree.LCA(john, pitty);
Assert.IsNotNull(ancestor);
Assert.AreEqual(john, ancestor);
}
示例13: InsertTest
public void InsertTest()
{
var tree = new BinarySearchTree<int>();
tree.Insert(10);
Assert.AreEqual(1, tree.Height);
Assert.IsNotNull(tree.Head);
tree.Insert(8);
Assert.AreEqual(2, tree.Height);
tree.Insert(12);
Assert.AreEqual(3, tree.Height);
}
示例14: AppearInBinarySerachTreeOrder
public void AppearInBinarySerachTreeOrder()
{
var tree = new BinarySearchTree<int, int>();
var element1 = 1.AsKeyValue();
var element2 = 2.AsKeyValue();
var element3 = 3.AsKeyValue();
tree.Insert(element2);
tree.Insert(element3);
tree.Insert(element1);
var elements = tree.BreadthFirstSearchEnumerator();
Assert.AreEqual(elements.ElementAt(0), element2);
Assert.AreEqual(elements.ElementAt(1), element1);
Assert.AreEqual(elements.ElementAt(2), element3);
}
示例15: CanFindMinimumInOneElementTree
public void CanFindMinimumInOneElementTree()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(1.AsKeyValue());
Assert.AreEqual(1, tree.Minimum);
}