本文整理汇总了C#中BinarySearchTree.TraverseInOrder方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.TraverseInOrder方法的具体用法?C# BinarySearchTree.TraverseInOrder怎么用?C# BinarySearchTree.TraverseInOrder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.TraverseInOrder方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnitTest
public static void UnitTest()
{
Console.WriteLine ("----- Testing BinarySearchTree<int> -----");
Console.Write ("Creating new BinarySearchTree of type <int>...");
BinarySearchTree<int> test = new BinarySearchTree<int> (); d ();
Console.Write ("Adding value 50...");
test.Add (50); d ();
Console.Write ("Adding value 25...");
test.Add (25); d ();
Console.Write ("Adding value 75...");
test.Add (75); d ();
Console.Write ("Checking for correct structure (25/50\\75)...");
Debug.Assert (test.root.value == 50);
Debug.Assert (test.root.nodes[0].value == 25);
Debug.Assert (test.root.nodes[1].value == 75);
d ();
Console.Write ("Checking if BinarySearchTree.Contains(75) works...");
Debug.Assert (test.Contains(75)); d ();
Console.Write ("Testing BinarySearchTree.TraversePre()...");
Debug.Assert( test.TraversePre ((x) => Console.Write (x.value + " ")) == "50 25 75 "); d ();
Console.Write ("Testing BinarySearchTree.TraverseInOrder()...");
Debug.Assert( test.TraverseInOrder ((x) => Console.Write (x.value + " ")) == "25 50 75 "); d ();
Console.Write ("Testing BinarySearchTree.TraversePost()...");
Debug.Assert( test.TraversePost ((x) => Console.Write (x.value + " ")) == "25 75 50 "); d ();
Console.WriteLine ("----- Testing BinarySearchTree<string> -----");
Console.Write ("Creating new BinarySearchTree of type <string> with custom IComparer...");
BinarySearchTree<string> foo = new BinarySearchTree<string> ( new StringComparer() ); d ();
Console.Write ("Adding value Urist McSmashes...");
foo.Add ("Urist McSmashes"); d ();
Console.Write ("Adding value Gordon Freeman...");
foo.Add ("Gordon Freeman"); d ();
Console.Write ("Adding value Winston Smith...");
foo.Add ("Winston Smith"); d ();
Console.Write ("Checking for correct structure (Gordon Freeman/Urist McSmashes\\Winston Smith)...");
Debug.Assert (foo.root.value == "Urist McSmashes");
Debug.Assert (foo.root.nodes[0].value == "Gordon Freeman");
Debug.Assert (foo.root.nodes[1].value == "Winston Smith");
d ();
Console.WriteLine ("----- Testing BinaryExpressionTree -----");
Console.Write ("Creating new BinaryExpressionTree with expression \"5 + 2 * 8 - 6 / 4\"...");
BinaryExpressionTree bar = new BinaryExpressionTree ("5 + 2 * 8 - 6 / 4"); d ();
Console.Write ("Checking for correct structure...");
Debug.Assert( bar.TraverseInOrder ((x) => Console.Write (x.value + " ")) == "4 47 6 45 8 42 2 43 5 ");
Debug.Assert( bar.root.value == 45 ); // -
Debug.Assert( bar.root.nodes[0].value == 47 ); // /
Debug.Assert( bar.root.nodes[0].nodes[0].value == 4 );
Debug.Assert( bar.root.nodes[0].nodes[1].value == 6 );
Debug.Assert( bar.root.nodes[1].value == 43 ); // +
Debug.Assert( bar.root.nodes[1].nodes[1].value == 5 );
Debug.Assert( bar.root.nodes[1].nodes[0].value == 42 ); // *
Debug.Assert( bar.root.nodes[1].nodes[0].nodes[0].value == 8 );
Debug.Assert( bar.root.nodes[1].nodes[0].nodes[1].value == 2 );
d ();
Console.Write ("Checking for correct answer \"5 + 2 * 8 - 6 / 4\" = " + (5.0 + 2.0 * 8.0 - 6.0 / 4.0) + "...");
Debug.Assert (bar.Eval () == (5.0 + 2.0 * 8.0 - 6.0 / 4.0)); d ();
Console.WriteLine ("Everything appears to be in order! Although asserts in C# don't actually end execution so there's no real way for me to know!");
}
示例2: AssertIsValid
public void AssertIsValid(BinarySearchTree<int> tree)
{
bool isValid = true;
tree.TraverseInOrder(node =>
{
isValid =
(node.LeftChild == null || node.LeftChild.Payload <= node.Payload) &&
(node.RightChild == null || node.RightChild.Payload > node.Payload);
}
);
Assert.AreEqual(true, isValid);
}
示例3: BinarySearchTreeTests_Traverse
public void BinarySearchTreeTests_Traverse()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
tree.Insert(10, "John");
tree.Insert(5, "Clark");
tree.Insert(13, "Pitty");
tree.Insert(17, "Lilly");
IList<IBinarySearchTreeNode<int, string>> inOrderTraversedTree = tree.TraverseInOrder().ToList();
Assert.IsTrue(inOrderTraversedTree.Count == 4);
Assert.IsTrue(inOrderTraversedTree[0].Key == 5);
Assert.IsTrue(inOrderTraversedTree[1].Key == 10);
Assert.IsTrue(inOrderTraversedTree[2].Key == 13);
Assert.IsTrue(inOrderTraversedTree[3].Key == 17);
}
示例4: BinarySearchTreeTests_Remove5
public void BinarySearchTreeTests_Remove5()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
tree.Insert(10, "John");
tree.Insert(5, "Clark");
tree.Insert(8, "Jack");
tree.Insert(3, "Lui");
tree.Insert(7, "Lu-lu");
tree.Insert(6, "Ben");
tree.Insert(1, "Lui");
tree.Insert(2, "Lui");
tree.Insert(0, "Lui");
int[] expectedKeys = { 0, 1, 2, 3, 6, 7, 8, 10 };
bool actualResult = tree.Remove(5);
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);
}
示例5: 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);
}
示例6: BinarySearchTreeTests_Remove1
public void BinarySearchTreeTests_Remove1()
{
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
tree.Insert(10, "John");
bool actualResult = tree.Remove(10);
var actualKeys = tree.TraverseInOrder().Select(n => n.Key);
Assert.IsTrue(actualResult);
Assert.IsTrue(tree.Count == 0);
Assert.IsTrue(actualKeys.Count() == 0);
}
示例7: BinarySearchTreeTests_Random_Remove
public void BinarySearchTreeTests_Random_Remove()
{
Random r = new Random();
int n =(r.Next(200)+ 1) * (r.Next(200)+ 1);
List<int> expectedKeys = new List<int>();
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
for (int i = 0; i < n; i++)
{
int newKey = r.Next(int.MaxValue);
expectedKeys.Add(newKey);
tree.Insert(newKey, string.Empty);
}
int k = r.Next((int)Math.Sqrt(n)) + 1;
var toRemove = expectedKeys.OrderBy(_ => r.NextDouble()).Take(k);
expectedKeys.Sort();
foreach(var key in toRemove)
{
bool actualResult = tree.Remove(key);
expectedKeys.Remove(key);
var actualKeys = tree.TraverseInOrder().Select(_ => _.Key);
Assert.IsTrue(actualResult);
Assert.IsTrue(tree.Count == expectedKeys.Count);
Assert.IsTrue(expectedKeys.Except(actualKeys).Count() == 0);
Assert.IsTrue(actualKeys.Except(expectedKeys).Count() == 0);
}
}
示例8: BinarySearchTreeTests_Random_Mirror
public void BinarySearchTreeTests_Random_Mirror()
{
Random r = new Random();
int n = r.Next(1000000) + 1;
IBinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();
for (int i = 0; i < n; i++)
{
int key = r.Next(int.MaxValue);
tree.Insert(key, string.Empty);
}
var expectedOrder = tree.TraverseInOrder().Reverse();
tree.Mirror();
var actualOrder = tree.TraverseInOrder().ToList();
Assert.IsTrue(expectedOrder.Except(actualOrder).Count() == 0);
Assert.IsTrue(actualOrder.Except(expectedOrder).Count() == 0);
}
示例9: BinarySearchTreeTests_Mirror
public void BinarySearchTreeTests_Mirror()
{
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");
var expectedOrder = tree.TraverseInOrder().Reverse();
tree.Mirror();
var actualOrder = tree.TraverseInOrder().ToList();
Assert.IsTrue(expectedOrder.Except(actualOrder).Count() == 0);
Assert.IsTrue(actualOrder.Except(expectedOrder).Count() == 0);
}