자바 이진탐색트리에서 사전을 만드는데 에러가 생겨요 제발 도와주세요 ㅠㅠ
지나
제목그대로 입니다.. 고수님들 도와주세요 ㅠㅠ
insertWord를테스트하려는데 에러가 생기네요.. 뭐가 문제인지 모르겠습니다!
너무 복잡할까봐 파일도 첨부해서 올립니다..
어떻게 해야 될까요??
제가잘 하고 있긴 한걸까요??
아, 참고로 문제는:
insertWord 메소드를 완성하라
insertWord는 key 와 meaning을 추가하는것으로 맞는 자리에 끼워 넣어야한다. (알파벳순)
트리의 왼쪽에 가는것은 키의 value 보다 작은것이고
트리의 오른쪽에 가는것은 키의 value 보다 큰것이다. (알파벳순)
잘 삽입됐다면 true를 리턴하고, 삽입하려는 단어가 존재하다면 false를 리턴하고 그대로 둔다.
key value 가 null 이나 이라면 IllegalArgumentException 을 throw 한다.
아래는 트리 코드입니다
public class BinaryTree implements Serializable
{
public static final int KEY_VALUE_NOT_FOUND = -1;
public TreeNode root;
public TreeNode findNode(String key) throws IllegalArgumentException
{
TreeNode position = root;
int compResult = key.compareTo(position.getKey());
try
{
//if root is null
if (this.root == null)
{
return null;
}
// if key is null
if (key == null)
{
throw new IllegalArgumentException(Key value is needed);
}
while(position != null)
{
//key == rootKey
if (compResult == 0)
return position;
//key rootKey
else if (compResult 0)
position.getLeft();
//key rootKey
else
position.getRight();
//if there is no key existed then return null
return null;
}
}catch (IllegalArgumentException argEx)
{
new IllegalArgumentException();
}
return null;
}
public boolean insertWord(String key, String meaning) throws IllegalArgumentException
{
try
{
// if key is null
if (key == null || meaning == null || key == || meaning == )
{
throw new IllegalArgumentException(Key value or meaning is null);
}
//if root is null add new element to the root.
if (this.root == null)
{
this.root.setKey(key);
this.root.setMeaning(meaning);
this.root.setLeft(null);
this.root.setRight(null);
return true;
}
TreeNode position = root;
int compResult = key.compareTo(position.getKey());
while(position != null)
{
//key = rootKey (already exist)
if (compResult == 0)
return false;
//key rootKey
else if (compResult 0)
{
position = position.getLeft();
if (position == null)
{
position.setKey(key);
position.setMeaning(meaning);
position.setLeft(null);
position.setRight(null);
return true;
}
//key rootKey
}else
{
position = position.getRight();
if (position == null)
&n {
position.setKey(key);
position.setMeaning(meaning);
position.setLeft(null);
position.setRight(null);
return true;
}
}
}
}catch (IllegalArgumentException argEx)
{
new IllegalArgumentException();
}
return false;
}
}
아래는 노드 코드입니다
public class TreeNode implements Serializable
{
public String key;
public String meaning;
public SynonymList synonyms;
public TreeNode parent;
public TreeNode left;
public TreeNode right;
public TreeNode()
{
synonyms = new SynonymList();
}
//set, get key
public void setKey(String key)
{
this.key = key;
}
public String getKey()
{
return key;
}
//set, get meaning
public void setMeaning(String meaning)
{
this.meaning = meaning;
}
public String getMeaning()
{
return meaning;
}
//set, get synonyms
public void setSynonyms(SynonymList synonyms)
{
this.synonyms = synonyms;
}
public SynonymList getSynonyms()
{
return synonyms;
}
//set, get parent
public void setParent(TreeNode parent)
{
this.parent = parent;
}
public TreeNode getParent()
{
return parent;
}
//set, get left
public void setLeft(TreeNode left)
{
this.left = left;
}
public TreeNode getLeft()
{
return left;
}
//set, get right
public void setRight(TreeNode right)
{
this.right = right;
}
public TreeNode getRight()
{
return right;
}
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2696043 | 제이쿼리 .scroll() 관련 질문드립니다 | 이거이름임 | 2025-05-26 |
2695984 | 마크업상으로 하단에 있으나 우선적으로 이미지파일을 다운로드받는 방법 (1) | 들꿈 | 2025-05-25 |
2695934 | tr 속성값 (9) | 새 | 2025-05-25 |
2695905 | ASP로 개발됐을 때 css가 달라져요 ㅠㅠ (4) | 슬아라 | 2025-05-24 |
2695878 | form을 이용한 다른 페이지로 넘기는 방법을 알려주세요 (1) | 핫파랑 | 2025-05-24 |
2695844 | 저기 암호화 및 복호화 프로그램.. 만들어볼려는대 (2) | 한빛 | 2025-05-24 |
2695814 | [질문] PDA에서 애플릿이 가능한가요? (1) | 봄시내 | 2025-05-24 |
2695785 | 웹 설정 도와줄분 | 화이트캣 | 2025-05-23 |
2695730 | 갑자기 기억이 안나는데 accesskey 속성.. | 빛나라 | 2025-05-23 |
2695702 | [질문] Java 버전 차이에 의한 오류?!! (2) | 검사 | 2025-05-23 |
2695672 | 자바 임베디드 쪽으로 배우고 싶은데요..질문이요.. (1) | 뽀그리 | 2025-05-22 |
2695647 | 헉! 이클립스(v3.1)에서 발생되는 널포인트 익셉션? ;;; (3) | 아빠몬 | 2025-05-22 |
2695586 | IFRAME 캐싱 질문 | 봄나비 | 2025-05-22 |
2695498 | [질문]실행가능한 jar파일.. 정말 이해가 안가네요... ㅡㅜ;; | 터1프한렩 | 2025-05-21 |
2695468 | 자바랑 이클립스에서요.. | 스킬 | 2025-05-21 |
2695375 | Mysql 연동하는 자바 질문있습니다. | 아리솔 | 2025-05-20 |
2695319 | 파워포인트 파일을 저장할 수 있을까요? | 시윤 | 2025-05-19 |
2695289 | [질문]Tween 값의 정도를 알고 싶습니다. | 타마 | 2025-05-19 |
2695238 | c 와 c++의 시작 (10) | ChocoHoilc | 2025-05-18 |
2695215 | 탑메뉴의 repeat-x .배경이 두가지에요ㅠ ㅠ | 널위해 | 2025-05-18 |