최근에 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;
}
나는 당신이하려는 일을 당신이 이해하지 못합니다. 질문은 대답하기가 어렵습니다. 유혹은 여기에서 코드로 작업하기보다는 솔루션을 구현하는 더 나은 방법을 논의하는 것이기 때문입니다. 솔루션에 대해이 특정 양식을 설정 한 경우 코드에 대해 특정 질문이있을 때까지 계속해서 퍼팅하는 것이 좋습니다. – Richard
http://s14.beta.photobucket.com/user/nousefouraname/media/trie.png.html – user1898442
이것은 구조를 만들려는 이미지입니다. 기본적으로 각 노드에는 카운터가 있습니다. 노드의 최상위 계층은 각 단일 int가 데이터 세트에 몇 번 있는지 추적합니다. 다음 레이어는 가능한 모든 2 아이템 세트를 나타내고, 세 번째 레이어는 가능한 모든 3 아이템 세트를 나타냅니다. 노드 옆의 파란색 숫자는 데이터 세트를 입력 한 후 카운터를 나타냅니다. – user1898442