Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Data Structures and Algorithms - Trees Please answer at least 5 of the ques

ID: 3559174 • Letter: J

Question

Java Data Structures and Algorithms - Trees

Please answer at least 5 of the questions for points, response with most answers will get all points.

Write a function that receives a tree of strings in sequential representation and prints the string of each leaf Write a function that receives a tree of strings in linked representation and prints the string of each leaf. Rewrite the functions of #1 and #2 such the strings of the leaves arc returned as a list instead of printing them. Write a function that receives a tree of strings in sequential representation and returns, the strings as a list of their inorder listing. Write a function that receives a tree of strings in sequential representation and returns, the strings as a list of their preorder listing. Write a function that receives a tree of strings in sequential representation and returns the strings as a list of their preorder listing. Write a function that receives a tree of strings in sequential representation and returns the strings as a list of their level-by-level listing. Red #4,5, and 6 assuming the tree is received in linked representation. Write a function that receives two arrays of integers and returns the value True if the two trees are isomorphic, otherwise it returns False. Two trees are isomorphic iff they have exactly the same shape and values.

Explanation / Answer

THE FOLLOWING CODE IS SOLUTION FOR YOUR 4 5 6 QUESTIONS (INORDER ,PREORDER ,POSTORDER)

/**
   * Represents a node in the Binary Search Tree.
   */
   public class Node<T> {
   //The value present in the node.
   public int value;
     
   //The reference to the left subtree.
   public Node left;

//The reference to the right subtree.
public Node right;

   public Node(int value) {
   this.value = value;
}
     
   }
     
   /**
   * Represents the Binary Search Tree.
   */
   public class BinarySearchTree {
     
   //Refrence for the root of the tree.
   public Node root;
     
   public BinarySearchTree insert(int value) {
   Node node = new Node<>(value);
     
   if (root == null) {
   root = node;
   return this;
   }
     
   insertRec(root, node);
   return this;
   }
     
   private void insertRec(Node latestRoot, Node node) {

   if (latestRoot.value > node.value) {
     
   if (latestRoot.left == null) {
   latestRoot.left = node;
   return;
   } else {
   insertRec(latestRoot.left, node);
   }
   } else {
   if (latestRoot.right == null) {
   latestRoot.right = node;
   return;
   } else {
   insertRec(latestRoot.right, node);
   }
   }
   }
     
   /**
   * Returns the minimum value in the Binary Search Tree.
   */
   public int findMinimum() {
   if (root == null) {
   return 0;
   }
   Node currNode = root;
   while (currNode.left != null) {
   currNode = currNode.left;
   }
   return currNode.value;
   }
     
   /**
   * Returns the maximum value in the Binary Search Tree
   */
   public int findMaximum() {
   if (root == null) {
   return 0;
   }
     
   Node currNode = root;
   while (currNode.right != null) {
   currNode = currNode.right;
   }
   return currNode.value;
   }
     
   /**
   * Printing the contents of the tree in an inorder way.
   */
   public void printInorder() {
   printInOrderRec(root);
   System.out.println("");
   }
     
   /**
   * Helper method to recursively print the contents in an inorder way
   */
   private void printInOrderRec(Node currRoot) {
   if (currRoot == null) {
   return;
   }
   printInOrderRec(currRoot.left);
   System.out.print(currRoot.value + ", ");
   printInOrderRec(currRoot.right);
   }
     
   /**
   * Printing the contents of the tree in a Preorder way.
   */
   public void printPreorder() {
   printPreOrderRec(root);
   System.out.println("");
   }
     
   /**
   * Helper method to recursively print the contents in a Preorder way
*/
   private void printPreOrderRec(Node currRoot) {
   if (currRoot == null) {
   return;
   }
   System.out.print(currRoot.value + ", ");
   printPreOrderRec(currRoot.left);
   printPreOrderRec(currRoot.right);
   }
     
   /**
   * Printing the contents of the tree in a Postorder way.
   */
   public void printPostorder() {
   printPostOrderRec(root);
   System.out.println("");
   }
     
   /**
   * Helper method to recursively print the contents in a Postorder way
   */
   private void printPostOrderRec(Node currRoot) {
   if (currRoot == null) {
   return;
   }
   printPostOrderRec(currRoot.left);
   printPostOrderRec(currRoot.right);
   System.out.print(currRoot.value + ", ");
     
   }
   }
     
   public class BinarySearchTreeDemo {
     
   public static void main(String[] args) {
   BinarySearchTree bst = new BinarySearchTree();
   bst .insert(40)
   .insert(25)
   .insert(78)
   .insert(10)
   .insert(3)
   .insert(17)
   .insert(32)
   .insert(30)
   .insert(38)
   .insert(78)
   .insert(50)
   .insert(93);
   System.out.println("Inorder traversal");
   bst.printInorder();
     
   System.out.println("Preorder Traversal");
   bst.printPreorder();
     
   System.out.println("Postorder Traversal");
   bst.printPostorder();
     
  
     
   }
   }

Q7 .) LEVEL ORDER TRAVERSAL

Function for level order traversal :

ALGORITHM:

1. Initialize a queue.
2. Insert the root into the queue.
3. Repeat steps 4 to 7 until the queue is empty.
4. Delete the node from the queue.
5. Process the node being deleted.
6. If the left child of node exists (is not null), insert it into the queue.
7. If the right child of node exists (is not null), insert it into the queue

Level order traversal
class Node
{
Object data;
Node left;
Node right;
Node( Object d ) // constructor
{ data = d; }
}
class BinaryTree
{ Object tree[];
int maxSize;
java.util.LinkedList<Node> que =
new java.util.LinkedList<Node>();
BinaryTree( Object a[], int n ) // constructor
{ maxSize = n;
tree = new Object[maxSize];
for( int i=0; i
tree[i] = a[i];
}
public Node buildTree( int index )
{ Node p = null;
if( tree[index] != null )
{ p = new Node(tree[index]);
p.left = buildTree(2*index+1);
p.right = buildTree(2*index+2);
}
return p;
}

public void levelorder(Node p)
{
que.addLast(p);
while( !que.isEmpty() )
{
p = que.removeFirst();
System.out.print(p.data +

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote