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

In this lab, you will implement three tree-traversal methods. Using the skeleton

ID: 3920489 • Letter: I

Question

In this lab, you will implement three tree-traversal methods. Using the skeleton code provided, you must fill in the indicated methods in order to traverse the tree in an “in-order,” “pre-order,” and “post-order” fashion. A description of each of these can be found at the following wikipedia page: https://en.wikipedia.org/wiki/Tree_traversal

public class Tree{
   Node root;
  
   public static void main(String[] args){
       Tree t = new Tree();
      
       t.add(16);
       t.add(10);
       t.add(19);
       t.add(22);
       t.add(1);
       t.add(14);
       t.add(15);
       t.add(25);
       t.add(18);
      
       t.preOrder();
       t.postOrder();
       t.inOrder();
      
   }
  
   public void add(int n){
       if (root == null) root = new Node(n);
       else add(n, root);
   }
  
   public void add(int n, Node current){
       if(n < current.data){
           if (current.left == null) current.left = new Node(n);
           else add(n, current.left);
       } else if(n > current.data){
           if(current.right == null) current.right = new Node(n);
           else add(n, current.right);
       }
   }
  
  
   public void preOrder(){
       preOrder(root);
       System.out.println();
   }
  
   public void postOrder(){
       postOrder(root);
       System.out.println();
   }
  
   public void inOrder(){
       inOrder(root);
       System.out.println();
   }
  
  
   //Fill in these three methods!
   public void preOrder(Node current){
      
   }
  
   public void postOrder(Node current){
      
   }
  
   public void inOrder(Node current){
      
   }
   //////////////////////////////      


   private class Node{
       int data;
       Node left;
       Node right;
      
       public Node(int n){
           data = n;
       }
   }
}

Explanation / Answer

public class Tree{ Node root; public static void main(String[] args){ Tree t = new Tree(); t.add(16); t.add(10); t.add(19); t.add(22); t.add(1); t.add(14); t.add(15); t.add(25); t.add(18); t.preOrder(); t.postOrder(); t.inOrder(); } public void add(int n){ if (root == null) root = new Node(n); else add(n, root); } public void add(int n, Node current){ if(n current.data){ if(current.right == null) current.right = new Node(n); else add(n, current.right); } } public void preOrder(){ preOrder(root); System.out.println(); } public void postOrder(){ postOrder(root); System.out.println(); } public void inOrder(){ inOrder(root); System.out.println(); } //Fill in these three methods! public void preOrder(Node current){ if(current != null) { System.out.print(current.data + " "); preOrder(current.left); preOrder(current.right); } } public void postOrder(Node current){ if(current != null) { postOrder(current.left); postOrder(current.right); System.out.print(current.data + " "); } } public void inOrder(Node current){ if(current != null) { inOrder(current.left); System.out.print(current.data + " "); inOrder(current.right); } } ////////////////////////////// private class Node{ int data; Node left; Node right; public Node(int n){ data = n; } } }
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