Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete part A and B for the question below. You can write either the code or pseudocode. Let the BinarySearchTree class be defined as

Please complete part A and B for the question below. You can write either the code or pseudocode.
 Let the BinarySearchTree class be defined as follows: class BinarySearchTree { class Tree { int element; Tree left, right; Tree(int x, Tree l, Tree r) { element = x; left = l; right = r; } } Tree root; // root of binary search tree int size; // number of elements in the BST BinarySearchTree() { // constructor root = null; size = 0; } BinarySearchTree(Tree t, int s) { // constructor root = t; size = s; } } A. Given a sorted array of integers, write an algorithm to build a height-balanced binary search tree with its elements. Analyze the running time of the algorithm. Recursive algorithms can be solved using the Master method. The functions that you need to write are the following. // Build a height-balanced BST from arr[0..arr.length-1] BinarySearchTree arrayToBST(int[] arr) { /* To do */ } // Helper function to build a tree from a subarray // Recursive algorithm that builds a tree recursively from the elements of arr[p..r]. // Returned tree is balanced, and satisfies the order constraints of a BST. Tree arrayToTree(int[] arr, int p, int r) { /* To do */ } B. Given a binary search tree of integers, and an integer x, write the functions floor and ceiling. Hint: If x is not in the tree, the floor and ceiling elements are on the path taken by find(x). // Class to store 2 integers class Pair { int floor, ceiling; Pair(int f, int c) { floor = f; ceiling = c; } } // Floor: largest element of the BST that is less than or equal to x // Ceiling: smallest element of the BST that is greater than or equal to x Pair floorAndCeiling(BinarySearchTree t, int x) { /* To do */ } 

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

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

c. Will leaders rotate periodically?

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago