Create a project using the classes BinarySearchTree, Node, and Main in Binary Search Tree. Compile the project
Create a project using the classes BinarySearchTree, Node, and Main in Binary Search Tree. Compile the project,
run it, and review the code that is given carefully. These programs test the code discussed in our lecture.
Exercise 2: An Improved BST Class
Add the toString method to the class BinarySearchTree in Exercise 1.
Note: The toString method returns a string representation of the object properties. By implementing toString, a BinarySearchTree object can be displayed in a simple way using System.out.print or System.out.println. For example, if bst is a BinarySearchTree object, it can be printed using System.out.println(bst).
Exercise 3: Using a BST in an Application
Create a class SimpleBag that uses a binary search tree to store the bag items.The class should have the methods listed below. Create a Main class to test your SimpleBag class.
- SimpleBag(): default constructor; creates an empty bag
- boolean isEmpty(): determines whether the bag is empty
- void print(): prints the SimpleBag elements
- void clear(): removes all of the items from the bag
- void add(int item): adds an item to the bag
- int count(int item): counts the number of occurrences of items in the bag.
Exercise 4: Recursion and Binary Trees
Add a recursive function getHeight to the BinarySearchTree class in Exercise 1 that returns the height of the tree. Create a Main class to test it.
Exercise 5: Using Properties of BSTs
Add methods preorderDisplay and postorderDisplay to the BinarySearchTree class in Exercise 1 to print the items in the BST listed in preorder and postorder, respectively. Create a Main class to test them.