Error in converting text file with nodes into AVL to be used in graphical interface

Asked

Viewed 46 times

1

The idea of generating the graphical interface will be through a python file so I have to generate a text file in the following format:

4  [2] [6]
2 [1]  [3]
6  [5] [8] 
1  [] []
3  []  [] 
5  [] []                 
8  [7] [9] 
7 []  [] 
9 []  []

Well, with that we have to do the job savetofile. She’s making the following mistakes:

AvlTree.java:188: error: cannot find symbol
    if (ptraiz==0) return;
        ^
  symbol:   variable ptraiz
  location: class AvlTree
AvlTree.java:190: error: cannot find symbol
    LinkedList<nd> list = new LinkedList<nd>();
               ^
  symbol:   class nd
  location: class AvlTree
AvlTree.java:190: error: cannot find symbol
    LinkedList<nd> list = new LinkedList<nd>();
                                         ^
  symbol:   class nd
  location: class AvlTree
AvlTree.java:192: error: cannot find symbol
        fila.push(ptraiz);
                  ^
  symbol:   variable ptraiz
  location: class AvlTree
AvlTree.java:192: error: cannot find symbol
        fila.push(ptraiz);
        ^
  symbol:   variable fila
  location: class AvlTree
AvlTree.java:197: error: cannot find symbol
        while (!fila.empty()) {
                ^
  symbol:   variable fila
  location: class AvlTree
AvlTree.java:198: error: cannot find symbol
            nd q = fila.front();
            ^
  symbol:   class nd
  location: class AvlTree
AvlTree.java:198: error: cannot find symbol
            nd q = fila.front();
                   ^
  symbol:   variable fila
  location: class AvlTree
AvlTree.java:201: error: cannot find symbol
                fila.push(q.esq);
                ^
  symbol:   variable fila
  location: class AvlTree
AvlTree.java:206: error: cannot find symbol
                fila.push(q.dir);
                ^
  symbol:   variable fila
  location: class AvlTree
AvlTree.java:211: error: cannot find symbol
            fila.pop();
            ^
  symbol:   variable fila
  location: class AvlTree
11 errors

someone could help

 import java.io.*;
 import java.util.*;
 public class AvlTree {  
       private AvlNode root = null;  

       public AvlTree( ) {  
           root = null;  
       }  

       public void clear() {  
           root = null;  
       }  
       public boolean isEmpty() {  
           return root == null;  
       }  

       public AvlNode getRootNode (){  
           return root;  
       }  

       /** Retorna a altura da árvore */  
       private static int height( AvlNode t ) {  
           return t == null ? -1 : t.height;  
       }  

        /** 
        * Retorna o maior valor ente lhs e rhs. 
        */  
       private static int max( int lhs, int rhs ) {  
           return lhs > rhs ? lhs : rhs;  
       }  

       /** Retorna o fator de balanceamento da árvore com raiz t */   
       private int getFactor (AvlNode t) {  
           return height( t.left ) - height( t.right );  
       }  

       public boolean insert (int x) {  
           root = insert (x, root);  
           return true;  
       }  

       private AvlNode insert (int x, AvlNode t) {  
           if( t == null )  
               t = new AvlNode( x, null, null );  
           else if( x<t.key ) t.left = insert( x, t.left );  
           else if( x>t.key) t.right = insert( x, t.right );  
           t = balance (t);  
           return t;  
       }  

       public AvlNode balance (AvlNode t) {  
           if ( getFactor(t) == 2 ) {  
                   if (getFactor (t.left)>0) t = doRightRotation( t );  
                   else t = doDoubleRightRotation( t );  
           }  
           else if ( getFactor(t) == -2 ) {  
                   if ( getFactor(t.right)<0 ) t = doLeftRotation( t );  
                   else t = doDoubleLeftRotation( t );  
           }  
           t.height = max( height( t.left ), height( t.right ) ) + 1;  
           return t;  
       }  

       /** Faz Rotação simples a direita */  
       private static AvlNode doRightRotation( AvlNode k2 ) {  
           AvlNode k1 = k2.left;  
           k2.left = k1.right;  
           k1.right = k2;  
           k2.height = max( height( k2.left ), height( k2.right ) ) + 1;  
           k1.height = max( height( k1.left ), k2.height ) + 1;  
           return k1;  
       }  

       /** Rotação simples à esquerda */  
       private static AvlNode doLeftRotation( AvlNode k1 ) {  
           AvlNode k2 = k1.right;  
           k1.right = k2.left;  
           k2.left = k1;  
           k1.height = max( height( k1.left ), height( k1.right ) ) + 1;  
           k2.height = max( height( k2.right ), k1.height ) + 1;  
           return k2;  
       }  

       /** Rotação dupla à direita */  
       private static AvlNode doDoubleRightRotation( AvlNode k3 ) {  
           k3.left = doLeftRotation( k3.left );  
           return doRightRotation( k3 );  
      }  

       /** Rotação dupla à esquerda */  
       private static AvlNode doDoubleLeftRotation( AvlNode k1 ) {  
           k1.right = doRightRotation( k1.right );  
           return doLeftRotation( k1 );  
       }  

       public AvlNode search(int el) {  
           return search(root,el);  
       }  
       protected AvlNode search(AvlNode p, int el) {  
           while (p != null) {  
               /* se valor procuradp == chave do nó retorna referência ao nó */   
               if (el==p.key) return p;  
               /* se valor procurado < chave do nó, procurar na sub-árvore esquerda deste nó */  
               else if (el<p.key) p = p.left;  
               /* se valor procurado > chave do nó, procurar na sub-árvore direita deste nó */  
               else p = p.right;  
           }  
           // caso chave não foi achada, retorna null  
           return null;  
       }  

       public void inorder() {  
           inorder(root);  
       }  
       protected void inorder(AvlNode p) {  
           if (p != null) {  
                inorder(p.left);  
                System.out.print(p.key+" - ");  
                inorder(p.right);  
           }  
       }  

       public void preorder() {  
           preorder(root);  
       }  
       protected void preorder(AvlNode p) {  
           if (p != null) {  
                System.out.print(p.key + " ");  
                preorder(p.left);  
                preorder(p.right);  
           }  
       }  

       public void postorder() {  
           postorder(root);  
       }  

       protected void postorder(AvlNode p) {  
           if (p != null) {  
                postorder(p.left);  
                postorder(p.right);  
                System.out.print(p.key + " ");  
           }  
       }  

   protected AvlNode searchFather (int el) {  
       AvlNode p = root;  
       AvlNode prev = null;  
       while (p != null && !(p.key==el)) {  // acha o nó p com a chave el  
           prev = p;                             
           if (p.key<el)  
                 p = p.right;  
           else p = p.left;  
       }  
       if (p!=null && p.key==el) return prev;  
       return null;  
   }  

   /* método de autoria de Leonardo Zandoná - 2006/2 */  
   public void displayTree() {  
    if (isEmpty()){  
        System.out.println("Árvore vazia!");  
        return;  
    }             
    String separator = String.valueOf("  |__");  
    System.out.println(this.root.key+"("+root.height+")");  
    displaySubTree(root.left,  separator);  
    displaySubTree(root.right, separator);  
}  
private void displaySubTree(AvlNode node, String separator) {  

    if (node != null) {  

        AvlNode father = this.searchFather(node.key);  
        if (node.equals(father.left) == true) {  
            System.out.println(separator+node.key+"("+node.height+")"+" (ESQ)");  
        }else{  
            System.out.println(separator+node.key+"("+node.height+")"+" (DIR)");  
        }             
        displaySubTree(node.left,  "     "+separator);  
        displaySubTree(node.right, "     "+separator);  
    }  
}  

         public void savetofile(OutputStream fnome) {
    if (ptraiz==0) return;

    LinkedList<nd> list = new LinkedList<nd>();

        fila.push(ptraiz);
   //     ofstream out(fnome);
    //    fnome= new FileOutputStream("saida.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fnome);
        BufferedWriter bw = new BufferedWriter(osw);
        while (!fila.empty()) {
            nd q = fila.front();
              bw.write(q.chave +" ");
            if (q.esq != 0) {
                fila.push(q.esq);
                bw.write(" ["+q.esq.chave  +"] ");
            } else
            bw.write(" [] ");
            if (q.dir != 0) {
                fila.push(q.dir);
                bw.write(" ["+q.dir.chave+"] ");
            } else
                    bw.write(" [] ");
                    bw.newLine();
            fila.pop();
    }
    bw.close();

1 answer

2

The error message cannot find symbol means that you tried to access a variable that was not defined anywhere. Or a parameter, or a type. In your case, there are 3 names that the program failed to solve: nd, fila and ptraiz.

nd If I’m not mistaken, it’s a class, right? Aside from going against the Java convention to create classes started in lower case (it is harder to know that it is a class), your problem must be that you have defined the class nd in another file (probably nd.java) but forgot to import it in his AvlTree.java (which must be in a different package, by the way). If nd.java is in the package meu.pacote import it this way in the AvlTree.java:

import meu.pacote.nd;

If it is in any package (i.e. in the default package/default), then maybe it can’t be imported, and you’ll have to put it in a package.

As to fila and ptraiz, make sure that these variables are accessible to your method in AvlTree (passing them as argument pro method, or including them among the object instance variables).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.