자바 이진탐색트리에서 사전을 만드는데 에러가 생겨요 제발 도와주세요 ㅠㅠ
지나
제목그대로 입니다.. 고수님들 도와주세요 ㅠㅠ
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;
}
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700359 | 3.0 ) SimpleButton 상태 강제 변경 (3) | 희선 | 2025-07-04 |
2700304 | ie8 전용핵 문의 (3) | 여자 | 2025-07-03 |
2700281 | 이런경우 어떻게 코딩해야 표준에 따르는건가요? (6) | 늘솜 | 2025-07-03 |
2700230 | 질문이여 ! | 뿡뿡몬 | 2025-07-03 |
2700205 | 액션스크립트책 좀 추천해주세요. (10) | 화이트캣 | 2025-07-02 |
2700173 | 자바 소스인데 어떤게 에러인지..? (1) | 호빵녀 | 2025-07-02 |
2700142 | 하단이 붙어있는 가변 레이아웃구조 질문드립니다. | 이플 | 2025-07-02 |
2700089 | 이미지를 사다리꼴로 비틀게 하는 액션코드가 있나요? (4) | 여름 | 2025-07-01 |
2700033 | 배경에 그라데이션을 넣으려고 하는데요.. (4) | 화이티 | 2025-07-01 |
2700005 | [질문] TextField 객체의 실제 높이 알아오는 방법 ? | 천사의눈물 | 2025-07-01 |
2699978 | FileReferenceList를 이용하여 업로드시 자꾸 실행속도가 느리다는 팝업이... (10) | 데이비드 | 2025-06-30 |
2699944 | 자바스크립트가 많은 사이트는... (6) | 희나리 | 2025-06-30 |
2699918 | 브라우저마다 다른 input과 텍스트 정렬 (3) | 늘봄 | 2025-06-30 |
2699887 | 동적텍스트를 그래픽으로?? (2) | 족장 | 2025-06-30 |
2699862 | scope넣기 (1) | 아인 | 2025-06-29 |
2699835 | exe로 만드는 방법을....알려주세요.. (5) | 방방 | 2025-06-29 |
2699809 | 롤오버할때 백그라운드 이미지로할때 alt설명은 어떻게해야하죠?..ㅠ (4) | 반혈 | 2025-06-29 |
2699757 | 리스트뷰에 있는 내용을 랜덤으로 추출 | 세실 | 2025-06-28 |
2699725 | JMX 질문입니다. (1) | 시나브로 | 2025-06-28 |
2699695 | 으으아으ㅏ으ㅏ 정말미쳐버리겟네요 | 진달래 | 2025-06-28 |