(a) Design and implement a Java program that performs an inorder traversal of a
ID: 3605840 • Letter: #
Question
(a) Design and implement a Java program that performs an inorder traversal of a binary tree without using recursion. The program must have time complexity O(n), where n is the number of nodes in the given binary tree. The program must have time complexity O(h), where h is the height of the tree. Input. The input is a textual representation of a binary tree in the same format as in the problem above. Sample input: 4 A5BD B 3 C NONE C 7 NONE NONE D 1 NONE NONE Output. The output consists of a single line of space-separated tokens. Each token is the value of a corresponding node in the binary tree. The tokens appear in the order in which the corresponding nodes are visited during an inorder traversal of the input binary tree. Sample output (for the above input): 7351 (b) (c) (d) • Note that the pseudo code should be meaningful in the sense that each step in the pseudo code should contribute to the solution. The pseudo code should not include steps that are there just to meet the time complexity requirements without contributing to the solution. State what the time complexity of your program is and explain why. State what the space complexity of your program is and explain why. Provide the pseudo code for an algorithm that performs an inorder traversal of a binary tree without using recursion, such that the algorithm’s time and space complexity are O(n). Explain why the algorithm you provide has this time and space complexity. (a) Design and implement a Java program that performs an inorder traversal of a binary tree without using recursion. The program must have time complexity O(n), where n is the number of nodes in the given binary tree. The program must have time complexity O(h), where h is the height of the tree. Input. The input is a textual representation of a binary tree in the same format as in the problem above. Sample input: 4 A5BD B 3 C NONE C 7 NONE NONE D 1 NONE NONE Output. The output consists of a single line of space-separated tokens. Each token is the value of a corresponding node in the binary tree. The tokens appear in the order in which the corresponding nodes are visited during an inorder traversal of the input binary tree. Sample output (for the above input): 7351 (b) (c) (d) • Note that the pseudo code should be meaningful in the sense that each step in the pseudo code should contribute to the solution. The pseudo code should not include steps that are there just to meet the time complexity requirements without contributing to the solution. State what the time complexity of your program is and explain why. State what the space complexity of your program is and explain why. Provide the pseudo code for an algorithm that performs an inorder traversal of a binary tree without using recursion, such that the algorithm’s time and space complexity are O(n). Explain why the algorithm you provide has this time and space complexity. (a) Design and implement a Java program that performs an inorder traversal of a binary tree without using recursion. The program must have time complexity O(n), where n is the number of nodes in the given binary tree. The program must have time complexity O(h), where h is the height of the tree. Input. The input is a textual representation of a binary tree in the same format as in the problem above. Sample input: 4 A5BD B 3 C NONE C 7 NONE NONE D 1 NONE NONE Output. The output consists of a single line of space-separated tokens. Each token is the value of a corresponding node in the binary tree. The tokens appear in the order in which the corresponding nodes are visited during an inorder traversal of the input binary tree. Sample output (for the above input): 7351 (b) (c) (d) • Note that the pseudo code should be meaningful in the sense that each step in the pseudo code should contribute to the solution. The pseudo code should not include steps that are there just to meet the time complexity requirements without contributing to the solution. State what the time complexity of your program is and explain why. State what the space complexity of your program is and explain why. Provide the pseudo code for an algorithm that performs an inorder traversal of a binary tree without using recursion, such that the algorithm’s time and space complexity are O(n). Explain why the algorithm you provide has this time and space complexity.Explanation / Answer
class Node
{
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
class BinaryTree
{
Node root;
int maxDepth(Node node)
{
if (node == null)
return 0;
else
{
int lDepth = maxDepth(node.left);
int rDepth = maxDepth(node.right);
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
System.out.println("Height of tree is : " +
tree.maxDepth(tree.root));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.