本文整理汇总了C#中BinarySearchTree.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Remove方法的具体用法?C# BinarySearchTree.Remove怎么用?C# BinarySearchTree.Remove使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Remove方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: KeyValuePair
public void KeyValuePair()
{
var tree = new BinarySearchTree<int, string>
{
{4, "4"},
{6, "6"},
{2, "2"},
{5, "5"},
{19, "19"},
{1, "1"}
};
Assert.AreEqual(tree.Count, 6);
Assert.IsTrue(tree.ContainsKey(4));
Assert.IsTrue(tree.ContainsKey(6));
Assert.IsTrue(tree.ContainsKey(2));
Assert.IsTrue(tree.ContainsKey(5));
Assert.IsTrue(tree.ContainsKey(19));
Assert.IsTrue(tree.ContainsKey(1));
Assert.IsFalse(tree.Remove(new KeyValuePair<int, string>(20, "20")));
Assert.IsTrue(tree.Remove(new KeyValuePair<int, string>(4, "4")));
Assert.AreEqual(tree.Count, 5);
Assert.IsFalse(tree.ContainsKey(4));
Assert.IsTrue(tree.ContainsKey(6));
Assert.IsTrue(tree.ContainsKey(2));
Assert.IsTrue(tree.ContainsKey(5));
Assert.IsTrue(tree.ContainsKey(19));
Assert.IsTrue(tree.ContainsKey(1));
}
示例2: 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);
}
示例3: Main
static void Main()
{
BinarySearchTree<int> bTree = new BinarySearchTree<int>();
AddSomeElements(bTree);
bTree.PrintTree();
Console.WriteLine("\n\nRemove 20 and 17\n\n");
bTree.Remove(20);
bTree.Remove(17);
bTree.PrintTree();
Console.Read();
}
示例4: RemoveTest
public void RemoveTest()
{
var bst = new BinarySearchTree<int>() { 10, 15, 5, 4, 7, 20, 14, 0, -5, -8, -2, -1 };
bst.Remove(20);
bst.Remove(0);
bst.Remove(10);
// TODO: Dump as array then compare against expected array
Assert.IsFalse(bst.Contains(20), "Did not remove element correctly");
Assert.IsFalse(bst.Contains(0), "Did not remove element correctly");
Assert.IsFalse(bst.Contains(10), "Did not remove root element correctly");
}
示例5: BinaryTreeAlgorithms_Test
public void BinaryTreeAlgorithms_Test()
{
var tree1 = new BinarySearchTree<int>();
tree1.Add(24);
tree1.Add(12);
tree1.Add(15);
tree1.Add(67);
tree1.Add(32);
tree1.Add(13);
tree1.Add(89);
tree1.Add(9);
tree1.Remove(13);
tree1.Remove(24);
//Assert.IsTrue(tree1.PathSum(tree1.Root(), 45));
//Assert.IsTrue(tree1.PathSum(tree1.Root(), 51));
//Assert.IsFalse(tree1.PathSum(tree1.Root(), 42));
//Assert.AreEqual(8, tree1.Size(tree1.Root()));
//Assert.AreEqual(9, tree1.Minimum(tree1.Root()));
//Assert.AreEqual(89, tree1.Maximum(tree1.Root()));
//Assert.IsTrue(BinaryTree.AreTwoTreesEqual(tree1.Root(), tree2.Root()));
Console.WriteLine("Inorder traversal ");
tree1.Inorder(tree1.Root);
Console.WriteLine(" ");
TreeAlgorithms<int>.Mirror(tree1.Root);
Console.WriteLine("Inorder traversal of the Mirror tree");
tree1.Inorder(tree1.Root);
Console.WriteLine(" ");
Console.WriteLine();
Console.WriteLine("Preorder traversal");
tree1.Preorder(tree1.Root);
Console.WriteLine(" ");
Console.WriteLine();
Console.WriteLine("Postorder traversal");
tree1.Postorder(tree1.Root);
Console.WriteLine(" ");
}
开发者ID:SaurabhNijhawan,项目名称:CSharpAlgorithmsAndDataStructures,代码行数:46,代码来源:BinaryTreeAlgorithmsTests.cs
示例6: Main
/// <summary>
/// Runs a basic test of a Binary Search Tree
/// </summary>
static void Main(string[] args)
{
// ==== Assign ==== //
BinarySearchTree<int> bTree = new BinarySearchTree<int>();
// ==== Act ==== //
{
// Insert
bTree.Insert(50);
bTree.Insert(30);
bTree.Insert(70);
bTree.Insert(15);
bTree.Insert(45);
bTree.Insert(55);
bTree.Insert(85);
bTree.Insert(19);
bTree.Insert(75);
bTree.Insert(77);
bTree.Insert(17);
// Remove
bTree.Remove(85);
bTree.Remove(70);
}
// ==== Assert ==== //
{
Console.WriteLine("Does it contain 55: " + bTree.Contains(55));
// Draw Tree
Console.WriteLine("\nDisplay the tree:");
Console.WriteLine(bTree.Display());
PrintOrdered(bTree);
}
// Wait for input before closing.
Console.ReadLine();
}
示例7: Main
static void Main()
{
BinarySearchTree<string> tree =
new BinarySearchTree<string>();
tree.Insert("Telerik");
tree.Insert("Google");
tree.Insert("Microsoft");
tree.Insert("ATNT");
tree.Insert("Blizoo");
tree.Insert("MTel");
tree.Insert("ZaraLab");
tree.Insert("Yahoo");
tree.PrintTreeDFS(); // Google Microsoft Telerik
Console.WriteLine(tree.Contains("Telerik")); // True
Console.WriteLine(tree.Contains("IBM")); // False
tree.Remove("Telerik");
Console.WriteLine(tree.Contains("Telerik")); // False
tree.PrintTreeDFS(); // Google Microsoft
}
示例8: Main
private static void Main(string[] args)
{
BinaryTree bt = new BinarySearchTree();
bt.Insert(6);
bt.Insert(2);
bt.Insert(8);
bt.Insert(1);
bt.Insert(5);
bt.Insert(10);
bt.Insert(3);
bt.Insert(4);
bt.Remove(1);
bt.Remove(2);
bt.Remove(3);
bt.Remove(4);
bt.Remove(5);
bt.Remove(6);
bt.Remove(8);
bt.Remove(10);
}
示例9: TestTreeCount
public void TestTreeCount()
{
var tree = new BinarySearchTree<int>();
var size = 100;
Node<int> node = null;
for (int i = 0; i < size; i++)
{
node = tree.Add(i);
}
Assert.AreEqual(tree.Count, size);
tree.Remove(node);
Assert.AreEqual(tree.Count, size - 1);
tree.Add(101);
Assert.AreEqual(tree.Count, size);
}
示例10: RemoveNodeNotFoundTest
public void RemoveNodeNotFoundTest()
{
BinarySearchTree<int> bst = new BinarySearchTree<int> {10, 7, 12};
Assert.IsFalse(bst.Remove(4));
}
示例11: RemoveTreeHasNoItemsTest
public void RemoveTreeHasNoItemsTest()
{
BinarySearchTree<int> actual = new BinarySearchTree<int>();
actual.Remove(10);
}
示例12: BinarySearchTreeTests_Remove4
public void BinarySearchTreeTests_Remove4()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
tree.Insert(10, "John");
tree.Insert(5, "Clark");
tree.Insert(13, "Pitty");
tree.Insert(17, "Lilly");
tree.Insert(8, "Jack");
tree.Insert(0, "Lui");
tree.Insert(16, "Petr");
int[] expectedKeys = { 8, 10, 16, 17 };
bool actualResult = tree.Remove(5) && tree.Remove(13) && tree.Remove(0);
var actualKeys = tree.TraverseInOrder().Select(n => n.Key);
Assert.IsTrue(actualResult);
Assert.IsTrue(tree.Count == expectedKeys.Length);
Assert.IsTrue(expectedKeys.Except(actualKeys).Count() == 0);
Assert.IsTrue(actualKeys.Except(expectedKeys).Count() == 0);
}
示例13: BinarySearchTreeTests_Remove0
public void BinarySearchTreeTests_Remove0()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
bool actualResult = tree.Remove(10);
Assert.IsFalse(actualResult);
}
示例14: RemoveLeafValueGreaterThanOrEqualToParentTest
public void RemoveLeafValueGreaterThanOrEqualToParentTest()
{
BinarySearchTree<int> bst = new BinarySearchTree<int> {10, 7, 12};
Assert.IsTrue(bst.Remove(12));
Assert.IsNull(bst.Root.Right);
Assert.AreEqual(2, bst.Count);
}
示例15: RemoveLeafValueLessThanParentTest
public void RemoveLeafValueLessThanParentTest()
{
BinarySearchTree<int> bst = new BinarySearchTree<int> {10, 7, 12, 11};
Assert.IsTrue(bst.Remove(7));
Assert.IsNull(bst.Root.Left);
Assert.AreEqual(3, bst.Count);
}