Q4. Consider the following declaration of the \"TreeNode\" class, which also has
ID: 3874065 • Letter: Q
Question
Q4. Consider the following declaration of the "TreeNode" class, which also has the getData(). getleft() getRight() and SetAlINull() public methods: public class TreeNode ( private Integer data; private TreeNode left; private TreeNode right; A binary tree can then be represented by its root only as: TreeNode root; Write recursive implementations for the following methods on the binary tree: a. I1 Input: p, root pointer of a binary tree which may be null // Postcondition: All nodes at the root and below are deleted. public void treeclear (TreeNode p) f b. I Input: P, root pointer of a binary tree which may be null // Postcondition: The data from the nodes of the tree have been // printed using in-order traversal. public void printTree (TreeNode p) fExplanation / Answer
PROGRAM
import java.io.*;
import java.util.*;
class BinaryTree
{
public class TreeNode
{
private int data;
private TreeNode left;
private TreeNode right;
public TreeNode(int x)
{
data=x;
left=right=null;
}
}
class Max_level
{
int max_level;
}
TreeNode root;
Max_level max = new Max_level();
static int level=0;
public BinaryTree()
{
root=null;
}
public int getData()
{
return root.data;
}
public void Left(TreeNode tree,int l)
{
if(tree==null) return;
if(level<l)
{
System.out.print(" "+tree.data);
level=l;
}
Left(tree.left,l+1);
Left(tree.right,l+1);
}
public void getLeft()
{
Left(root,1);
}
public void Right(TreeNode tree,int l,Max_level max_level)
{
if(tree==null) return;
if(max_level.max_level<l)
{
System.out.print(" "+tree.data);
max_level.max_level=l;
}
Right(tree.right,l+1,max_level);
Right(tree.left,l+1,max_level);
}
public void getRight()
{
Right(root,1,max);
}
public void treeClear(TreeNode p)
{
if(p==null) return;
treeClear(p.left);
treeClear(p.right);
System.out.println(" Clear the Tree: "+p.data);
p=null;
}
public void setAllNull()
{
root.data=0;
root.left=root.right=null;
}
TreeNode insertRec(TreeNode root, int a)
{
if (root == null)
{
root = new TreeNode(a);
return root;
}
if (a < root.data)
root.left = insertRec(root.left, a);
else
if (a > root.data)
root.right = insertRec(root.right, a);
return root;
}
void insert(int x)
{
root = insertRec(root, x);
}
public void printTree(TreeNode p)
{
if(p!=null)
{
printTree(p.left);
System.out.print(" "+p.data);
printTree(p.right);
}
}
public static void main(String args[])
{
BinaryTree tree=new BinaryTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
System.out.println("Actual Tree ");
tree.printTree(tree.root);
System.out.print(" Left Tree Elements: ");
tree.getLeft();
System.out.print(" Right Tree Elements: ");
tree.getRight();
int t=tree.getData();
tree.treeClear(tree.root);
System.out.println("Getting Data: "+t);
tree.root=null;
System.out.println("Tree is Clear");
tree.printTree(tree.root);
}
}
OUTPUT:
C:>javac BinaryTree.java
C:>java BinaryTree
Actual Tree
20 30 40 50 60 70 80
Left Tree Elements: 50 30 20
Right Tree Elements: 50 70 80
Clear the Tree: 20
Clear the Tree: 40
Clear the Tree: 30
Clear the Tree: 60
Clear the Tree: 80
Clear the Tree: 70
Clear the Tree: 50
Getting Data: 50
Tree is Clear
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.