Question: 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;

 Here is BST code: public class BST{ BSTNode root; //Constructor public

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!