Question

In: Computer Science

On page 372 of book, an unbalanced tree is defined as a tree that is created...

On page 372 of book, an unbalanced tree is defined as a tree that is created when most of the nodes are on one side of the root or the other. By this definition we will consider a tree balanced if total number of nodes in the left subtree of the root is equal to total number of nodes in the right subtree of the root.

Implement isBalancedByNodeCount() method with code below (With Java)

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[] values2 = {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(values2);
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;
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 +"}");
}
}

Solutions

Expert Solution

Answer:---                                                            Date:--26/10/2020

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.

**************************/

   class A{

       int numberOfNodes(Node node){

           if(node==null) return 0;

           return 1+numberOfNodes(node.leftChild)+numberOfNodes(node.rightChild);

       }

   }

   A ob = new A();

   if(ob.numberOfNodes(root.leftChild)==ob.numberOfNodes(root.rightChild))

       return true;

   return false;

}

public static void main(String args[]) {

int[] values1 = {50, 25, 12, 23, 75, 65, 85};

int[] values2 = {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(values2);

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;

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 +"}");

}

}

Output:

Tree No. 1 is Balanced.

Tree No. 2 is NOT Balanced!

Note:

In the main method a logical error is found, instead of tree2.buildTreeFromArray(values1); it should be tree2.buildTreeFromArray(values2);

And another spelling mistake is found instead of int[] valeus2 = {50, 25, 12, 53, 75, 65, 85}; it should be

int[] values2 = {50, 25, 12, 53, 75, 65, 85};

plzzz rate me my answer....


Related Solutions

On page 372 of book, an unbalanced tree is defined as a tree that is created...
On page 372 of book, an unbalanced tree is defined as a tree that is created when most of the nodes are on one side of the root or the other. By this definition we will consider a tree balanced if total number of nodes in the left subtree of the root is equal to total number of nodes in the right subtree of the root. Implement isBalancedByNodeCount() method with this code below: public class BinarySearchTree { private Node root;...
Suppose that the number of printing mistakes on each page of a 200-page Mathematics book is...
Suppose that the number of printing mistakes on each page of a 200-page Mathematics book is independent of that on other pages. and it follows a Poisson distribution with mean 0.2. (a) Find the probability that there is no printing mistake on page 23. (b) Let page N be the first page which contains printing mistakes. Find (i) the probability that N is less than or equal to 3, (ii) the mean and variance of N. (c) Let M be...
A binary tree data type is defined in OCaml as follows: type 'a binary_tree = |...
A binary tree data type is defined in OCaml as follows: type 'a binary_tree = | Empty | Node of 'a * 'a binary_tree * 'a binary_tree;; The mirror of a binary tree is defined as the tree obtained by reversing its left and right subtrees at each level. Write an OCaml function is_mirror: 'a binary_tree -> 'a binary_tree -> bool = <fun> to determine if one tree is the mirror of another. Your function must take into account the...
11a. Why do some companies have lower WACC estimates than other companies? (page 372)   11b. How...
11a. Why do some companies have lower WACC estimates than other companies? (page 372)   11b. How could someone come up with significantly different WACC estimates? (page 372) 11c. Using the following information, calculate the CAPM cost of equity for WMT. (pages 365 and 372) Risk-free rate (10-year Treasury as of Dec 2019) 1.9% Market risk premium (Investopedia December 2019) 5.4% Beta 0.38 Walmart CAPM cost of equity estimate (Solution: 3.95%. Compare to 6.22% on page 372) 12. This is an...
how did the new deal and events after that created what an American is defined as?...
how did the new deal and events after that created what an American is defined as? help asap!
Create a Web page about a book ( The Richest Man in Babylon) that includes the...
Create a Web page about a book ( The Richest Man in Babylon) that includes the following: -page title -a meta keywords tag with at least (5) keywords -a meta description tag -a meta robots tag to allow indexing, but not permit crawling of the site Also, provide 1-2 paragraphs of content with three relevant text links.
Read through "The Huffman Code" on page 415 of the book. Write a program to implement...
Read through "The Huffman Code" on page 415 of the book. Write a program to implement Huffman coding and decoding. It should do the following: Accept a text message, possibly of more than one line. Create a Huffman tree for this message. Create a code table. Encode the message into binary. Decode the message from binary back to text. in JAVA
Read through "The Huffman Code" on page 415 of the book. Write a program to implement...
Read through "The Huffman Code" on page 415 of the book. Write a program to implement Huffman coding and decoding. It should do the following: Accept a text message, possibly of more than one line. Create a Huffman tree for this message. Create a code table. Encode the message into binary. Decode the message from binary back to text.
Read through "The Huffman Code" on page 415 of the book. Write a program to implement...
Read through "The Huffman Code" on page 415 of the book. Write a program to implement Huffman coding and decoding. It should do the following: Accept a text message, possibly of more than one line. Create a Huffman tree for this message. Create a code table. Encode the message into binary. Decode the message from binary back to text. in java please
In Chapter 5, the book touches on Diffusion of Innovation on page 152 but I wanted...
In Chapter 5, the book touches on Diffusion of Innovation on page 152 but I wanted to provide you with a little more detail on the topic. It's really important when you think about change- this could be the kind of change that comes when you develop a new product, modify an existing one, or make any kind of organizational change. It's all about how to people adopt (or accept) innovation. The process by which the use of an innovation-...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT