Question

In: Computer Science

Please write a Java algorithm solving the following problem: Implement a Java method to check if...

Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.

1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree:

public class BinTreeNode<T>

{

private T key;

private Object satelliteData;

private BinTreeNode<T> parent;

private BinTreeNode<T> leftChild;

private BinTreeNode<T> rightChild;

public BinTreeNode<T>(<T> key, Object satelliteData); // constructor

}

supporting the following public methods:

public void addLeftChild(BinTreeNode leftChildNode);

public void addRightChild(BinTreeNode rightChildNode);

public void setParent(BinTreeNode parentNode);

public BinTreeNode<T> getParent();

public BinTreeNode<T> getLeftChild();

public BinTreeNode<T> getRightChild();

and

public class BinTree<T> {

private BinTreeNode<T> root;

}

supporting the following methods

public BinTreeNode<T> getRoot();

public void setRoot(BinTreeNode<T>);

2. Add method public boolean isBalanced() 2 to the class BinTree, which returns true if the tree is balanced, and false otherwise.

Solutions

Expert Solution

public boolean isBalanced()
{
   return isBalanced(getRoot());
}
public boolean isBalanced(BinTreeNode<T> node)
{
int lh; /* for height of left subtree */
  
int rh; /* for height of right subtree */
  
/* If tree is empty then return true */
if (node == null)
return true;
  
/* Get the height of left and right sub trees */
lh = height(node.getLeftChild());
rh = height(node.getRightChild());
  
if (Math.abs(lh - rh) <= 1
&& isBalanced(node.getLeftChild())
&& isBalanced(node.getRightChild()))
return true;
  
/* If we reach here then tree is not height-balanced */
return false;
}
//helper method
int height(BinTreeNode<T> node)
{
/* base case tree is empty */
if (node == null)
return 0;
  
/* If tree is not empty then height = 1 + max of left
height and right heights */
return 1 + Math.max(height(node.getLeftChild()), height(node.getRightChild()));
}
  


Related Solutions

Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Write an algorithm to check equality of two link lists.(java)
Write an algorithm to check equality of two link lists.(java)
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
Please implement the java method addInOrder() that allows you to create and maintain the lists in...
Please implement the java method addInOrder() that allows you to create and maintain the lists in the order required. The addInOrder method must work with different external Comparator objects. (Hint: Consider creating and using a private compare method and a private Comparator reference as members of your SLL class. If your SLL is constructed without any parameter, then you should initialize the internal Comparator object reference to null. Otherwise, you should initialize it to the external Comparator object passed as...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
Describe an efficient recursive algorithm for solving the element uniqueness problem
Describe an efficient recursive algorithm for solving the element uniqueness problem, which runs in time that is at most O(n2) in the worst case without using sorting.    
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT