• Home
  • University of the People
  • Data Structures (proctored course) CS 3303
  • Implementing a Binary Search Tree in Java

Implementing a Binary Search Tree in Java

Note that I have attached the files for the program in the attachment. You can open those in your IDE and run it to test my program. Thank you. Overview of program structure and functioning I have chosen to implement the tree structure using a link list. The project involves creating a Java program that constructs a Binary Search Tree (BST) from user-entered integers. Users input integers one by one, which are inserted into the BST following the binary search property: for any given node, all values in the left subtree are smaller, and in the right subtree are larger. After all integers are entered, the user inputs a search value. The program then searches the BST for this value, utilizing a recursive approach that tracks and reports the number of iterations required to find the value, demonstrating the efficiency of data retrieval in a BST structure. Code import java.util.Scanner; / ** -* This class demonstrates tree traversal in a binary search tree. -* -* @author Gilles Babou -* / public class Tree Traversal { - - / ** - - This class represents a node in the binary search tree. - - -* / - - public static class Node { - - - - int value; - - - - Node left, right; - - - - / ** - - - - * Constructs a new node with the given value. - - - - - - - - @param item the value of the node - - - - */ - - - - public Node(int item) { - - - - - - value = item; - - - - - - left = right = null; - - - - } - - } - - / ** - - This class represents a binary search tree. -- -* / - - public static class BinarySearch Tree { - - - - - - - - - - Node root; - / ** - * Constructs a new, empty binary search tree. - - - - -* / - - - - - - - - root = null; - - BinarySearchTree() { - - - -} - - - - / ** - - - - - - - - * * Inserts a new value into the binary search tree. - - - - @param value the value to insert - - - - */ - - - - void insert(int value) { - - - - - root = insertRec(root, value); - - - - } - - - - / ** - - - - * Helper method for insert(int value). Recursively inserts a new value into the binary search tree. - - - - - - - - * @param root the root of the current subtree - - - - * @param value the value to insert - - - - * @return the new root of the current subtree - - - - */ - - -