Question

In: Computer Science

a java code In this lab you will implement an inorder traversal, and a search for...

a java code

In this lab you will implement an inorder traversal, and a search for a 2-3-4 tree. The inorder traversal will print the contents (integers) of the tree, and the search method will return a boolean, indicating whether a given key was found in the tree.

Examine the provided template. The main method is provided for you, as well as a template of the Node and 2-3-4 classes. The main method will read either "inorder" or an integer from the user, build a tree, and then call either the inorder traversal method or the search method on the tree. Your task is to write the inorder traversal and search methods. In both cases, you should have a driver in the 2-3-4 Tree class, and a recursive helper in the Node class.

For example, if the tree was

        [12   16     23]
       /    /    \      \
     /    /        \      \
[4  7]  [13]   [18  21]  [25  29  31]

the input

inorder

would output

4 7 12 13 16 18 21 23 25 29 31

the input

18

would produce the output

The key of 18 was FOUND

and the input

5

would produce the output

The key of 5 was NOT found

Note 1: Similar to Lab 8, rather than using an insert method (which we will not cover in code for multiway trees), the tree is constructed directly from Nodes. This is not proper programming practice, and this approach should not be used in other assignments.

Note 2: Be careful of how you test the number of items in a Node. An interior node could contain one, two, or three items, and has one more child than number of items. A leaf node could contain one, two, or three items, but does not have any children.

used this code

//==============================================================
// Lab10.java
//
// COMP 2140 SUMMER 2020
//
// PURPOSE: Print contents of a 2-3-4 tree, using an inorder traversal,
// and search for values.
//
// NOTE: We will not cover insertion or deletion code for 2-3-4 trees, although you have
// all of the knowledge to write insertion. This template builds a
// tree directly from Nodes, and the instance members in the Node class are public.
// This is not proper programming practice, but will make for a simpler lab. In a
// 2-3-4 tree, insertions should always be done in the leaves.
//==============================================================
import java.util.*;
import java.io.*;

public class Lab10 {

public static void main( String[] args ) {
       Scanner keyboard;
       String input;

       keyboard = new Scanner( System.in );

       //read "inorder" or a key
input = keyboard.nextLine();

       keyboard.close();

       //build tree
       Node temp1 = new Node(5, 8, 10);
       Node temp2 = new Node(23, 28);
       Node temp3 = new Node(37, 39, 41);
       Node temp4 = new Node(45, 51, 53);
       Node temp5 = new Node(64, 71);
       Node temp6 = new Node(82, 86, 89);
       Node temp7 = new Node(99);
       Node temp8 = new Node(12, 30, 42, temp1, temp2, temp3, temp4);
       Node temp9 = new Node(80, 95, temp5, temp6, temp7);
       Node temp10 = new Node(60, temp8, temp9);
       My234Tree testTree = new My234Tree(temp10);


       if (input.equals("inorder")){ //traverse tree
           testTree.inorder();
       }
       else { //search tree
           int key = Integer.parseInt(input);
           if (testTree.search(key))
               System.out.println( "The key of " + key + " was FOUND" );
           else
               System.out.println( "The key of " + key + " was NOT found" );
       }

} // end main
}

class Node{
   public int dataLeft;
   public int dataMid;
   public int dataRight;
   public Node left;
   public Node midLeft;
   public Node midRight;
   public Node right;

   public Node(int d){
       dataLeft = d;
       dataMid = dataRight = Integer.MIN_VALUE;
       left = midLeft = midRight = right = null;
   }

   public Node(int d, Node l, Node ml){
       dataLeft = d;
       dataMid = dataRight = Integer.MIN_VALUE;
       left = l;
       midLeft = ml;
       midRight = right = null;
   }

   public Node(int d1, int d2){
       dataLeft = d1;
       dataMid = d2;
       dataRight = Integer.MIN_VALUE;
       left = midLeft = midRight = right = null;
   }

   public Node(int d1, int d2, Node l, Node ml, Node mr){
       dataLeft = d1;
       dataMid = d2;
       dataRight = Integer.MIN_VALUE;
       left = l;
       midLeft = ml;
       midRight = mr;
       right = null;
   }

   public Node(int d1, int d2, int d3){
       dataLeft = d1;
       dataMid = d2;
       dataRight = d3;
       left = midLeft = midRight = right = null;
   }

   public Node(int d1, int d2, int d3, Node l, Node ml, Node mr, Node r){
       dataLeft = d1;
       dataMid = d2;
       dataRight = d3;
       left = l;
       midLeft = ml;
       midRight = mr;
       right = r;
   }

   //NOTE: Leaf nodes could have one, two, or three items, even though all pointers are null
   public void inorder(){
   //****Fill in this recursive helper method****

   }

   public boolean search(int key){
   //****Fill in this recursive helper method****

   }
}//end Node class

class My234Tree{
   public Node root;

   public My234Tree(){
       root = null;
   }

   public My234Tree(Node r){
       root = r;
   }

   public void inorder(){
       //****Fill in this driver method. It should call the recursive helper method on the root.****
      
   }

   public boolean search(int key){
   //****Fill in this driver method. It should call the recursive helper method on the root.****
      
   }
}

Solutions

Expert Solution

////////////////////////////////////////////////////////

class Node{
public int dataLeft;
public int dataMid;
public int dataRight;
public Node left;
public Node midLeft;
public Node midRight;
public Node right;

public Node(int d){
dataLeft = d;
dataMid = dataRight = Integer.MIN_VALUE;
left = midLeft = midRight = right = null;
}

public Node(int d, Node l, Node ml){
dataLeft = d;
dataMid = dataRight = Integer.MIN_VALUE;
left = l;
midLeft = ml;
midRight = right = null;
}

public Node(int d1, int d2){
dataLeft = d1;
dataMid = d2;
dataRight = Integer.MIN_VALUE;
left = midLeft = midRight = right = null;
}

public Node(int d1, int d2, Node l, Node ml, Node mr){
dataLeft = d1;
dataMid = d2;
dataRight = Integer.MIN_VALUE;
left = l;
midLeft = ml;
midRight = mr;
right = null;
}

public Node(int d1, int d2, int d3){
dataLeft = d1;
dataMid = d2;
dataRight = d3;
left = midLeft = midRight = right = null;
}

public Node(int d1, int d2, int d3, Node l, Node ml, Node mr, Node r){
dataLeft = d1;
dataMid = d2;
dataRight = d3;
left = l;
midLeft = ml;
midRight = mr;
right = r;
}

//NOTE: Leaf nodes could have one, two, or three items, even though all pointers are null
public void inorder(){
//****Fill in this recursive helper method****
   inorderHelper(this.left);
   if(this.dataLeft!=Integer.MIN_VALUE){
       System.out.print(this.dataLeft+" ");
   }
   inorderHelper(this.midLeft);
   if(this.dataMid!=Integer.MIN_VALUE){
       System.out.print(this.dataMid+" ");
   }
   inorderHelper(this.midRight);
   if(this.dataRight!=Integer.MIN_VALUE){
       System.out.print(this.dataRight+" ");
   }
   inorderHelper(this.right);
}

public void inorderHelper(Node n){
   if(n==null){
       return ;
   }
   else{
       // recursive call 1
       inorderHelper(n.left);
         
       // display left element
       if(n.dataLeft!=Integer.MIN_VALUE){
           System.out.print(n.dataLeft+" ");
       }
         
       // recursive call 2
       inorderHelper(n.midLeft);
         
       // display middle element
       if(n.dataMid!=Integer.MIN_VALUE){
           System.out.print(n.dataMid+" ");
       }
         
       // recursive call 3
       inorderHelper(n.midRight);
         
        // display right element
       if(n.dataRight!=Integer.MIN_VALUE){
           System.out.print(n.dataRight+" ");
       }
         
       // recursive call 4
       inorderHelper(n.right);
   }
}
public boolean search(int key){
   boolean ret=false;
   if(this.dataLeft==key){
       ret=true;
   }
   // if middle data found
   if(this.dataMid==key){
       ret=true;
   }
   // if right data found
   if(this.dataRight==key){
       ret=true;
   }
   return (ret || searchHelper(this.left,key) || searchHelper(this.midLeft,key) || searchHelper(this.midRight,key) ||searchHelper(this.right,key));
}

public boolean searchHelper(Node n,int key){
   // if node is null
   if(n==null){
       return false ;
   }
   else{
       boolean ret=false;
       // if left data found
       if(n.dataLeft==key){
           ret=true;
       }
       // if middle data found
       if(n.dataMid==key){
           ret=true;
       }
       // if right data found
       if(n.dataRight==key){
           ret=true;
       }
       return(ret || searchHelper(n.left,key) || searchHelper(n.midLeft,key) || searchHelper(n.midRight,key) || searchHelper(n.right,key));
   }
}

}//end Node class

//////////////////////////////////////////////////


class My234Tree{
public Node root;

public My234Tree(){
root = null;
}

public My234Tree(Node r){
root = r;
}

public void inorder(){
this.root.inorder();
}

public boolean search(int key){
return this.root.search(key);
}
}

//////////////////////////////////////////////////////////

//==============================================================
// Lab10.java
//
// COMP 2140 SUMMER 2020
//
// PURPOSE: Print contents of a 2-3-4 tree, using an inorder traversal,
// and search for values.
//
// NOTE: We will not cover insertion or deletion code for 2-3-4 trees, although you have
// all of the knowledge to write insertion. This template builds a
// tree directly from Nodes, and the instance members in the Node class are public.
// This is not proper programming practice, but will make for a simpler lab. In a
// 2-3-4 tree, insertions should always be done in the leaves.
//==============================================================
import java.util.*;
import java.io.*;

public class Lab10 {

public static void main( String[] args ) {
Scanner keyboard;
String input;

keyboard = new Scanner( System.in );

//read "inorder" or a key
input = keyboard.nextLine();

keyboard.close();

//build tree
Node temp1 = new Node(5, 8, 10);
Node temp2 = new Node(23, 28);
Node temp3 = new Node(37, 39, 41);
Node temp4 = new Node(45, 51, 53);
Node temp5 = new Node(64, 71);
Node temp6 = new Node(82, 86, 89);
Node temp7 = new Node(99);
Node temp8 = new Node(12, 30, 42, temp1, temp2, temp3, temp4);
Node temp9 = new Node(80, 95, temp5, temp6, temp7);
Node temp10 = new Node(60, temp8, temp9);
My234Tree testTree = new My234Tree(temp10);


if (input.equals("inorder")){ //traverse tree
testTree.inorder();
}
else { //search tree
int key = Integer.parseInt(input);
if (testTree.search(key))
System.out.println( "The key of " + key + " was FOUND" );
else
System.out.println( "The key of " + key + " was NOT found" );
}

} // end main
}

//////////////////////////////////////////////////////////////


Related Solutions

Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You....
Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You. #include<bits/stdc++.h> using namespace std; struct Node{     int data;     Node* left;     Node* right; }; Node* createNode(int value){     Node* newNode = new Node();     newNode->data = value;     newNode->left = NULL;     newNode->right = NULL;     return newNode; } Node* insert(Node *currentNode, int value){     if(currentNode==NULL){         currentNode = createNode(value);     }else if(value <= currentNode->data){         currentNode->left = insert(currentNode->left,value);     }else{        ...
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is...
Language: Java Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value...
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in...
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in ALGOL W programming only. Thumbs down for wrong answers Make a program to perform Heap Sort, must run in Alice programming only. Only correct answers needed should be in given language
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces....
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces. Please make sure you have a partner for this lab. No code is provided with this lab. Write code to experimentally resolve the following questions about Java. You will need to show your code to the TA or lab assistant. Part I: Assignments and Casting (1 point) ------------------------------------------ Let Y be a subclass of X, and let y and x be variables of classes...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates...
JAVA Code Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive...
JAVA Code Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder,...
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder, level order), done nonrecursively 2)Ability to provide the result of traversing a tree in all traversals (inorder, preorder, postorder, level order)
For this question, you need to implement Java code for the following Modified Ciphertext encryption and...
For this question, you need to implement Java code for the following Modified Ciphertext encryption and attack for known patterns to find the code. Please read the modified Ciphertext carefully and examples before implementing them.   Modified Ciphertext is working as follows: If a plain text (only letters ignore uppercase or lowercase) and sequences of numbers are given, it encrypts the given plain text by shifting each letter in a message the characters based on the given numbers one forward direction...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT