Question

Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

Assignment on Java programing
1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console.

b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The tree should store key as integer. You should have a single file with class BST for the program. Your test program (main method) should demonstrate that all operations work correctly. Note that your textbook has the pseudo code and code in C for these operations that you can use but you need to modify a little to meet the assignment instructions and grading criteria.


c) BST2List: Copy your BST implementation from above and call it BST2. Change the Node to have additional data: status of type boolean and change BST2 operations appropriately: insert(int key, boolean status), delete(int key), find(int key), traverse(). Then implement an algorithm as method, findList(int low, int max) in BST2 class which prints out all key values within the range low and max if the status is true. Write test program which creates a BST2 instance and loads with below data, displays the values of the nodes in the tree in-order, displays low and max values, and calls findList to display values in range. Evaluate and report the time and space complexity of your findList algorithm.
For example,

Original list with status values:

45 false, 38 true, 21 false, 40 true, 50 true, 86 true, 90 false
In-order traversal of the BST: 21 38 40 45 50 86 90
low=39
max=51
Find list: 40 50

Grading Rubric
Points Criteria
10 Programs use object oriented program approach, have the appropriate naming convention, author’s name, and brief description of the implementation in the files
and the problem it is solving for main() methods
30 InsertionSort:
Correctly implements raw array for data Correctly implements insertion sort algorithm that was covered in class.Correctly selects test data for the analysis and describes in the report Correctly analyzes the runtime performance of the sort, provides results and conclusions in the report
explains implementation of insertion sort using raw array
explains test data used, analysis, and results of sort performance
shows the running program for given instance problem and explains
output
For extra credit: manually show the array contents after each iteration of the sort and explain what it is doing
30 BST:
Correctly implements Binary Search Tree ADT with insert, delete, find, and in-order traverse where it prints the value of the node.
explains implementation of BST ADT, test program, shows program running, and explains output
30 BST2List:
Correctly adds findList method to modified BST2 ADT
Correctly implements algorithm (implemented as method) with recursion to
print all the nodes with values in the range of low and max inclusive when
status=true
Test program creates a BST2 and loads with data

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Searching a key

// A utility function to search a given key in BST

public Node search(Node root, int key)

{

    // Base Cases: root is null or key is present at root

    if (root==null || root.key==key)

        return root;

    // val is greater than root's key

    if (root.key > key)

        return search(root.left, key);

    // val is less than root's key

    return search(root.right, key);

}

Insertion of a key

// Java program to demonstrate insert operation in binary search tree
class BinarySearchTree {
/* Class containing left and right child of current node and key value*/
class Node {
int key;
Node left, right;

public Node(int item) {
key = item;
left = right = null;
}
}
// Root of BST
Node root;

// Constructor
BinarySearchTree() {
root = null;
}

// This method mainly calls insertRec()
void insert(int key) {
root = insertRec(root, key);
}

/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key) {

/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}

/* Otherwise, recur down the tree */
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);

/* return the (unchanged) node pointer */
return root;
}

// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}

// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}

// Driver Program to test above functions
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();

/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
// print inorder traversal of the BST
tree.inorder();
}
}
// This code is contributed by Ankur Narain Verma

Output :

20
30
40
50
60
70
80

// Java program to correct the BST
// if two nodes are swapped
import java.util.*;
import java.lang.*;
import java.io.*;

class Node {

int data;
Node left, right;

Node(int d) {
data = d;
left = right = null;
}
}

class BinaryTree
{
Node first, middle, last, prev;

// This function does inorder traversal
// to find out the two swapped nodes.
// It sets three pointers, first, middle
// and last. If the swapped nodes are
// adjacent to each other, then first
// and middle contain the resultant nodes
// Else, first and last contain the
// resultant nodes
void correctBSTUtil( Node root)
{
if( root != null )
{
// Recur for the left subtree
correctBSTUtil( root.left);

// If this node is smaller than
// the previous node, it's
// violating the BST rule.
if (prev != null && root.data <
prev.data)
{
// If this is first violation,
// mark these two nodes as
// 'first' and 'middle'
if (first == null)
{
first = prev;
middle = root;
}

// If this is second violation,
// mark this node as last
else
last = root;
}

// Mark this node as previous
prev = root;

// Recur for the right subtree
correctBSTUtil( root.right);
}
}

// A function to fix a given BST where
// two nodes are swapped. This function
// uses correctBSTUtil() to find out
// two nodes and swaps the nodes to
// fix the BST
void correctBST( Node root )
{
// Initialize pointers needed
// for correctBSTUtil()
first = middle = last = prev = null;

// Set the poiters to find out
// two nodes
correctBSTUtil( root );

// Fix (or correct) the tree
if( first != null && last != null )
{
int temp = first.data;
first.data = last.data;
last.data = temp;
}
// Adjacent nodes swapped
else if( first != null && middle !=
null )
{
int temp = first.data;
first.data = middle.data;
middle.data = temp;
}

// else nodes have not been swapped,
// passed tree is really BST.
}

/* A utility function to print
Inoder traversal */
void printInorder(Node node)
{
if (node == null)
return;
printInorder(node.left);
System.out.print(" " + node.data);
printInorder(node.right);
}


// Driver program to test above functions
public static void main (String[] args)
{
/* 6
/ \
10 2
/ \ / \
1 3 7 12

10 and 2 are swapped
*/

Node root = new Node(6);
root.left = new Node(10);
root.right = new Node(2);
root.left.left = new Node(1);
root.left.right = new Node(3);
root.right.right = new Node(12);
root.right.left = new Node(7);

System.out.println("Inorder Traversal"+
" of the original tree");
BinaryTree tree = new BinaryTree();
tree.printInorder(root);

tree.correctBST(root);

System.out.println("\nInorder Traversal"+
" of the fixed tree");
tree.printInorder(root);
}
}

Add a comment
Know the answer?
Add Answer to:
Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Could someone please summarize the following for my programming class? They are study questions for java...

    Could someone please summarize the following for my programming class? They are study questions for java What an association list is. How to test if an association list is empty. How to find the value associated with a key in an association list. How to add a key-value pair to an association list. How to delete a key-value pair from an association list. How efficient an association list is (using O notation). What a circular list is. What a circular...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...

    Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data and nextNode. The singly list class should contain the following methods . Middlelnsert-insert a node somewhere in the middle of the list . Startinsert-insert a node at start of the Linked list Endinsert-insert a node at the end of the Linked list . Delete-delete a node Traverse-prints all the node's data Reverse -reverses the linked list Note: Choose appropriate...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • Using JAVA Lab Exercises 1. Complete the class AVLTree that extends BST. It should have four...

    Using JAVA Lab Exercises 1. Complete the class AVLTree that extends BST. It should have four methods RotateLeft, Rotate Right RotateLeftRight, and RetateRightLeft. You have to provide the methods rotate Right(), rotateLeftRight and rotateRightLeft, Provide the method delete() to delete elements in the AVL tree. 2. Create an AVL tree in which the following keys are inserted in the given order: 8, 12, 14, 18, 20, 23, 15, 13, 7, 16 Then ask the user to provide any 3 elements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT