Write a public method “printRootToLeafPath()” that prints out ALL its root-to-leaf paths. Note: you should use internal recursive private method to print all paths
java code
Node.Java
class Node
{
int num;
Node leftNode,rightNode;
Node(int element)
{
num=element;
leftNode=rightNode=null;
}
}
Main.java
class Main
{
Node rootNode;
public static void printRootToLeafPath(Node node)
{
int p[]=new int[100];
printRootToLeafPathRecursive(node,p,0);
}
public static void printRootToLeafPathRecursive(Node n, int p[], int len){
if(n== null)
return;
p[len]=n.num;
len++;
if(n.leftNode==null && n.rightNode==null)
displayArr(p,len);
else
{
printRootToLeafPathRecursive(n.leftNode,p,len);
printRootToLeafPathRecursive(n.rightNode,p,len);
}
}
public static void displayArr(int nums[],int len)
{
for (int i=0;i<len;i++)
{
System.out.print(nums[i]+" ");
}
System.out.println("");
}
public static void main(String args[])
{
Main BTree=new Main();
BTree.rootNode=new Node(15);
BTree.rootNode.leftNode=new Node(9);
BTree.rootNode.rightNode=new Node(1);
BTree.rootNode.leftNode.leftNode=new Node(4);
BTree.rootNode.leftNode.rightNode=new Node(6);
BTree.rootNode.rightNode.leftNode=new Node(3);
BTree.printRootToLeafPath(BTree.rootNode);
}
}
if you like the answer please provide a thumbs up.
Write a public method “printRootToLeafPath()” that prints out ALL its root-to-leaf paths. Note: you should use...
Write a function that given the root of a BST, prints out all its elements in the sorted order. (JAVA)
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super E>> { private Node<E> root; private int size; public FinalTree() { root = null; size = 0; } public boolean isEmpty() { return size == 0; } public void add(E newItem) { if (isEmpty()) { root = new Node<E>(newItem); } else { Node<E> parent = null; Node<E> temp = root; int result = 0; while (temp != null) { parent = temp; result =...
Write a JAVA recursive method to calculate an exponent. Your method should have the following header. Note: you can write a second, helper method if you choose. (Example: if base = 2 and power = 4, your method should return 2 ^ 4 = 16.) public int calculateExponent(int base, int power)
In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.
Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...
Write a program which: 1. Prints out the Multiplication Table for a range of numbers (positive integers). • Prompts the user for a starting number: say 'x' • Prompts the user for an ending number: say 'y' • Prints out the multiplication table of 'x' up to the number 'y' SPECIFIC REQUIREMENTS 1. You must use the following method to load the array: public static void loadArray(int table[][], int x, int y) 2. You must use the following method to...
All JAVA code 6.2.2: Define a method printFeetInchShort, with int parameters numFeet and numInches, that prints using ' and " shorthand. Ex: printFeetInchShort(5, 8) prints: 5' 8" Hint: Use \" to print a double quote Sample program: import java.util.Scanner; public class HeightPrinter { /* Your solution goes here */ public static void main (String [] args) { printFeetInchShort(5, 8); System.out.println(""); return; } } 6.4.1: Define stubs for the methods called by the below main(). Each stub should print "FIXME: Finish...
Write a method printCostTable() that prints out a table like the following: price carpet fitting 5.0 150.0 120.0 9.0 270.0 120.0 13.0 390.0 120.0 The method should take four int arguments representing the width and length of the carpet, a start price and an end price. The method should first print the headings price carpet fitting It should then print the price, carpet cost and fitting cost for each price starting from startPrice and increasing in increments of £4, up...
Write a JAVA program that has a method which accepts a string and prints it back to the user with all vowels replaced with *'s (multiple asterisks not '*s') with a new line after the string. The method signature should look like this: public static void replacePrint(String input)