本文整理汇总了C#中BinaryTree类的典型用法代码示例。如果您正苦于以下问题:C# BinaryTree类的具体用法?C# BinaryTree怎么用?C# BinaryTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BinaryTree类属于命名空间,在下文中一共展示了BinaryTree类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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));
}
开发者ID:alex687,项目名称:SoftUni-Homeworks,代码行数:60,代码来源:Program.cs
示例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);
}
开发者ID:j-u-l-i-o-u-n-a,项目名称:codigo,代码行数:27,代码来源:BinaryTreeTests.cs
示例3: Connect
public TreeNodeWithNext Connect(BinaryTree input)
{
TreeNodeWithNext root = input.ConvertToTreeNodeWithNext();
Connect(root);
return root;
}
开发者ID:bluesteyes,项目名称:LeetSharp,代码行数:7,代码来源:Q117_PopulatingNextRightPointersinEachNodeII.cs
示例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;
}
开发者ID:changLiuUNSW,项目名称:BDSystem,代码行数:29,代码来源:QualificationCreator.cs
示例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);
}
开发者ID:korz,项目名称:MathIsEverywhere,代码行数:32,代码来源:Add.cs
示例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);
}
开发者ID:iliankostov,项目名称:DataStructures,代码行数:28,代码来源:UnitTestsBinaryTree.cs
示例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);
}
开发者ID:iramov,项目名称:EmployeeBinaryTree,代码行数:40,代码来源:TreeBuilder.cs
示例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;
}
}
}
}
开发者ID:dullcat,项目名称:leetcode_csharp,代码行数:29,代码来源:Q094_BinaryTreeInorderTraversal.cs
示例9: ExceptionNull
public void ExceptionNull()
{
var binaryTree = new BinaryTree<string>("asdasd");
Assert.AreEqual(binaryTree.Data, "asdasd");
binaryTree.Data = null;
}
开发者ID:GTuritto,项目名称:ngenerics,代码行数:7,代码来源:Data.cs
示例10: TestMethod_remove
public void TestMethod_remove()
{
BinaryTree<int> bt = new BinaryTree<int>();
bt.Add(10);
Assert.AreEqual(true, bt.Remove(10));
}
开发者ID:DigilevichAlexandr,项目名称:Kontrolnaya,代码行数:7,代码来源:UnitTest1.cs
示例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();
}
开发者ID:lopezpdvn,项目名称:SoftwareEngineeringProblemsCSharp,代码行数:26,代码来源:TreesAndGraphs.cs
示例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);
}
开发者ID:GTuritto,项目名称:ngenerics,代码行数:26,代码来源:Contruction.cs
示例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;
}
开发者ID:bluesteyes,项目名称:LeetSharp,代码行数:9,代码来源:Q133_LowestCommonAncestor.cs
示例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;
}
开发者ID:bluesteyes,项目名称:LeetSharp,代码行数:32,代码来源:Q095_UniqueBinarySearchTreesII.cs
示例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);
}
开发者ID:VladislavMedved,项目名称:Day9,代码行数:27,代码来源:BinaryTreeTest.cs
示例16: MaxDepth
public int MaxDepth(BinaryTree root)
{
if (root == null)
return 0;
return Math.Max(MaxDepth(root.Left), MaxDepth(root.Right)) + 1;
}
开发者ID:bluesteyes,项目名称:LeetSharp,代码行数:7,代码来源:Q104_MaximumDepthofBinaryTree.cs
示例17: Main
public static void Main()
{
// JFK
BinaryTree<string> jfkFamilyTree = new BinaryTree<string>(
"John Fitzgerald Kennedy");
jfkFamilyTree.SubItems = new Pair<BinaryTree<string>>(
new BinaryTree<string>("Joseph Patrick Kennedy"),
new BinaryTree<string>("Rose Elizabeth Fitzgerald"));
// Grandparents (Father's side)
jfkFamilyTree.SubItems.First.SubItems =
new Pair<BinaryTree<string>>(
new BinaryTree<string>("Patrick Joseph Kennedy"),
new BinaryTree<string>("Mary Augusta Hickey"));
// Grandparents (Mother's side)
jfkFamilyTree.SubItems.Second.SubItems =
new Pair<BinaryTree<string>>(
new BinaryTree<string>("John Francis Fitzgerald"),
new BinaryTree<string>("Mary Josephine Hannon"));
foreach(string name in jfkFamilyTree)
{
Console.WriteLine(name);
}
}
开发者ID:andrewdwalker,项目名称:EssentialCSharp,代码行数:27,代码来源:Listing16.17.UsingForeachWithBinaryTree.cs
示例18: TreeWithPointProvider
public void TreeWithPointProvider(CustomPoint[] point, CustomPoint[] resultPoint, IComparer<CustomPoint> comparer)
{
var tree = new BinaryTree<CustomPoint>(point, comparer);
var pointCompare = new PointComparer();
var enumeratorBook = tree.Preorder().ToArray();
CollectionAssert.AreEqual(enumeratorBook, resultPoint, pointCompare);
}
开发者ID:ZheldakArtem,项目名称:EPAM.Summer.Day10-11.Zheldak,代码行数:7,代码来源:BinaryTreeTest.cs
示例19: AddElements
public static void AddElements(BinaryTree<int> binaryTree, int count)
{
for (int i = 1; i <= count; i++)
{
binaryTree.Add(i);
}
}
开发者ID:InKolev,项目名称:TelerikAcademy,代码行数:7,代码来源:Startup.cs
示例20: RecoverTree
public BinaryTree RecoverTree(BinaryTree root)
{
BinaryTree n1 = null;
BinaryTree n2 = null;
bool findingN2 = false;
int last = int.MinValue;
foreach (var cur in root.InOrderBinaryTree())
{
if (cur.Value <= last) // desending
{
n2 = cur;
findingN2 = true;
}
else
{
if (!findingN2)
{
n1 = cur;
}
}
last = cur.Value;
}
int tmp = n1.Value;
n1.Value = n2.Value;
n2.Value = tmp;
return root;
}
开发者ID:dullcat,项目名称:leetcode_csharp,代码行数:28,代码来源:Q099_RecoverBinarySearchTree.cs
注:本文中的BinaryTree类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论