0

我最近做了一个 26array 并试图模拟一个字典。

我似乎无法弄清楚如何制作这个。我尝试过传入整数的链表而不是字符串。我当前的代码创建了 26 个节点(az),然后每个节点都有 26 个节点(az)。我想用整数实现一种方法,比如(1-26)。这些 int 节点将表示项目,而我想要传入的 int 链表将包含一组我想要在树中表示的 int,类似于字符串。

示例:传入集合 {1, 6 , 8},而不是诸如“hello”之类的字符串

   #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;

}
4

2 回答 2

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;
}

啊! 我的眼睛!

说真的,你正在尝试一些太高级的东西。您的代码充满了错误,无法按预期工作。修修补补将无济于事,您必须回到指针和链表的基础知识。学习基础知识,不要尝试链接列表之类的东西,直到您了解上面的代码有什么问题。

我会给你一些提示:“内存泄漏”、“悬空指针”、“类型不匹配”、“未定义的行为”。

于 2012-12-12T17:11:59.093 回答
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;

}
于 2012-12-13T00:51:30.603 回答