本文整理汇总了C#中BinaryTree类的典型用法代码示例。如果您正苦于以下问题:C# BinaryTree类的具体用法?C# BinaryTree怎么用?C# BinaryTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BinaryTree类属于命名空间,在下文中一共展示了BinaryTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
BinaryTree<int> tree = new BinaryTree<int>();
tree.Insert(2);
tree.Insert(3);
tree.Insert(5);
tree.Insert(8);
tree.Insert(11);
tree.Insert(200);
tree.Insert(120);
tree.Insert(600);
Console.WriteLine(tree);
Console.WriteLine(tree.Count);
tree.Remove(2);
tree.Remove(5);
tree.Remove(8);
Console.WriteLine(tree);
tree.Remove(11);
tree.Remove(200);
tree.Remove(120);
tree.Remove(600);
tree.Remove(3);
Console.WriteLine(tree);
Console.WriteLine(tree.Count);
tree.Insert(10);
tree.Insert(20);
tree.Insert(30);
tree.Insert(60);
tree.Insert(1);
tree.Insert(500);
tree.Insert(2);
Console.WriteLine(tree);
Console.WriteLine(tree.Count);
var newTree = new BinaryTree<int>();
newTree.Insert(10);
newTree.Insert(20);
newTree.Insert(30);
newTree.Insert(60);
newTree.Insert(1);
newTree.Insert(500);
newTree.Insert(2);
Console.WriteLine(newTree);
Console.WriteLine(newTree.Count);
Console.WriteLine(tree.Equals(newTree));
foreach (var value in tree)
{
Console.WriteLine(value);
}
BinaryTree<int> r = null;
Console.WriteLine(tree.Equals(r));
int a = 5;
Console.WriteLine(a.Equals(null));
}
示例2: GetDeepFirstOrder_ShouldReturnExpectedList
public void GetDeepFirstOrder_ShouldReturnExpectedList()
{
//Arrange
var sut = new BinaryTree(1);
var root = sut.GetRoot();
var node2 = sut.AddLeftChild(2, root);
var node3 = sut.AddRightChild(3, root);
var node4 = sut.AddLeftChild(4, node2);
var node5 = sut.AddRightChild(5, node3);
var node6 = sut.AddLeftChild(6, node4);
var node7 = sut.AddRightChild(7, node4);
var node8 = sut.AddRightChild(8, node5);
var node9 = sut.AddLeftChild(9, node8);
var node10 = sut.AddRightChild(10, node8);
var controlList = new List<int>
{
1, 2, 4, 6, 7, 3, 5, 8, 9, 10
};
//Act
var resultsList = new List<int>();
resultsList = sut.GetDeepFirstOrder(resultsList, root);
//Assert
CollectionAssert.AreEqual(controlList, resultsList);
}
示例3: Connect
public TreeNodeWithNext Connect(BinaryTree input)
{
TreeNodeWithNext root = input.ConvertToTreeNodeWithNext();
Connect(root);
return root;
}
示例4: Create
public BinaryTree<Script> Create()
{
var tree = new BinaryTree<Script>(ScriptType.Quali_Email.ToString())
{
Root = new BinaryTreeNode<Script>
{
Value = new Script
{
Question = "Is it okay if we can send you our company profile by email?"
},
Right = new BinaryTreeNode<Script>
{
Value = new Script
{
Question = "Could I get your email address?",
Actions = new Collection<ScriptAction>
{
new UpdateEmail()
}
},
Right = End(true),
Left = End(true)
},
Left = End(true)
}
};
return tree;
}
示例5: Simple_Create
public void Simple_Create()
{
var list = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
var tree = new BinaryTree<int>();
tree.Add(list[3]);
tree.Add(list[1]);
tree.Add(list[0]);
tree.Add(list[2]);
tree.Add(list[5]);
tree.Add(list[4]);
tree.Add(list[6]);
Assert.AreEqual(4, tree.Root.Value);
Assert.AreEqual(2, tree.Root.Left.Value);
Assert.AreEqual(1, tree.Root.Left.Left.Value);
Assert.AreEqual(3, tree.Root.Left.Right.Value);
Assert.AreEqual(null, tree.Root.Left.Left.Left);
Assert.AreEqual(null, tree.Root.Left.Left.Right);
Assert.AreEqual(null, tree.Root.Left.Right.Left);
Assert.AreEqual(null, tree.Root.Left.Right.Right);
Assert.AreEqual(6, tree.Root.Right.Value);
Assert.AreEqual(5, tree.Root.Right.Left.Value);
Assert.AreEqual(7, tree.Root.Right.Right.Value);
Assert.AreEqual(null, tree.Root.Right.Left.Left);
Assert.AreEqual(null, tree.Root.Right.Left.Right);
Assert.AreEqual(null, tree.Root.Right.Right.Left);
Assert.AreEqual(null, tree.Root.Right.Right.Right);
}
示例6: BuildBinaryTree_PrintIndentedPreOrder_ShouldWorkCorrectly
public void BuildBinaryTree_PrintIndentedPreOrder_ShouldWorkCorrectly()
{
// Arrange
var binaryTree =
new BinaryTree<string>("*",
new BinaryTree<string>("-",
new BinaryTree<string>("+",
new BinaryTree<string>("3"),
new BinaryTree<string>("2")),
new BinaryTree<string>("*",
new BinaryTree<string>("9"),
new BinaryTree<string>("6"))),
new BinaryTree<string>("8"));
// Act
var outputStream = new MemoryStream();
using (var outputWriter = new StreamWriter(outputStream))
{
Console.SetOut(outputWriter);
binaryTree.PrintIndentedPreOrder();
}
var output = Encoding.UTF8.GetString(outputStream.ToArray());
// Assert
var expectedOutput = "*\n -\n +\n 3\n 2\n *\n 9\n 6\n 8\n";
output = output.Replace("\r\n", "\n");
Assert.AreEqual(expectedOutput, output);
}
示例7: recursiveTreeBuilder
/// <summary>
/// Static method for building a binary tree with Employees and returning it
/// </summary>
/// <param name="employee">Current employee(at the start the root)</param>
/// <param name="employees">Dictionary with employees</param>
/// <returns>Binary tree</returns>
public static BinaryTree<Employee> recursiveTreeBuilder(Employee employee, Dictionary<Employee, List<Employee>> employees)
{
BinaryTree<Employee> tree;
//When the node is found the recursion goes deeper
foreach (var pair in employees)
{
//searching for perticular node, so it can build the tree correctly
if (pair.Key.FirstName == employee.FirstName)
{
//taking the left and right child of this node
if (pair.Value.Count == 2)
{
var employeeLeftSubordinate = pair.Value[0];
var employeeRightSubordinate = pair.Value[1];
//building the tree recursively by building first the left subtree and then the right
tree = new BinaryTree<Employee>(employee, recursiveTreeBuilder(employeeLeftSubordinate, employees),
recursiveTreeBuilder(employeeRightSubordinate, employees));
return tree;
}
else
{
var employeeLeftSubordinate = pair.Value[0];
//building the tree recursively by building first the left subtree and then the right
tree = new BinaryTree<Employee>(employee, recursiveTreeBuilder(employeeLeftSubordinate, employees),
null);
return tree;
}
}
}
//When it reaches a leaf(node with no children) it returns it as tree with only a root and no children
return tree = new BinaryTree<Employee>(employee);
}
示例8: InorderTraversalNonRec
private void InorderTraversalNonRec(BinaryTree root, List<int> result)
{
if (root == null)
{
return;
}
Stack<BinaryTree> stack = new Stack<BinaryTree>();
stack.Push(root);
while (root.Left != null)
{
stack.Push(root.Left);
root = root.Left;
}
while (stack.Count > 0)
{
var tmp = stack.Pop();
result.Add(tmp.Value);
if (tmp.Right != null)
{
stack.Push(tmp.Right);
tmp = tmp.Right;
while (tmp.Left!= null)
{
stack.Push(tmp.Left);
tmp = tmp.Left;
}
}
}
}
示例9: ExceptionNull
public void ExceptionNull()
{
var binaryTree = new BinaryTree<string>("asdasd");
Assert.AreEqual(binaryTree.Data, "asdasd");
binaryTree.Data = null;
}
示例10: TestMethod_remove
public void TestMethod_remove()
{
BinaryTree<int> bt = new BinaryTree<int>();
bt.Add(10);
Assert.AreEqual(true, bt.Remove(10));
}
示例11: TreesAndGraphsTests
public TreesAndGraphsTests()
{
var _J = new Node<char>('J');
var _I = new Node<char>(_J, null, 'I');
var _H = new Node<char>(null, _I, 'H');
var _D = new Node<char>(_H, null, 'D');
var _M = new Node<char>(null, null, 'M');
var _L = new Node<char>(null, _M, 'L');
var _K = new Node<char>(_L, null, 'K');
var _E = new Node<char>(_K, null, 'E');
var _B = new Node<char>(_D, _E, 'B');
var _F = new Node<char>(null, null, 'F');
var _S = new Node<char>(null, null, 'S');
var _R = new Node<char>(_S, null, 'R');
var _N = new Node<char>(_R, null, 'N');
var _Q = new Node<char>(null, null, 'Q');
var _P = new Node<char>(null, _Q, 'P');
var _O = new Node<char>(null, _P, 'O');
var _G = new Node<char>(_N, _O, 'G');
var _C = new Node<char>(_F, _G, 'C');
var _A = new Node<char>(_B, _C, 'A');
treeLetters = new BinaryTree<char>(_A);
treeLetter["tree"] = treeLetters;
treeLetter["bf_traversal_char_array"] =
"ABCDEFGHKNOILRPJMSQ".ToCharArray();
}
示例12: Simple
public void Simple()
{
var binaryTree = new BinaryTree<int>(5);
Assert.AreEqual(binaryTree.Count, 0);
Assert.AreEqual(binaryTree.Degree, 0);
Assert.AreEqual(binaryTree.Data, 5);
binaryTree = new BinaryTree<int>(5, new BinaryTree<int>(3), new BinaryTree<int>(4));
Assert.AreEqual(binaryTree.Count, 2);
Assert.AreEqual(binaryTree.Degree, 2);
Assert.AreEqual(binaryTree.Data, 5);
Assert.AreEqual(binaryTree.Left.Data, 3);
Assert.AreEqual(binaryTree.Right.Data, 4);
binaryTree = new BinaryTree<int>(5, 3, 4);
Assert.AreEqual(binaryTree.Count, 2);
Assert.AreEqual(binaryTree.Degree, 2);
Assert.AreEqual(binaryTree.Data, 5);
Assert.AreEqual(binaryTree.Left.Data, 3);
Assert.AreEqual(binaryTree.Right.Data, 4);
}
示例13: FindLowestCommonAncestor
// http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html
// this solution doesn't work if node1 or node2 doesn't exist in the tree.
public BinaryTree FindLowestCommonAncestor(BinaryTree tree, int node1, int node2)
{
var result = LCA(tree, node1, node2);
if (result != null)
result.Left = result.Right = null;
return result;
}
示例14: GenerateTree
public List<BinaryTree> GenerateTree(int[] input, int start, int end)
{
if (start > end)
{
return new List<BinaryTree> { null };
}
if (start == end)
{
BinaryTree tree = new BinaryTree(input[start]);
return new List<BinaryTree> { tree };
}
List<BinaryTree> results = new List<BinaryTree>();
for (int i = start; i <= end; i++)
{
List<BinaryTree> left = GenerateTree(input, start, i - 1);
List<BinaryTree> right = GenerateTree(input, i + 1, end);
foreach (var leftTree in left)
{
foreach (var rightTree in right)
{
BinaryTree current = new BinaryTree(input[i]);
current.Left = leftTree;
current.Right = rightTree;
results.Add(current);
}
}
}
return results;
}
示例15: Point2D_InOrderTraversal_CustomComparerTest
public void Point2D_InOrderTraversal_CustomComparerTest()
{
BinaryTree<Point2D> bt = new BinaryTree<Point2D>();
Point2D[] elemToAdd =
{
new Point2D{X = 1, Y = 2},
new Point2D{X = 10, Y = 2},
new Point2D{X = 2, Y = 1},
new Point2D{X = 4, Y = 4}
};
bt.Comparer = new CustomComparer<Point2D>((p1,p2) => (p1.X + p1.Y) - (p2.X+p2.Y));
foreach (var item in elemToAdd)
bt.Add(item);
List<Point2D> actual = new List<Point2D>();
foreach (var item in bt.Inorder())
{
actual.Add(item);
}
Point2D[] expected =
{
new Point2D{X = 2, Y = 1},
new Point2D{X = 1, Y = 2},
new Point2D{X = 4, Y = 4},
new Point2D{X = 10, Y = 2}
};
CollectionAssert.AreEqual(expected, actual);
}