- 1
In-order, Pre-order and Post-order binary tree traversal with only number of nodes?
Is there a way to print out numbers for pre-order, post-order and in-order traversals of trees using just recursion and a number. The trees are binary and n is the number of nodes from the parent to al children. Can anyone help me write the function to print the numbers in their orderly fashion in any programming language using n only and recursion?
5 Answers
0
Just remove the templates in favor of a concrete integral type like int, replace the output via std::cout by calls to Console.WriteLine(), and make the functions normal static mathods of a class.
- 1
Yeah let me send you a screenshot of the problem via imgur, part of the recursive code has been provided as below
public class Tree
{
//the method below expands the tree to view the
//order in which its been traversed
public void Expand(int n)
{
if (n > 8)
{
return;
}
}
- 1
A link to to the assignment and the requirements is here, please help
https://i.imgur.com/s6EwerY.png
- 1
If I understand the exercise correctly, you would have to come up with formulas for how to calculate the label of the left and right child during each method of traversal, given the label of the current node and the depth.
For example, during pre-order traversal, the label of the left child would be the label of the current node + 1.
Drawing some trees on paper with assigned labels for each algorithm might be helpful to figure out connections, as well as the following formula that holds true in perfect binary trees:
size = 2^(depth + 1) - 1
Sample solution that prints the labels in pre-linearized form:
https://code.sololearn.com/c79Te6k054hY/?ref=app
- 1
Hey can you write the answer in C# please, I saw the link attached has a cpp solution. Can you edit for C#?