2016-04-06 35 views
-1

Bu ödev üzerinde çalışırken bazı sorunlar yaşıyorum. Şu anda bunun üzerinde çalışmaya çalışıyorum. Değerleri saklayabilir ve yazdırabilirim, ancak değerleri bastığımda, sadece girdiğim ilk değeri basar. Herhangi bir yardım Muhteşem olacak! Özyineleme konusunda pek bir şey bilmiyorum ve bu da beyni incitiyor.Özyineleme ikili ağaç baskı hatası

package lab6; 

import java.util.Scanner; 

public class node { 

    private int value; 
    static node root; 
    public node leftLink; 
    public node rightLink; 

    public node(int v) { 
     this.value = v; 
    } 

    public int getValue() { 
     return value; 
    } 

    static void traverseShow() { 
     if (root.leftLink != null) { 
      root = root.leftLink; 
      traverseShow(); 
     } 
     System.out.println(root.getValue()); 
     if (root.rightLink != null) { 
      root = root.rightLink; 
      traverseShow(); 

     } 

     return; 
    } 

    static void addNode(node n) { 
     if (root == null) { 
      root = n; 
     } else { 
      node tmp = root; // save the current root 
      if (root.getValue() > n.getValue()) { 
       root = root.leftLink; 
       addNode(n); 
      } else if (root.getValue() < n.getValue()) { 
       root = root.rightLink; 
       addNode(n); 
      } 
      root = tmp; // put the root back to its original value 
     } 
     return; 
    } 

    public static void main(String[] args) { 
     int val = 0; 
     Scanner sc = new Scanner(System.in); 
     boolean loop = true; 
     String command = ""; 

     while (loop == true) { 
      System.out.println("Please enter a command:"); 
      System.out.println("A = insert a new value"); 
      System.out.println("B = display all values"); 
      System.out.println("C = exit program"); 
      command = sc.next(); 
      if (command.equalsIgnoreCase("a")) { 
       System.out.println("Enter value: "); 
       val = sc.nextInt(); 
       node newNode = new node(val); 
       addNode(newNode); 
      } else if (command.equalsIgnoreCase("b")) { 
       traverseShow(); 
      } else if (command.equalsIgnoreCase("c")) { 
       sc.close(); 
       System.exit(0); 
      } else { 
       System.out.println("Invalid command! Please try again."); 
      } 
     } 
    } 
} 
+1

"Düğüm" sınıfınız bir "düğüm kökü" değişkenine sahip olmamalıdır. Ve kesinlikle statik olmamalı. Ayrıca, tam olarak neyin çalışmadığı net değil. –

cevap

1

Kodunuzu düzelttim ve iki sınıfa böldüm: Ana ve Düğüm. Şimdi test ettim ve çalışıyor. Ana hata, kökünü değiştiremezsiniz çünkü tüm ağaca erişmek için tek referansımız budur. Bunun yerine, çocuk Düğümüne sizin için addNode (node ​​n) 'ye söylemek isteyeceksiniz. Bu özyineleme gerçekleşir. Aynısı traverseShow() yönteminde geçerlidir. Aslında, hata ayıklama bu gibi durumlarda size çok yardımcı olacaktır.

public class Node { 

    private int value; 
    public Node leftLink; 
    public Node rightLink; 

    public Node() { 

    } 

    public Node(int v) { 
     this.value = v; 
    } 

    public int getValue() { 
     return value; 
    } 

    void addNode(Node n) { 

     //node tmp = root; // save the current root 
     if (getValue() > n.getValue()) { 
      if(leftLink == null){ 
       leftLink = n; 
      }else{ 
       leftLink.addNode(n); 
      } 
     } else if (getValue() < n.getValue()) { 
      if(rightLink == null){ 
       rightLink = n; 
      }else{ 
       rightLink.addNode(n); 
      } 
      //root = root.rightLink; 
      //addNode(n); 
     } 
     //root = tmp; // put the root back to its original value 

     return; 
    } 

    void traverseShow() { 
     if (leftLink != null) { 
      leftLink.traverseShow(); 
     } 
     System.out.println(getValue()); 
     if (rightLink != null) { 
      rightLink.traverseShow(); 
     } 

     return; 
    } 
} 

public class Main { 
    public static void main(String[] args) { 

     Node rootNode = null; 
     int val = 0; 
     Scanner sc = new Scanner(System.in); 
     boolean loop = true; 
     String command = ""; 

     while (loop == true) { 
      System.out.println("Please enter a command:"); 
      System.out.println("A = insert a new value"); 
      System.out.println("B = display all values"); 
      System.out.println("C = exit program"); 
      command = sc.next(); 
      if (command.equalsIgnoreCase("a")) { 
       System.out.println("Enter value: "); 
       val = sc.nextInt(); 
       Node newNode = new Node(val); 
       if(rootNode == null){ 
        rootNode = new Node(val); 
       }else{ 
        rootNode.addNode(newNode); 
       } 
      } else if (command.equalsIgnoreCase("b")) { 
       rootNode.traverseShow(); 
      } else if (command.equalsIgnoreCase("c")) { 
       sc.close(); 
       System.exit(0); 
      } else { 
       System.out.println("Invalid command! Please try again."); 
      } 
     } 
    } 
} 
+1

Düzeltmeler için çok teşekkür ederim! –