Question
Write the following methods into the .java files given below the sample output(Please follow exactly the requirements mentioned below and provide snapshots of your code
Write the following methods into the .java files given below the sample output(Please follow exactly the requirements mentioned below and provide snapshots of your code and output):
a)Write a method public int count() to count the number of nodes in a binary tree.
b)Write a method public boolean isLeaf(BTNode node) to determine if a given binary tree node is a leaf.
c)Write a method public int countLeaves() to count the number of leaves in a binary tree.
d)Write a method int getLevel(T data) to find the level of a node with key data of a binary tree. Assume that the binary tree has distinct keys.
Test program.
Write a test java program that creates the binary tree shown below, traverses it using the breadth-first and depth-first traversals (preorder, inorder, and postorder) and prints the traversal results. It also tests the delete method and the above methods. For example, for the following tree:
////BinaryTree.java////
import java.util.Queue; import java.util.LinkedList; import java.util.NoSuchElementException;
public class BinaryTree
private void levelOrderTraversal(BTNode root){ if(root == null) return; Queue
protected void inorderTraversal(BTNode node) { if (node == null) return; inorderTraversal(node.left); System.out.print(node.data + " "); inorderTraversal(node.right); } public void postorderTraversal(){ postorderTraversal(root); } private void postorderTraversal(BTNode node){ if (node == null) return; postorderTraversal(node.left); postorderTraversal(node.right); System.out.print(node.data + " "); } public void preorderTraversal(){ preorderTraversal(root); } private void preorderTraversal(BTNode node){ if (node == null) return; System.out.print(node.data + " ");
preorderTraversal(node.left); preorderTraversal(node.right); } public boolean search(T key){ if(root == null) return false; Queue
public void printTree(){ printTree(root, "", true); }
// Print the tree private void printTree(BTNode currPtr, String indent, boolean last) { if (currPtr != null) { System.out.print(indent); if (last) { System.out.print("R----"); indent += " "; } else { System.out.print("L----"); indent += "| "; } System.out.println(currPtr.data); printTree(currPtr.left, indent, false); printTree(currPtr.right, indent, true); } }
}
////BTNode.java////
public class BTNode
public BTNode(T data) { this(data,null,null); }
public BTNode(T data, BTNode
////BinaryTreeDriver.java////
public class BinaryTreeDriver{ public static void main(String args[]) { // To be completed } }
2 3 4 5 12 a sample program run is: The number of nodes in the tree is 6 The number of leaf nodes in the tree is 3 The level of node with key 4 is 2 Trying to find the level of node with key 60 ... java.util.NoSuchElementException: Key not in tree. Preorder Traversal is: 124 5 123 Inorder Traversal is: 4 2 12 5 13 Before deleting key 3, level order traversal of binary tree is: 1 2 3 4 5 12 The tree is: R----1 L----2 | L----4 R----5 L----12 R----3 After deleting key 3, level order traversal of binary tree is: 1 2 12 4 5 The tree is: R----1 L----2 L----4 | R----5 R----12
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started