Question

In: Computer Science

JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method:...

JAVA (Tree height)

Define a new class named BSTWithHeight that extends BST with the following method:

/** Return the height of this binary tree */
public int height()

Class Name: Exercise25_01

Solutions

Expert Solution

public class BST {

   /* Class containing left and right child of current node and key value*/
class Node {
int key;
Node left, right;

public Node(int item) {
key = item;
left = right = null;
}
}

// Root of BST
Node root;

// Constructor
BST() {
root = null;
}

// This method mainly calls insertRec()
void insert(int key) {
root = insertRec(root, key);
}

/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key) {

/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}

/* Otherwise, recur down the tree */
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);

/* return the (unchanged) node pointer */
return root;
}

// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}

// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}

}

public class BSTwithHEight extends BST {

   static int height(Node node)
{
if (node == null)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = height(node.left);
int rDepth = height(node.right);

/* use the larger one */
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
  
   public static void main(String[] args) {
       BSTwithHEight tree = new BSTwithHEight();
       tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
  
System.out.println("height is : "+height(tree.root));
  

   }

}


Related Solutions

Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
I need this done in JAVA. Define a class named Cash. The class contains the following...
I need this done in JAVA. Define a class named Cash. The class contains the following public elements: A Double stored property that contains the amount of money (dollars and cents) described by an object of the class. A read-only, computed property. It calculates and returns the minimum number of U.S. bills and coins that add up to the amount in the stored property.  The return value is an Int array of length 9 that contains (beginning with index 0 of...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
java script Define a function named height which has two inputs. This first input is the...
java script Define a function named height which has two inputs. This first input is the number of feet. The second input is the number of inches. Both inputs will be Number values. Your function must calculate and return a Number. he returned value represents number of meters equal to the input. Your function should start by multiplying the first parameter by 0.3048 (1 foot is 0.3048 meters). Then multiply the second input by 0.0254 (1 inch is 0.0254 meters)....
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT