Question: Can anyone provide a flowchart for this code? package com.src; class Node { String name; Node left, right; public Node(String item) { name = item;
Can anyone provide a flowchart for this code?
package com.src;
class Node
{
String name;
Node left, right;
public Node(String item)
{
name = item;
left = right = null;
}
}
class BinaryTree
{
// Root of Binary Tree
Node root;
BinaryTree()
{
root = null;
}
void printLevelOrderFamily()
{
int h = height(root);
int i;
for (i=1; i<=h; i++) {
System.out.print("Generation level: "+ i + " -> ");
printGivenLevel(root, i);
System.out.println();
}
}
/* Compute the "height" of a tree .*/
int height(Node root)
{
if (root == null)
return 0;
else
{
/* compute height of each subtree */
int lheight = height(root.left);
int rheight = height(root.right);
/* use the larger one */
if (lheight > rheight)
return(lheight+1);
else return(rheight+1);
}
}
/* Print nodes at the given level */
void printGivenLevel (Node root ,int level)
{
if (root == null)
return;
if (level == 1)
System.out.print(root.name + " ");
else if (level > 1)
{
printGivenLevel(root.left, level-1);
printGivenLevel(root.right, level-1);
}
}
// Driver method
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node("Myself");
tree.root.left = new Node("father P1");
tree.root.right = new Node("Mother P2");
tree.root.left.left = new Node("Grand Father P1-1");
tree.root.left.right = new Node("Grand Mother P1-2");
tree.root.right.left = new Node("Grand Father P2-1");
tree.root.right.right = new Node("Grand Mother P2-2");
System.out.println("Generation level Family tree is ");
tree.printLevelOrderFamily();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
