Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is BST code: public class BST{ BSTNode root; //Constructor public BST(){ root=null; } //Method to serach a key public boolean search(int k){ BSTNode cur=null;

image text in transcribed

Here is BST code:

public class BST{ BSTNode root; //Constructor public BST(){ root=null; } //Method to serach a key public boolean search(int k){ BSTNode cur=null; while(cur!=null){ if(cur.key==k) return true; else if(k

--------------------------

public class BSTNode{ int key; BSTNode left; BSTNode right; //Constructor public BSTNode(int k){ key=k; left=null; right=null; } }

The purpose of this questions is to observe the behavior of binary search trees when inserting random keys ( integers). You have already given a code for binary search tree (see zip file BST) a Add a function private int Height(BSTNode r) to the BST class. This function will compute the height of the subtree rooted at node r. The running time of your method should be O(1) in the worst-case. Hint: Add another field called int height to BSTNode.java class and update the height parameter during insertion] b Add a function public int HeightBST() to the BST class. This functiorn will compute the height of the binary search tree. The running time of your method should be O(1) in the worst-case c Add a method called private int subtreeSize(BSTNode r) to BST class that takes a node r and return the number of nodes in the subtree rooted at r included r itself. The running time of your method should be O(1) in the worst-case. Hint: Add another field called int size to BSTNode.java class and update size parameter during insertion

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

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions

Question

To find integral of sin(logx) .

Answered: 1 week ago

Question

What is Centrifugation?

Answered: 1 week ago

Question

Question Can a self-employed person adopt a profit sharing plan?

Answered: 1 week ago