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 ⌋
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...
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 please You are given a matrix of characters representing a big box. Each cell...
In Java please You are given a matrix of characters representing a big box. Each cell of the matrix contains one of three characters: " / "which means that the cell is empty; '*', which means that the cell contains an obstacle; '+', which means that the cell contains a small box. You decide to rotate the big box clockwise to see how the small boxes will fall under the gravity. After rotating, each small box falls down until it...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT