Question

In: Computer Science

In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write...

In java,

Finding the maximum value in a BST (Binary Search Tree). Students need to write the code.

Solutions

Expert Solution

As we have to find the maximum value in a BST (Binary Search Tree), let us understand the properties of BST,

1. The element which is smaller than the root element will go to the left side of BST.

2. The element which is greater than the root element will go to the rigth side of BST.

Our aim is to find the maximum value in a BST, so no doubt it will be in the most right side of the BST.

We will create 2 classes namely

  1. Node class
  2. BST_MAX class

Node class contains the basic structure of a node, that is value, leftChild and rightChild.

BST_MAX class contains main method and methods for creating new node, inserting new node.

Let us see and understand the code,

import java.util.Scanner;
//creating structure of a node using class
class Node 
{ 
        int value; 
        Node leftChild; 
        Node rightChild; 
};
 
//class contains main method
class BST_MAX 
{ 
        
        //method for creating new node
        //new node does not have left and right child hence null
        public static Node newNode(int value) 
        { 
            Node node = new Node(); 
            node.value = value; 
            node.leftChild = null; 
            node.rightChild = null; 
            return node; 
        } 
  
        //method to insert new node everytime 
        public static Node insertNode(Node node, int value) 
        { 
                //check if BST is empty (whether it first node of tree)
                if (node == null) 
                {
                    return newNode(value); 
                }
                //not the first node (traverse and feed value in right position)
                else 
                { 
                        //if value is less than or equal to parent node then keep recursion at left side
                        if (value <= node.value) 
                        {
                                node.leftChild = insertNode(node.leftChild, value); 
                        }
                        //if value is greater than parent node then keep recursion with right side 
                        else
                        {
                                node.rightChild = insertNode(node.rightChild, value); 
                        }
                } 
                return node; 
        } 
  
        //method to find max element in BST
        //traversing the tree to the right most side    
        public static int maxElement(Node node) 
        {
                Node temp = node; 
                while (temp.rightChild != null)
                {
                        temp = temp.rightChild; 
                }      
                return temp.value; 
        } 

        //main method (execution starts from here)
        public static void main(String args[])  
        { 
                Node root = null; 
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter total number of elements: ");
                int n=sc.nextInt();
                //if thier is no value
                if(n<=0)
                {
                        System.out.println("Does not have max element");
                } 
                //else values exists in BST
                else
                {
                        System.out.println("Enter values: ");
                        int value=sc.nextInt();
                        root=insertNode(root,value);    //root node
                        while(n>1)
                        {
                                value=sc.nextInt();
                                insertNode(root,value);
                                n--;
                        }
                        int max=maxElement(root);
                        System.out.println("Maximum Value in Binary Search Tree is "+max);
                }       
        } 
} 

The code is successfully compiled and asked the user to enter total number of elements want to enter in BST.

Compile and Run in cmd:

INPUT:

OUTPUT:


Related Solutions

• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a...
• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a preorder and a Breadth First Search (BFS) way. • The program gets a parameter ? from the command line and stores ? randomly generated integers in a dynamic array. To generate a random integer between 1 and Max-Rand (a system constant): ▪ Use srand() to seed the rand() function. Then, call rand() again and again (? times) to generate ? random integers. Next, printout...
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
What is a binary search tree or BST? Discuss its characteristics. What are other types of...
What is a binary search tree or BST? Discuss its characteristics. What are other types of binary trees as well?
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports...
Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports an efficient procedure which, on input (x, k) where x is the root of a BST and k an integer, output the k-th smallest number store in the BST. Let n denote the total number of elements stored in the BST, what is the running time of your efficient procedure? How does your modification of the BST data structure affect the performance of other...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use...
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use any data structure, library functions) ---------- 20 points i) Depth First Traversal: Inorder, LVR ii) Depth First Traversal: Inorder, RVL iii) Depth First Traversal: Preorder, VLR iv) Depth First Traversal: Preorder, VRL v) Depth First Traversal: Postorder, LRV vi) Depth First Traversal: Postorder, RLV No choice menu required. Sample Input (taken from keyboard, single space separated nodes as found in a complete BFS: top-down,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT