本文整理汇总了C#中BinarySearchTree.Delete方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Delete方法的具体用法?C# BinarySearchTree.Delete怎么用?C# BinarySearchTree.Delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Delete方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
var sb = new StringBuilder();
var tree = new BinarySearchTree<int>(15);
for (int i = 0; i < 30; i++)
{
tree.Add(i);
}
var clonedNode = (TreeNode<int>)tree.Root.Clone();
sb.AppendLine(tree.ToString())
.AppendLine("Tree root: " + tree.Root.ToString())
.AppendLine("Tree contains 7? " + tree.Contains(7).ToString())
.AppendLine("Cloned root: " + clonedNode.ToString())
.AppendLine("Cloned Equals root? " + (clonedNode.Equals(tree.Root)).ToString())
.AppendLine("Cloned == root? " + (clonedNode == tree.Root).ToString())
.AppendLine("Cloned != root? " + (clonedNode != tree.Root).ToString())
.AppendLine("12 deleted. New tree:");
Console.Write(sb.ToString());
tree.Delete(12);
Console.WriteLine(tree.ToString());
}
示例2: BST_Delete
public void BST_Delete()
{
var BST = new BinarySearchTree<int, object>();
BST[4] = 0; BST[3] = 0; BST[2] = 0; BST[1] = 0;
BST[5] = 0; BST[6] = 0; BST[7] = 0;
BST.Delete(4);
BST.Delete(6);
var keys = BST.Enumerator();
string actual = "";
foreach (var item in keys)
{
actual += item.ToString();
}
Assert.AreEqual("12357", actual);
}
示例3: 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.");
}
示例4: CanDeleteFromMiddleWithRightChild
public void CanDeleteFromMiddleWithRightChild()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(1.AsKeyValue());
tree.Insert(2.AsKeyValue());
tree.Insert(3.AsKeyValue());
tree.Delete(2);
CollectionAssert.AreEqual(new[] {1, 3}, tree.BreadthFirstSearchEnumerator().Select(_ => _.Value).ToList());
}
示例5: CanDeleteLeafNode
public void CanDeleteLeafNode()
{
//Arrange
var tree = new BinarySearchTree<int, int> { { 2, 2 }, { 1, 1 }, { 3, 3 }, { 4, 4 } };
var expected = new[] { 1, 2, 3 }.ToList();
//Act
tree.Delete(4);
var result = tree.Select(r => r.KeyValue.Value).ToList();
//Assert
CollectionAssert.AreEquivalent(expected, result);
}
示例6: CanDeleteFromLeftLeaf
public void CanDeleteFromLeftLeaf()
{
var tree = new BinarySearchTree<int, int>();
tree.Insert(3.AsKeyValue());
tree.Insert(2.AsKeyValue());
tree.Insert(1.AsKeyValue());
tree.Delete(1);
CollectionAssert.AreEqual(new[] {3, 2}, tree.BreadthFirstSearchEnumerator().Select(_ => _.Value).ToList());
}
示例7: 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));
}
示例8: Delete
public void Delete()
{
var tree = new BinarySearchTree<int>();
var firstNode = new TNode<int>(17);
tree.Insert(firstNode);
var secondNode = new TNode<int>(7);
tree.Insert(secondNode);
var thirdNode = new TNode<int>(4);
tree.Insert(thirdNode);
var fourthNode = new TNode<int>(15);
tree.Insert(fourthNode);
tree.Insert(new TNode<int>(10));
tree.Insert(new TNode<int>(16));
tree.Insert(new TNode<int>(8));
tree.Insert(new TNode<int>(12));
tree.Delete(secondNode);
var result = tree.CollectInOrder().ToArray();
var expected = new int[] {4,8,10,12,15,16,17};
for(int i=0; i < expected.Length; i++)
{
Assert.AreEqual(expected[i], result[i], "#E"+i);
}
tree.Delete(thirdNode);
result = tree.CollectInOrder().ToArray();
expected = new int[] {8, 10, 12, 15, 16, 17 };
for (int i = 0; i < expected.Length; i++)
{
Assert.AreEqual(expected[i], result[i], "#EE" + i);
}
tree.Delete(firstNode);
result = tree.CollectInOrder().ToArray();
expected = new int[] { 8, 10, 12, 15, 16};
for (int i = 0; i < expected.Length; i++)
{
Assert.AreEqual(expected[i], result[i], "#EEE" + i);
}
}
示例9: 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();
}
}
示例10: Main
static void Main()
{
BinarySearchTree<int> intTree = new BinarySearchTree<int>();
intTree.Add(3);
intTree.Add(1);
intTree.Add(6);
Console.WriteLine("Call intTree.ToString();");
Console.WriteLine(intTree);
Console.WriteLine("\nUse foreach:");
foreach (var item in intTree)
{
Console.WriteLine(item);
}
BinarySearchTree<int> compareTree = intTree;
Console.WriteLine("\nIs intTree equals with compareTree: {0}", compareTree.Equals(intTree));
compareTree = new BinarySearchTree<int>();
compareTree.Add(7);
Console.WriteLine("\nIs intTree != with compareTree: {0}", compareTree != intTree);
Console.WriteLine("\nIs intTree equals with compareTree: {0}", compareTree.Equals(intTree));
Console.WriteLine("\nTest Clone");
compareTree = intTree.Clone();
Console.WriteLine("intTree: {0}", intTree);
Console.WriteLine("compareTree: {0}", compareTree);
int searchedNumber = -3;
Console.WriteLine("\nintTree contains {0} = {1}", searchedNumber, intTree.Contains(searchedNumber));
int deleteNumber = 3;
intTree.Add(3);
Console.WriteLine("\nintTree before deleting: {0}", intTree);
Console.WriteLine("\nintTree Delete {0}, deleted elements are: {1}", deleteNumber, intTree.Delete(deleteNumber));
Console.WriteLine("\nintTree after deleting: {0}", intTree);
}
示例11: 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));
}
示例12: CanDeleteRootNodeWithTwoChildrenNestedBinaryTreeUseSuccessor
public void CanDeleteRootNodeWithTwoChildrenNestedBinaryTreeUseSuccessor()
{
//Arrange
var tree = new BinarySearchTree<int, int> { { 100, 100 }, { 200, 200 }, { 50, 50 }, { 40, 40 }, { 25, 25 }, { 26, 26 }, { 60, 60 }, { 20, 20 } };
tree.ForceDeleteType = InOrderNode.Successor;
var expected = new[] { 20, 25, 26, 40, 50, 60, 200 }.ToList();
//Act
tree.Delete(100);
var result = tree.Select(r => r.KeyValue.Value).ToList();
//Assert
CollectionAssert.AreEquivalent(expected, result);
}
示例13: CanDeleteNodeWithOneLeftChild
public void CanDeleteNodeWithOneLeftChild()
{
//Arrange
var tree = new BinarySearchTree<int, int> { { 9, 9 }, { 1, 1 }, { 3, 3 }, { 2, 2 }, { 10, 10 }, { 11, 11 } };
var expected = new[] { 1, 2, 9, 10, 11 }.ToList();
//Act
tree.Delete(3);
var result = tree.Select(r => r.KeyValue.Value).ToList();
//Assert
CollectionAssert.AreEquivalent(expected, result);
}
示例14: 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);
}
}
示例15: Delete_NodeIsNotRootAndHasTwoChildren_HasCorrectOrder
public void Delete_NodeIsNotRootAndHasTwoChildren_HasCorrectOrder()
{
testTree = new BinarySearchTree<int>(new[] { 5, 3, 2, 4, 7, 6, 8 });
testTree.Delete(7);
Debug.WriteLine(testTree.PreOrder());
Assert.IsTrue(IsCorrectOrder(testTree.PreOrder(), "532486"));
}