f (root==null || root.key==key)            return root;              if (root.key > key)            return searchRec(root.left, key);              return searchRec (root.right, key);     }              void deleteKey(int key) {            root = deleteRec(root, key);      }      /* A recursive function to insert a new key in BST */      BSTNode deleteRec(BSTNode root, int key) {            /* Base Case: If the tree is empty */            if (root == null) return root;              /* Otherwise, recur down the tree */            if (key < root.key)                 root.left = deleteRec(root.left, key);            else if (key > root.key)                 root.right = deleteRec(root.right, key);              // if key is same as root's key, then This is the target           else {                 // node with only one child or no child                 if (root.left == null)                       return root.right;                 else if (root.right == null)                       return root.left;                   // node with two children: Get the inorder successor                 // (smallest in the right subtree)                 root.key = minValue(root.right);                   // Delete the inorder successor                 root.right = deleteRec(root.right, root.key);            }              return root;      }            int minValue(BSTNode root) {            int minv = root.key;            while (root.left != null) {                 minv = root.left.key;                 root = root.left;            }            return minv;      }              void insert(int key) {            root = insertRec(root, key);      }      /* A recursive function to insert a new key in BST */      BSTNode insertRec(BSTNode ro

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

class BSTNode  {

     int key;

     BSTNode left, right;

 

     public BSTNode(int item) {

           key = item;

           left = right = null;

     }

}  

 

class BST_Tree  {

     

     BSTNode root;

 

     BST_Tree()  {   // Constructor

           root = null;

     }

 

    boolean search(int key){

        return (searchRec(root, key) != null);

    }

    

    public BSTNode searchRec(BSTNode root, int key) {

    

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

           return root;

   

    

    if (root.key > key)

           return searchRec(root.left, key);

   

    

    return searchRec (root.right, key);

    }

 

     

     void deleteKey(int key) {

           root = deleteRec(root, key);

     }

     /* A recursive function to insert a new key in BST */

     BSTNode deleteRec(BSTNode root, int key) {

           /* Base Case: If the tree is empty */

           if (root == null) return root;

 

           /* Otherwise, recur down the tree */

           if (key < root.key)

                root.left = deleteRec(root.left, key);

           else if (key > root.key)

                root.right = deleteRec(root.right, key);

 

           // if key is same as root's key, then This is the target

          else {

                // node with only one child or no child

                if (root.left == null)

                      return root.right;

                else if (root.right == null)

                      return root.left;

 

                // node with two children: Get the inorder successor

                // (smallest in the right subtree)

                root.key = minValue(root.right);

 

                // Delete the inorder successor

                root.right = deleteRec(root.right, root.key);

           }

 

           return root;

     }

     

     int minValue(BSTNode root) {

           int minv = root.key;

           while (root.left != null) {

                minv = root.left.key;

                root = root.left;

           }

           return minv;

     }

 

     

     void insert(int key) {

           root = insertRec(root, key);

     }

     /* A recursive function to insert a new key in BST */

     BSTNode insertRec(BSTNode root, int key) {

           /* If the tree is empty, return a new node */

           if (root == null) {

                root = new BSTNode(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 root;

     }

 

     

     void inorder() {

           inorderRec(root);

     }

     // A utility function to do inorder traversal of BST

     void inorderRec(BSTNode root) {

           if (root != null) {

                inorderRec(root.left);

                System.out.print(root.key + " ");

                inorderRec(root.right);

           }

     }

 

     void preorder() {

           preorderRec(root);

     }

     

     void preorderRec(BSTNode root) {

           if (root != null) {

                System.out.print(root.key + " ");

                preorderRec(root.left);

                preorderRec(root.right);

           }

     }

 

     void postorder()      {

           postorderRec(root);

     }

     

     void postorderRec(BSTNode root) {

           if (root != null) {

                postorderRec(root.left);

                postorderRec(root.right);

                System.out.print(root.key + " ");

           }

     }   

}

 

public class BST_Recursive  {

     public static void main(String[] args)  {

 

           BST_Tree tree = new BST_Tree();

             System.out.println("Build a tree  by inserting :\n 50 30 20 40 70 60 80");

           tree.insert(50);

           tree.insert(30);

           tree.insert(20);

           tree.insert(40);

           tree.insert(70);

           tree.insert(60);

           tree.insert(80);

 

             System.out.println("\nPre-order traversal of the given tree");

           tree.preorder();

             System.out.println("\nIn-order traversal of the given tree");

           tree.inorder();

             System.out.println("\nPost-order traversal of the given tree");

           tree.postorder();

 

           System.out.println("\n\nDelete 20");

           tree.deleteKey(20);

             System.out.println("Inorder traversal of the modified tree");

           tree.inorder();

 

           System.out.println("\nDelete 30");

           tree.deleteKey(30);

             System.out.println("Inorder traversal of the modified tree");

           tree.inorder();

 

           System.out.println("\nDelete 50");

           tree.deleteKey(50);

             System.out.println("Inorder traversal of the modified tree");

           tree.inorder();

 

           System.out.println("\nSearch 70");

           System.out.println("Found ? :  " + tree.search(70) );

     }

}

Task 1:   

Modify BST_Recursive.java  to write a complete program to implement a spare-parts database with itemNo, price and description. Provide options to insert, search, delete, and show all items in order of itemNo. 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY