0

InOrderIterator 통과 메서드로 작업합니다. 이 재귀 적으로 수행하는 방법을 이해하지만이 complier 오류가 계속 발생합니다.inOrderIterator 메서드를 BinaryTreeNode에 적용 할 수 없습니다. <T>

inOrderIterator() in LinkedBinarySearchTree<T> cannot be applied to (BinaryTreeNode<T>) 

왜이 메서드를 해당 개체에 적용 할 수 있는지 확실하지 않습니다. 어떤 아이디어? 당신의 메소드 선언에서 Heres는 내 방법 지금까지

public ArrayList<T> inOrderIterator() 
{ 
    ArrayList<T> myArr = new ArrayList<T>(); 
    BinaryTreeNode<T> currentNode = this.root; 
    if(currentNode != null) 
    { 
    inOrderIterator(currentNode.getLeftChild()); 
    myArr.add(currentNode.getElement()); 
    inOrderIterator(currentNode.getRightChild()); 
    } 

    return myArr; 
} 

LinkedBinarySearchTree.java

import jss2.exceptions.EmptyCollectionException; 
import jss2.exceptions.ElementNotFoundException; 
import java.util.ArrayList; 


public class LinkedBinarySearchTree<T extends Comparable<T>> 
{ 

private T elem; 
BinaryTreeNode<T> root; 

public LinkedBinarySearchTree (T element) 
{ 
    elem = element; 
    root = null; 
} 

public LinkedBinarySearchTree() 
{ 
    root = null; 
} 

public void addToTree (T element) 
{ 

    //Check if root is null 
    if(root == null) 
    { 
    root.getElement().equals(element); 
    } 
    else 
    { 
    addToTreeHelper(root, element); 
    } 
} 

public void addToTreeHelper(BinaryTreeNode<T> node, T target) 
{ 
    BinaryTreeNode<T> child; 
    BinaryTreeNode<T> targetNode = new BinaryTreeNode<T>(target); 

    if(target.compareTo(node.getElement()) == -1) 
    { 
    child = node.getLeftChild(); 

    if(child == null) 
    { 
     node.setLeftChild(targetNode); 
    } 

    else 
    { 
     addToTreeHelper(node.getLeftChild(), target); 
    } 
    } 

    else if(target.compareTo(node.getElement()) >= 0) 
    { 
    child = node.getRightChild(); 

    if(child == null) 
    { 
     node.setRightChild(targetNode); 
    } 

    else 
    { 
     addToTreeHelper(node.getRightChild(), target); 
    } 
    } 
} 


    //remove Element 

    public void removeElement(T target) throws Exception 
    { 
    BinaryTreeNode<T> node; 

    if(root.getElement() == null) 
    { 
    throw new EmptyCollectionException("tree is empty"); 
    } 

    else if(target.compareTo(root.getElement()) == 0) 
    { 
    root = getReplacement(root); 

    } 

    else 
    { 
    node = removeElemHelper(root, target); 
    if(node == null) 
    { 
     throw new ElementNotFoundException ("not found "+target.toString()); 
    } 
    } 
    } 

    //remove element helper 

public BinaryTreeNode<T> removeElemHelper(BinaryTreeNode<T> node, T target) 
{ 

    BinaryTreeNode<T> result, child, replacement; 
    result = null; 
    if(node != null) 
    { 
    if(target.compareTo(node.getElement()) == -1) 
    { 
     child = node.getLeftChild(); 
     if(child != null && target.compareTo(child.getElement()) == 0) 
     { 
      result = child; 
      replacement = getReplacement(child); 
      if(replacement == null) 
      { 
       node.setLeftChild(null); 
      } 
      else 
      { 
       node.setLeftChild(replacement); 
      } 
     } 

     else 
     { 
      result = removeElemHelper(child, target); 
     } 
    } 

    // 
    else if(target.compareTo(node.getElement()) == 1) 
    { 
     child = node.getRightChild(); 
     if(child != null && target.compareTo(child.getElement()) == 0) 
     { 
      result = child; 
      replacement = getReplacement(child); 
      if(replacement == null) 
      { 
       node.setRightChild(null); 
      } 
      else 
      { 
       node.setRightChild(replacement); 
      } 
     } 

     else 
     { 
      result = removeElemHelper(child, target); 
     } 
    } 

    } 

    return result; 
} 

//replacement 
public BinaryTreeNode<T> getReplacement(BinaryTreeNode<T> node) 
{ 
    BinaryTreeNode<T> result,leftChild, rightChild; 
    leftChild = node.getLeftChild(); 
    rightChild = node.getRightChild(); 

    if(node.getLeftChild() == null && node.getRightChild() == null) 
    { 
    result = null; 
    } 

    else if(node.getLeftChild() == null && node.getRightChild() != null) 
    { 
    result = node.getRightChild(); 
    } 

    else if(node.getLeftChild() != null && node.getRightChild() == null) 
    { 
    result = node.getLeftChild(); 
    } 

    else 
    { 
    result = findInorderSucessor(rightChild); 
    result.setLeftChild(leftChild); 
    result.setRightChild(rightChild); 
    } 

    return result; 

} 

//findInorderSucessor 
private BinaryTreeNode<T> findInorderSucessor(BinaryTreeNode<T> node) 
{ 
    BinaryTreeNode<T> child = node.getLeftChild(); 

    if(child == node) 
    { 
    return node; 
    } 
    else if(child.getLeftChild() == null) 
    { 
    child.setRightChild(node.getLeftChild()); 
    } 

    return findInorderSucessor(child); 
} 

public ArrayList<T> inOrderIterator() 
{ 
    ArrayList<T> myArr = new ArrayList<T>(); 
    BinaryTreeNode<T> currentNode = this.root; 
    if(currentNode != null) 
    { 
    inOrderIterator(currentNode.getLeftChild()); 
    myArr.add(currentNode.getElement()); 
    inOrderIterator(currentNode.getRightChild()); 
    } 

    return myArr; 
} 


} 

답변

3

봐 :

public ArrayList<T> inOrderIterator() 

그것은 매개 변수가 없습니다. 하지만 어떻게 호출하려고하는지보세요 :

inOrderIterator(currentNode.getRightChild()); 

... 인수를 지정하고 있습니다. 해당 전화에 적용 할 수있는 방법이 없습니다.

노드를 수락하는 개인 메서드 및 구축 할 사람 메서드 인 List<T>을 메서드에 오버로드하여 공개 메서드 호출을 수행하려는 것으로 의심됩니다. 예 :

public List<T> inOrderIterator() { 
    List<T> list = new ArrayList<T>(); 
    inOrderIterator(list, this.root); 
    return list; 
} 

private void inOrderIterator(List<T> list, BinaryTreeNode<T> current) { 
    if (current == null) { 
     return; 
    } 
    inOrderIterator(current.getLeftChild()); 
    list.add(current); 
    inOrderIterator(current.getRightChild()); 
} 
관련 문제