h文件
#pragma once #include <iostream> #include <vector> using namespace std; class TrieTreeNode { public: TrieTreeNode(); ~TrieTreeNode(); void Clear(); void Clear_Recu(); bool Insert(char* pChar); TrieTreeNode* operator[](char* pChar); string GetStr(); char GetChar(){return m_Char;} void SetChar(char nCh){m_Char = nCh;} bool IsWord(){return m_bWord;} void SetWord(bool bWord){m_bWord = bWord;} private: char m_Char; vector<TrieTreeNode*> m_pVect; bool m_bWord; int m_nCount; TrieTreeNode* m_pParent; }; class TrieTree { public: TrieTree(void); ~TrieTree(void); void Clear(); void Insert(char* pChar); void Release(char* pChar); TrieTreeNode* GetStrEnd(char* pChar); bool GetFrontWord(int nNum,vector<string>& vecstr); private: TrieTreeNode m_rHead; }; class TrieNodeManager { public: TrieNodeManager(){m_vecList.clear();} ~TrieNodeManager(){} TrieTreeNode* GetNode(); void Clear(); static TrieNodeManager& GetSingle() { return m_Single; } private: static TrieNodeManager m_Single; vector<TrieTreeNode*> m_vecList; };
cpp文件
#include "TrieTree.h" ///////////////////////////////////////////////////////////////////////////////// TrieNodeManager TrieNodeManager::m_Single; //////////////////////////////////////////////////////////////////////////////// TrieTreeNode::TrieTreeNode():m_Char(0),m_pVect(),m_bWord(false),m_nCount(0),m_pParent(NULL) { } TrieTreeNode::~TrieTreeNode() { } void TrieTreeNode::Clear() { m_Char = 0; m_pVect.clear(); m_bWord = false; m_nCount = 0; m_pParent = NULL; } void TrieTreeNode::Clear_Recu() { vector<TrieTreeNode*>::iterator itr = m_pVect.begin(); while(itr != m_pVect.end()) { (*itr)->Clear_Recu(); itr ++; } Clear(); } bool TrieTreeNode::Insert(char* pChar) { if(pChar == NULL || strlen(pChar) <= 0) { return false; } if(m_Char == 0) { m_Char = pChar[0]; } bool bOK = false; if(strlen(pChar) <= 1) { m_bWord = true; m_nCount ++; bOK = true; } else { if(m_Char == pChar[0] || m_Char == (char)0xFF) { vector<TrieTreeNode*>::iterator itr = m_pVect.begin(); while(itr != m_pVect.end()) { bOK = (*itr)->Insert(m_Char == (char)0xFF?pChar:pChar + 1); if(bOK) { break; } itr ++; } if(bOK == false) { TrieTreeNode* newNode = TrieNodeManager::GetSingle().GetNode(); newNode->Clear(); newNode->Insert(m_Char == (char)0xFF?pChar:pChar + 1); newNode->m_pParent = this; m_pVect.push_back(newNode); bOK = true; } } } return bOK; } TrieTreeNode* TrieTreeNode::operator[](char* pChar) { if(pChar == NULL) { return NULL; } cout<<m_Char<<endl; if(strlen(pChar) <= 0) { return this; } vector<TrieTreeNode*>::iterator itr = m_pVect.begin(); while(itr != m_pVect.end()) { TrieTreeNode& rNode = *(*itr); if(rNode.GetChar() == *pChar) { return rNode[pChar + 1]; } itr ++; } return NULL; } string TrieTreeNode::GetStr() { string str = ""; if(m_pParent) { str += m_Char; str += m_pParent->GetStr(); } return str; } //////////////////////////////////////////////////////////////////////////////// TrieTree::TrieTree(void) { m_rHead.Clear(); m_rHead.SetChar(0xFF); } TrieTree::~TrieTree(void) { TrieNodeManager::GetSingle().Clear(); } void TrieTree::Clear() { m_rHead.Clear_Recu(); m_rHead.SetChar(0xFF); } void TrieTree::Insert(char* pChar) { if(pChar == NULL || strlen(pChar) <= 0) { return; } m_rHead.Insert(pChar); } void TrieTree::Release(char* pChar) { Insert(pChar); TrieTreeNode* pNode = GetStrEnd(pChar); if(pNode) { pNode->SetWord(false); string str = pNode->GetStr(); string tmp = str; str = ""; for(string::reverse_iterator itr = tmp.rbegin();itr < tmp.rend();itr ++) { str += *itr; } cout<<str.c_str()<<endl; } } TrieTreeNode* TrieTree::GetStrEnd(char* pChar) { return m_rHead[pChar]; } bool TrieTree::GetFrontWord(int nNum,vector<string>& vecstr) { return false; } /////////////////////////////////////////////////////////////////////////////////////// TrieTreeNode* TrieNodeManager::GetNode() { TrieTreeNode* newNode = new TrieTreeNode(); m_vecList.push_back(newNode); return newNode; } void TrieNodeManager::Clear() { for(int i=0;i<m_vecList.size();i++) { if(m_vecList[i]) { delete m_vecList[i]; } } m_vecList.clear(); }
main
#include "TrieTree.h" int main() { TrieTree rTree; rTree.Insert("fuck,ou"); rTree.Insert("black"); rTree.Release("black"); return 0; }