2015-03-11 6 views
0

Я пытался выяснить, почему мой метод countingLeaves не может быть найден, когда я вызываю его из класса тестера.Couting оставляет в двоичном дереве

Мой компилятор дает мне ошибку TreeTester.java:25: error: не может найти символ countLeaves(); ^ символ: метод countLeaves() местоположение: Класс TreeTester класс

public class BinarySearchTree 
{ 
    private Node root; 


    public BinarySearchTree() 
    { 
     root = null; 
    } 


    public void add(Comparable obj) 
    { 
     Node newNode = new Node(); 
     newNode.data = obj; 
     newNode.left = null; 
     newNode.right = null; 
     if (root == null) { root = newNode; } 
     else { root.addNode(newNode); } 
    } 


    public boolean find(Comparable obj) 
    { 
     Node current = root; 
     while (current != null) 
     { 
     int d = current.data.compareTo(obj); 
     if (d == 0) { return true; } 
     else if (d > 0) { current = current.left; } 
     else { current = current.right; } 
     } 
     return false; 
    } 


    public void remove(Comparable obj) 
    { 


     Node toBeRemoved = root; 
     Node parent = null; 
     boolean found = false; 
     while (!found && toBeRemoved != null) 
     { 
     int d = toBeRemoved.data.compareTo(obj); 
     if (d == 0) { found = true; } 
     else 
     { 
      parent = toBeRemoved; 
      if (d > 0) { toBeRemoved = toBeRemoved.left; } 
      else { toBeRemoved = toBeRemoved.right; } 
     } 
     } 

     if (!found) { return; } 



     if (toBeRemoved.left == null || toBeRemoved.right == null) 
     { 
     Node newChild; 
     if (toBeRemoved.left == null) 
     { 
      newChild = toBeRemoved.right; 
     } 
     else 
     { 
      newChild = toBeRemoved.left; 
     } 

     if (parent == null) // Found in root 
     { 
      root = newChild; 
     } 
     else if (parent.left == toBeRemoved) 
     { 
      parent.left = newChild; 
     } 
     else 
     { 
      parent.right = newChild; 
     } 
     return; 
     } 



     Node smallestParent = toBeRemoved; 
     Node smallest = toBeRemoved.right; 
     while (smallest.left != null) 
     { 
     smallestParent = smallest; 
     smallest = smallest.left; 
     } 



     toBeRemoved.data = smallest.data; 
     if (smallestParent == toBeRemoved) 
     { 
     smallestParent.right = smallest.right; 
     } 
     else 
     { 
     smallestParent.left = smallest.right; 
     } 
    } 


    public void print() 
    { 
     print(root); 
     System.out.println(); 
    } 


    private static void print(Node parent) 
    { 
     if (parent == null) { return; } 
     print(parent.left); 
     System.out.print(parent.data + " "); 
     print(parent.right); 
    } 

    public int countLeaves(Node node) 
    { 

     if(node == null) 
     return 0; 
     else if(node.left == null && node.right == null) 
     { 
     return 1; 
     } 
     else 
     { 
     return countLeaves(node.left) + countLeaves(node.right); 
     } 
    } 


    class Node 
    { 
     public Comparable data; 
     public Node left; 
     public Node right; 


     public void addNode(Node newNode) 
     { 
     int comp = newNode.data.compareTo(data); 
     if (comp < 0) 
     { 
      if (left == null) { left = newNode; } 
      else { left.addNode(newNode); } 
     } 
     else if (comp > 0) 
     { 
      if (right == null) { right = newNode; } 
      else { right.addNode(newNode); } 
     } 
     } 
    } 
} 

Тестер используется

public class TreeTester 
{ 
    public static void main(String[] args) 
    { 
     BinarySearchTree t = new BinarySearchTree(); 
     t.add("D"); 
     t.add("B"); 
     t.add("A"); 
     t.add("C"); 
     t.add("F"); 
     t.add("E"); 
     t.add("I"); 
     t.add("G"); 
     t.add("H"); 
     t.add("J"); 
     t.remove("A"); // Removing leaf 
     t.remove("B"); // Removing element with one child 
     t.remove("F"); // Removing element with two children 
     t.remove("D"); // Removing root 
     t.print(); 
     System.out.println("Expected: C E G H I J"); 
     countLeaves(t); 
    } 
} 
+1

Компилятор ищет метод 'countLeaves()' в вашем классе 'TreeTester' ... но он определен в вашем классе' BinarySearchTree'. Поэтому вам нужно вызвать 't.countLeaves()' и передать узел в качестве аргумента. –

+0

Возможно, спросите об этом на http://codereview.stackexchange.com/ –

ответ

0

CountLeaves нуждается в Node, а не BinarySearchTree.

Вы можете добавить метод в BinarySearchTree, как это:

Node getRoot(){return root;} 

Amd использовать countLeaves(t.getRoot()).

+0

Er, t.countLeaves вместо этого. – Turtle

+0

Я использовал t.countLeaves (getRoot()); и он по-прежнему ищет в моем классе теретестера. Я считаю, что не понимаю концепцию. – sippycup

+0

@sippycup 't.countLeaves (t.getRoot())' – Turtle

Смежные вопросы