Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

binary search tree in java plz solve all the Qs (methods) and read the Qs carefully i included the class BST node , class BST(to

binary search tree in java

plz solve all the Qs (methods) and read the Qs carefully

i included the class BST node , class BST(to write all the methods )and test (main methode)

HERE R THE Qs

1. A method that generate a binary search tree (BST) where the number of nodes and the range of the values are

from a file. ---> i didn't know how to do this so i made the input from the main

2. A recursive method to find the largest value in a BST

3. A method to find and return the smallest value in a BST.

4. A method to search for a given value V in a BST.

5. A method to count the number of ancestors of a given value V

6. A method to count the number of comparisons to decide whether a given value V is in a BST.

7. A method to count the number of leaf nodes in BST

8. A method to count the number of nodes that contain even numbers in a BST

CLASSBST NODE

package bst;

public class BSTclassNode {

private Comparable value; private BSTclassNode left; private BSTclassNode right;

public BSTclassNode(Comparable value) { this.value = value; left = null; right = null; }

public boolean add(Comparable value) { if (value.compareTo(this.value) == 0) { return false; } else if (value.compareTo(this.value) < 0) { if (left == null) { left = new BSTclassNode(value); return true; } else { return left.add(value); } } else if (value.compareTo(this.value) > 0) { if (right == null) { right = new BSTclassNode(value); return true; } else { return right.add(value); } } return false; }

public boolean search(Comparable value) { if (value.compareTo(this.value) == 0) { return true; } else if (value.compareTo(this.value) < 0) { if (left == null) { return false; } else { return left.search(value); } } else if (value.compareTo(this.value) > 0) { if (right == null) { return false; } else { return right.search(value); } } return false; }

public void printInOrder(BSTclassNode node) { if (node == null) { return; }

printInOrder(node.left); System.out.println(node.value); printInOrder(node.right); }

public void postOrder(BSTclassNode node) { if (node == null) { return; }

postOrder(node.left); postOrder(node.right); System.out.println(node.value); }

public void preOrder(BSTclassNode node) { if (node == null) { return; } System.out.println(node.value); preOrder(node.left); preOrder(node.right); }

public int countNodes(BSTclassNode node) { int count = 0; if (node == null) { return 0; } count += countNodes(node.left); count++; count += countNodes(node.right); return count; }

// public int getMax(BSTclassNode node) { // if (node.right == null) { // return 0; // } // return Math.max(0, getMax(node.right)); // } }

CLASS BST( TO WRITE THE METHODS)

package bst;

public class BinarySearchTree {

private BSTclassNode root; private BSTclassNode right;

public BinarySearchTree() { root = null; }

public boolean add(Comparable value) { if (root == null) { root = new BSTclassNode(value); return true; } else { return root.add(value); } }

public boolean search(Comparable value) { if (root == null) { return false; } else { return root.search(value); } }

public void printInOrder() { if (root == null) { return; } else { root.printInOrder(root); } }

public void postOrder() { if (root == null) { return; } else { root.printInOrder(root); } }

public void preOrder() { if (root == null) { return; } else { root.printInOrder(root); } }

public int countNodes() { if (root == null) { return 0; } else { return root.countNodes(root); } }

// public int getMax() { // if (right == null) { // return 0; // } else { // return right.getMax(right); // }} }

MAIN METHOD

package bst;

public class TestBST {

public static void main(String[] args) { BinarySearchTree myBST = new BinarySearchTree(); myBST.add(10); myBST.add(5); myBST.add(15); myBST.add(3); myBST.add(13); myBST.add(1); myBST.add(20);

System.out.println("printing post order"); myBST.postOrder(); System.out.println("printing pre order"); myBST.preOrder(); System.out.println("printing in order"); myBST.printInOrder(); System.out.println("The number of nodes is " + myBST.countNodes()); System.out.println("Searching for 13 " + myBST.search(13)); System.out.println("Searching for 8 " + myBST.search(8)); // System.out.println("getting max number in BST " +myBST.getMax()); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

Q.1. Taxonomic classification of peafowl, Tiger and cow ?

Answered: 1 week ago

Question

Q .1. Different ways of testing the present adulterants ?

Answered: 1 week ago

Question

Q.1. Health issues caused by adulteration data ?

Answered: 1 week ago

Question

1. Traditional and modern methods of preserving food Articles ?

Answered: 1 week ago