Question

In: Computer Science

AVL trees in Java For a given adjacency matrix of a rooted tree you need to...

AVL trees in Java

For a given adjacency matrix of a rooted tree you need to decide whether this tree can be labeled to become an avl tree.

Vertices of the tree do not have keys but numbered from 0 to n − 1 in order to write the adjacency matrix.

INPUT format: The input consists of blocks. Blocks are written one after another without empty lines between them. Every block starts with a new line and consists of several lines:

• the first line contains a positive integer number n which corresponds to the number of nodes in a tree and the root index;

• the next n lines correspond to the adjacency matrix of this tree.

OUTPUT format: The answer for every block should consist of the height of the tree and True/False (True if the tree is an avl tree).

Answers for different blocks should be located in different lines.

Input example:

5 3
0 0 0 0 0
1 0 1 0 0
0 0 0 0 0
0 1 0 0 1
0 0 0 0 0
4 0
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0
10 3
0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
10 6
0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0

Output example:

2 True
3 False
3 True
4 False

please make comments for code

Solutions

Expert Solution

Java code for above problem

import java.util.*;

// class for a node in tree
class Node
{
   int data;
   Node left,right;
   Node(int data)
   {
       this.data=data;
       this.left=null;
       this.right=null;
   }
}
class Main
{
   // global variable to say that given tree is AVL or not
   public static boolean isAVL=true;
  
   // testing main method
   public static void main(String args[])
   {
       Scanner file=new Scanner(System.in);
       int n=file.nextInt();
       int start=file.nextInt();
       int [][] graph=new int[n][n];
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               graph[i][j]=file.nextInt();
           }
       }
       Node root=create_graph(graph,n,start);
       System.out.println(height(root)+" "+(isAVL(root) ? "True" : "False"));
   }
  
   // method that returns the height of tree of given root node
   public static int height(Node root)
   {
       if(root==null || (root.left==null && root.right==null))
           return 0;
       int leftHeight=height(root.left);
       int rightHeight=height(root.right);
       return 1+Math.max(leftHeight,rightHeight);
   }
  
   // method that returns whether the tree with given root is AVL or not
   public static boolean isAVL(Node root)
   {
       isAVL=true;
       checkForAVL(root);
       return isAVL;
   }
  
   // helper method for isAVL() method
   public static int checkForAVL(Node root)
   {
       if(root==null || (root.left==null && root.right==null))
           return 0;
       int leftHeight=checkForAVL(root.left);
       int rightHeight=checkForAVL(root.right);
       if(Math.abs(leftHeight-rightHeight)>1)
           isAVL=false;
       return 1+Math.max(leftHeight,rightHeight);
   }
  
   // method that creates a tree of given graph
   public static Node create_graph(int graph[][],int n,int index)
   {
       Node node=new Node(index);
       for(int i=0;i<n;i++)
       {
           if(graph[index][i]==1)
           {
               if(node.left==null)
               {
                   node.left=create_graph(graph,n,i);
               }
               else
               {
                   node.right=create_graph(graph,n,i);
               }
           }
       }
       return node;
   }
}

Sample output

Mention in comments if any mistakes or errors are found. Thank you.


Related Solutions

How do you implement an AVL tree for strings in Java?
How do you implement an AVL tree for strings in Java?
AVL tree; Insert and Range Minimum operation in Java code.
AVL tree; Insert and Range Minimum operation in Java code.
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
Given the following adjacency matrix, A, for nodes a, b, c, and d, find the transitive...
Given the following adjacency matrix, A, for nodes a, b, c, and d, find the transitive closure of A. Is the result an equivalence relation, and why or why not? A = ⌈ 1 0 1 0 ⌉ | 0 1 1 0 | | 1 0 0 1 | ⌊ 1 1 0 0 ⌋
You are to draw the AVL trees that result after inserting each of the following keys...
You are to draw the AVL trees that result after inserting each of the following keys in the order given: TCG, TAC, AAC, TGg, TTC, ACC, GGC. You will draw a total of 7 trees, one after each insertion
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
AVL Group assignment POST ANSWER IN JAVA PLS Populate a tree via a text file (input.txt)...
AVL Group assignment POST ANSWER IN JAVA PLS Populate a tree via a text file (input.txt) Make sure that after every insert, the tree is balanced. At the end, display the tree in level format. Make sure to include the height and the balance factor of every node in your output. Redirect the display to an output file (output.txt) Hint: //I will not accept any other algorithm //This is not a recursive algorithm node * rebalance(node *node){     node->height =...
Regression Trees​ Explain how classification trees works. Given a classification tree, state the classification rule for...
Regression Trees​ Explain how classification trees works. Given a classification tree, state the classification rule for a particular leaf. List the two measures of impurities that were covered in class. Why do we prune trees? What are the advantages of single classification trees? The weaknesses?
Express the degree of a vertex in terms of the adjacency matrix Describe how you can...
Express the degree of a vertex in terms of the adjacency matrix Describe how you can find out whether a graph is connected, based on its adjacency matrix, using only matrix addition and multiplication.
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a...
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a value k. The input matrix A[1...n, 1...n] has all rows and columns arranged in an non-descending order A[i, j] < A[i, j+1], A[j, i] < A[j + 1, i] for all 1 < i < n and 1 < j < n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT