2012-12-12 2 views
0

최근에 26array를 만들고 사전을 시뮬레이트하려고했습니다.int의 연결된 목록으로 n 배열 만들기

나는 이것을 만드는 방법을 생각할 수 없습니다. 문자열 대신 int의 연결된 목록을 전달하는 작업을 시도했습니다. 현재 코드는 26 개의 노드 (a-z)를 만들고 그 각각의 노드는 26 개의 노드 (a-z)를 갖습니다. int를 사용하여이를 수행하는 방법을 구현하려고합니다 (1-26). 이 int 노드는 아이템을 표현할 것이고, 전달하고자하는 int의 linkedlist는 문자열과 비슷한 트리에 표현되기를 원하는 int 세트를 포함하게 될 것입니다.

예 대신 문자열 등

#include <iostream> 
using namespace std; 

class N26 
{ 
    private: 
     struct N26Node 
     { 
      bool isEnd; 
      struct N26Node *children[26]; 
     }*head; 

    public: 
     N26(); 
     ~N26(); 

     void insert(string word); 
     bool isExists(string word); 
     void printPath(char searchKey); 
}; 
N26::N26() 
{ 
    head = new N26Node(); 
    head->isEnd = false; 
} 
N26::~N26() 
{ 
} 

void N26::insert(string word) 
{ 
    N26Node *current = head; 
    for(int i = 0; i < word.length(); i++) 
    { 
     int letter = (int)word[i] - (int)'a'; 

     if(current->children[letter] == NULL) 
     { 
      current->children[letter] = new N26Node(); 
     } 
     current = current->children[letter]; 
    } 
    current->isEnd = true; 

} 

/*  Pre: A search key 
*  Post: True is the search key is found in the tree, otherwise false 
* Purpose: To determine if a give data exists in the tree or not 
******************************************************************************/ 

bool N26::isExists(string word) 
{ 
    N26Node *current = head; 
    for(int i=0; i<word.length(); i++) 
    { 
     if(current->children[((int)word[i]-(int)'a')] == NULL) 
     { 
      return false; 
     } 
     current = current->children[((int)word[i]-(int)'a')]; 
    } 
    return current->isEnd; 

} 
+1

나는 당신이하려는 일을 당신이 이해하지 못합니다. 질문은 대답하기가 어렵습니다. 유혹은 여기에서 코드로 작업하기보다는 솔루션을 구현하는 더 나은 방법을 논의하는 것이기 때문입니다. 솔루션에 대해이 특정 양식을 설정 한 경우 코드에 대해 특정 질문이있을 때까지 계속해서 퍼팅하는 것이 좋습니다. – Richard

+0

http://s14.beta.photobucket.com/user/nousefouraname/media/trie.png.html – user1898442

+0

이것은 구조를 만들려는 이미지입니다. 기본적으로 각 노드에는 카운터가 있습니다. 노드의 최상위 계층은 각 단일 int가 데이터 세트에 몇 번 있는지 추적합니다. 다음 레이어는 가능한 모든 2 아이템 세트를 나타내고, 세 번째 레이어는 가능한 모든 3 아이템 세트를 나타냅니다. 노드 옆의 파란색 숫자는 데이터 세트를 입력 한 후 카운터를 나타냅니다. – user1898442

답변

1
class N26 
{ 
    private: 
    N26Node newNode(void); 
    N26Node *mRootNode; 
    ...  
}; 

N26Node *newNode(void) 
{ 
    N26Node *mRootNode = new N26Node; 
    mRootNode = NULL; 
    mRootNode->mData = NULL; 

    for (int i = 0; i < 26; i++) 
    mRootNode->mAlphabet[i] = NULL; 
    return mRootNode; 
} 

아 "안녕하세요"와 같은 집합 {1, 6, 8} 전달! 내 눈!

진지하게, 당신은 너무 진보 된 것을 시도하고 있습니다. 귀하의 코드는 버그로 가득 차서 의도 한대로 작동하지 않습니다. 땜질은 도움이되지 않을 것이다. 포인터와 연결리스트의 기본으로 돌아 가야한다. 기초를 배우고 위의 코드가 잘못된 점을 이해할 때까지 연결된 목록의 연결된 목록 인과 같은 것을 시도하지 마십시오.

"메모리 누수", "매달려있는 포인터", "유형 불일치", "정의되지 않은 동작"을 알려 드리겠습니다.

+0

Oh geez wow, 잘못된 파일을 업로드했습니다. 다시 봐도 될까요? 아마도 이번에는 나쁘지 않을 것입니다. – user1898442

+0

@ user1898442 : 나쁘지는 않습니다. 작동합니까? 어떻게 실패합니까? – Beta

0

링크 된 목록을 사용하지 못했지만 배열을 사용하여 작업 할 수있었습니다.

/* *** Author:  Jamie Roland 
    * Class:  CSI 281 
    * Institute: Champlain College 
    * Last Update: October 31, 2012 
    * 
    * Description: 
    *  This class is to implement an n26 trie. The 
    * operations 
    * available for this impementation are: 
    * 
     * 1. insert 
    * 2. isEmpty 
     * 3. isExists 
    * 4. remove 
    * 5. showInOrder 
    * 6. showPreOrder 
    * 7. showPostOrder 
    * 
    * Certification of Authenticity: 
    *  I certify that this assignment is entirely my own work. 
    **********************************************************************/ 
#include <iostream> 
using namespace std; 

class N26 
{ 
    private: 
     struct N26Node 
     { 
      bool isEnd; 
      struct N26Node *children[26]; 
     }*head; 

    public: 
     N26(); 
     ~N26(); 

     void insert(int word[]); 
     bool isExists(int word[]); 
     void printPath(char searchKey); 
}; 
N26::N26() 
{ 
    head = new N26Node(); 
    head->isEnd = false; 
} 
N26::~N26() 
{ 
} 

void N26::insert(int word[]) 
{ 
    int size = sizeof word/sizeof(int); 
    N26Node *current = head; 
    for(int i = 0; i < size; i++) 
    { 
     int letter = word[i] - 1; 

     if(current->children[letter] == NULL) 
     { 
      current->children[letter] = new N26Node(); 
     } 
     current = current->children[letter]; 
    } 
    current->isEnd = true; 

} 

/*  Pre: A search key 
*  Post: True is the search key is found in the tree, otherwise false 
* Purpose: To determine if a give data exists in the tree or not 
******************************************************************************/ 

bool N26::isExists(int word[]) 
{ 
    int size = sizeof word/sizeof(int); 
    N26Node *current = head; 
    for(int i=0; i<size; i++) 
    { 
     if(current->children[(word[i]-1)] == NULL) 
     { 
      return false; 
     } 
     current = current->children[(word[i]-1)]; 
    } 
    return current->isEnd; 

} 
관련 문제