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;
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.
Get Answers For Free
Most questions answered within 1 hours.