Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

6A. (20 points) Below is the BinaryNode class from the review session used to make a BinarySearchTree. The following pages contain part of the BinarySearchTree

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
6A. (20 points) Below is the BinaryNode class from the review session used to make a BinarySearchTree. The following pages contain part of the BinarySearchTree implementation. You are to add the following method to the BinarySearchTree class (do not miss the second part of this question): Override java's equals method (public boolean equals (Object o)). This method returns true if and only if the two trees have the same elements in the tree. Note: This is not the same denition of equality from the sample question we did in class. Under this denition, two trees are equal if, and only if they have the same elements in them regardless of position. You can use the following methods from the Stack class (Note: I changed the element type of the Queue class to Comparable to make it easier): public Stack( ); public boolean isEmpty( ); public Comparable pop( ); public void push( Comparable X ); Do not worry about catching overows from the Stack class you can assume the stack can hold all your data. Basically, you want to build two stacks; one with the elements of each BinarySearchTree. Then cycle through them to make sure they contain all the same elements. (You could use a different algorithm if you had a successor method for the BinarySearchTree but you do not have one.) The order in which you place the elements into the stack is very important. Your algorithm should only visit each element in each stack one time (as opposed to cycling through one of the queues for each element in the other queue). You can use as many helper methods as you see t. One solution uses just one recursive helper method for building the queues. Note: Your eg uals method should not modify the two trees in any way. 6B. (5 points) What is the Big Oh for your algorithm? // Basic node stored in unbalanced binary search trees / / Note that this class is not accessible outside / / of package DataStructures class BinaryNode // Constructors BinaryNode ( Comparable theElement ) this ( theElement, null, null ) ; BinaryNode ( Comparable theElement, BinaryNode It, BinaryNode rt ) element = theElement; left = 1t; right = rt; Comparable element; / / The data in the node BinaryNode left; / / Left child BinaryNode right; / / Right childpublic class BinarySearchTree { /** * Construct the tree. */ public BinarySearchTree( ) { root = null; } /** * Insert into the tree; duplicates are ignored. * @param x the item to insert. */ public void insert( Comparable x ) { root = insert( x, root ); } /** * Remove from the tree. Nothing is done if x is not found. * @param x the item to remove. */ public void remove( Comparable x ) { root = remove( x, root ); } /** * Find the smallest item in the tree. * @return smallest item or null if empty. */ public Comparable findMin( ) { return elementAt( findMin( root ) ); } /** * Find the largest item in the tree. * @return the largest item of null if empty. */ public Comparable findMax( ) { return elementAt( findMax( root ) ); } /** * Find an item in the tree. * @param x the item to search for. * @return the matching item or null if not found. */ public Comparable find( Comparable x ) { return elementAt( find( x, root ) ); } /** * Make the tree logically empty. */ public void makeEmpty( ) { root = null; } /** * Test if the tree is logically empty. * @return true if empty, false otherwise. */ public boolean isEmpty( ) { return root == null; } /** * Internal method to get element field. * @param t the node. * @return the element field or null if t is null. */ private Comparable elementAt( BinaryNode t J { return t == null ? null : t.element; } /** * Internal method to insert into a subtree. * @param x the item to insert- * @param t the node that roots the tree. * @return the new root. */

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions