Question

How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...

How do I implement this method BalancedByNodeCount() ?

public class BinarySearchTree {
private Node root;

private boolean isBalancedByNodeCount() {
/****************************************************************************
Implement this method and replace the return statement below with your code.
* Definition of Balance tree (On page 372 of book):
* An unbalanced tree is created when most of the nodes are on one side of the root or the other.
****************************************************************************/
  
return false;
}
  
  
public static void main(String args[]) {
int[] values1 = {50, 25, 12, 23, 75, 65, 85};
int[] valeus2 = {50, 25, 12, 53, 75, 65, 85};
  
BinarySearchTree tree1 = new BinarySearchTree();
tree1.buildTreeFromArray(values1);
if (tree1.isBalancedByNodeCount())
System.out.println("Tree No. 1 is Balanced.");
else
System.out.println("Tree No. 1 is NOT Balanced!");
  
BinarySearchTree tree2 = new BinarySearchTree();
tree2.buildTreeFromArray(values1);
if (tree2.isBalancedByNodeCount())
System.out.println("Tree No. 2 is Balanced.");
else
System.out.println("Tree No. 2 is NOT Balanced!");
}
  
private void buildTreeFromArray(int[] values) {
for (int i = 0; i < values.length; i++) {
double d = values[i] * 2.5;
insert(values[i], d);
}
}
/***********************************************************************************
************ Code below this is copied from our implementation in class ************
************************************************************************************/
private void insert(int key, double data) {
Node n = new Node(key, data);
  
if (root == null)
root = n;
else {
Node current = root;
Node parent;
  
while (true) {
parent = current;
if (key < current.key) {
current = current.leftChild;
if (current == null) {
parent.leftChild = n;
return;
}
} else {
current = current.rightChild;
if (current == null) {
parent.rightChild = n;

Homework Answers

Answer #1

CODE

import java.util.Scanner;
public class BinarySearchTree {
private Node root;

private boolean isBalancedByNodeCount() {
int lh;
int rh;  
  
if (root == null)
return true;
  
lh = height(leftChild);
rh = height(rightChild);
  
if (Math.abs(lh - rh) <= 1
&& leftChild.isBalancedByNodeCount()
&& rightChild.isBalancedByNodeCount())
return true;
return false;
}
int height(Node node)
{  
if (node == null)
return 0;
    if(height(leftChild)>height(rightChild))
return 1 + height(leftChild);

else

  return 1 + height(rightChild);
}
public static void main(String args[]) {
int[] values1 = {50, 25, 12, 23, 75, 65, 85};
int[] valeus2 = {50, 25, 12, 53, 75, 65, 85};
  
BinarySearchTree tree1 = new BinarySearchTree();
tree1.buildTreeFromArray(values1);
if (tree1.isBalancedByNodeCount())
System.out.println("Tree No. 1 is Balanced.");
else
System.out.println("Tree No. 1 is NOT Balanced!");
BinarySearchTree tree2 = new BinarySearchTree();
tree2.buildTreeFromArray(values1);
if (tree2.isBalancedByNodeCount())
System.out.println("Tree No. 2 is Balanced.");
else
System.out.println("Tree No. 2 is NOT Balanced!");
}
private void buildTreeFromArray(int[] values) {
for (int i = 0; i < values.length; i++) {
double d = values[i] * 2.5;
insert(values[i], d);
}
}

private void insert(int key, double data) {
Node n = new Node(key, data);
if (root == null)
root = n;
else {
Node current = root;
Node parent;  
while (true) {
parent = current;
if (key < current.key) {
current = current.leftChild;
if (current == null) {
parent.leftChild = n;
return;
}
} else {
current = current.rightChild;
if (current == null) {
parent.rightChild = n;
return;
}
}
}   
}
}
}

class Node {
int key;
double data;
Node leftChild;
Node rightChild;
Node(int key, double data) {
this.key = key;
this.data = data;
}
void displayNode() {
System.out.println("{" + key + ", " + data +"}");
}
}

If the code works for you please UPVOTE my answer or if you face any problem comment bellow.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted below) Implement the following methods using recursion: int depth() // returns the length of the deepest path from root to any leaf int node_count() // returns the number of nodes in the tree void insert(int n) // inserts value n into the tree BinarySearchTree2C clone() // returns a clone (deep copy) of the tree Add code to the main method to test these methods....
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
I have written working code for this problem however it is not producing an output and...
I have written working code for this problem however it is not producing an output and it states is a wrong answer. Please just fix my code below and make it work for the sample test cases and all test cases. You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class...
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3BST. Add two public methods: one named countTwins that takes no parameters and that, when called, traverses every node of the tree to count the number of nodes having two children. one named lessThanValueCount that takes a paremeter of generic type V, and that, when called, traverses every node of the tree to count the number of nodes whose value (not its key) is...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Add a method to RedBlackBST to print a RB-BST indexing the subnodes. Test the tree inserting...
Add a method to RedBlackBST to print a RB-BST indexing the subnodes. Test the tree inserting element by element and printing the results with (1) S E A R C H E X A M P L E (where the value is the index of the letter in the initial word) and (2) K R I S T E N public class RedBlackBST<Key extends Comparable<Key>, Value> { private Node root; private class Node // BST node with color bit (see...
How do I make this: public class Country {     private String name;     private double area;     private...
How do I make this: public class Country {     private String name;     private double area;     private int population;     public Country(String name, double area, int population) {         this.name = name;         this.area = area;         this.population = population;     }     public double getPopulationDensity() {         return population / area;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public double getArea() {         return area;     }     public void setArea(double area) {         this.area = area;     }     public int getPopulation()...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT