数据结构与程序设计_第1页
数据结构与程序设计_第2页
数据结构与程序设计_第3页
数据结构与程序设计_第4页
数据结构与程序设计_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

数据结构与程序设计(29)教师:鲍钰ybao@4/21/20231.Chapter11MULTIWAYTREES4/21/20232.Tries:

LexicographicSearchTrees

P531DEFINITIONAtrieofordermiseitheremptyorconsistsofanorderedsequenceofexactlymtriesoforderm.4/21/20233.4/21/20234.C++TrieDeclarationsEveryRecordhasaKeythatisanalphanumericstring.Methodcharkey_letter(intposition)returnsthecharacterinthegivenpositionofthekeyorreturnsaNONE,ifthekeyhaslengthlessthanposition.Auxiliaryfunctionintalphabetic_order(charsymbol)returnsthealphabeticpositionofthecharactersymbol,or27fornonblank,nonalphabeticcharacters,or0forblank,NONEcharacters.4/21/20235.Trie--Key#include"String.h"#include"iostream.h"constintkey_size=10;classKey{ charstr[key_size];public: Key(chars[]); char*the_key()const; charkey_letter(intposition)const;};4/21/20236.Trie--Key#include"Key.h"Key::Key(chars[]){ for(inti=0;i<=strlen(s);i++) str[i]=s[i];}char*Key::the_key()const{ return(char*)str;}charKey::key_letter(intposition)const{ if(position<strlen(str))returnstr[position]; elsereturn'\0';}4/21/20237.Trie--Record#include"Key.h"classRecord{public: operatorKey();//implicitconversionfromRecordtoKey. Record(chars[]=""); char*the_key()const; charkey_letter(intposition)const;private: charstr[key_size];};ostream&operator<<(ostream&output,Record&x);4/21/20238.Trie--Record#include"Record.h"Record::Record(chars[]){ for(inti=0;i<=strlen(s);i++) str[i]=s[i];}Record::operatorKey(){ Keytmp(str);}4/21/20239.Trie--RecordcharRecord::key_letter(intposition)const{ if(position<strlen(str))returnstr[position]; elsereturn'\0';}char*Record::the_key()const{ return(char*)str;}ostream&operator<<(ostream&output,Record&x){ output<<x.the_key(); output<<""; returnoutput;}4/21/202310.Trie—Trie_node#include"Record.h"constintnum_chars=28;structTrie_node{//datamembers Record*data; Trie_node*branch[num_chars];//constructors Trie_node();};4/21/202311.Trie—Trie_node#include"Trie_node.h"Trie_node::Trie_node(){ data=NULL; for(inti=0;i<num_chars;i++) branch[i]=NULL;}4/21/202312.Trie#include"Trie_node.h"enumError_code{not_present,overflow,underflow,duplicate_error,success};classTrie{public://Addmethodprototypeshere. Error_codeinsert(constRecord&new_entry); Error_codetrie_search(constKey&target,Record&x)const; Trie();private://datamembers Trie_node*root;};intalphabetic_order(charc);4/21/202313.Trie#include"Trie.h"intalphabetic_order(charc)/*Post:Thefunctionreturnsthealphabeticpositionofcharacterc,oritreturns0ifthecharacterisblank.*/{ if(c==''||c=='\0')return0; if('a'<=c&&c<='z')returnc-'a'+1; if('A'<=c&&c<='Z')returnc-'A'+1; return27;}Trie::Trie(){ root=NULL;}4/21/202314.TrieError_codeTrie::insert(constRecord&new_entry)/*Post:IftheKeyofnew_entryisalreadyintheTrie,acodeofduplicate_errorisreturned.Otherwise,acodeofsuccessisreturnedandtheRecordnewentryisinsertedintotheTrie.Uses:MethodsofclassesRecordandTrie_node.*/{ Error_coderesult=success; if(root==NULL)root=newTrie_node;//CreateanewemptyTrie. intposition=0;//indexeslettersofnewentry charnext_char; Trie_node*location=root;//movesthroughtheTrie while(location!=NULL&& (next_char=new_entry.key_letter(position))!='\0'){ intnext_position=alphabetic_order(next_char); if(location->branch[next_position]==NULL) location->branch[next_position]=newTrie_node; location=location->branch[next_position]; position++; }//Atthispoint,wehavetestedforallnonblankcharactersofnewentry. if(location->data!=NULL)result=duplicate_error; elselocation->data=newRecord(new_entry); returnresult;}4/21/202315.TrieError_codeTrie::trie_search(constKey&target,Record&x)const/*Post:Ifthesearchissuccessful,acodeofsuccessisreturned,andtheoutputparameterxissetasacopyoftheTrie'srecordthatholdstarget.Otherwise,acodeofnot_presentisreturned.Uses:MethodsofclassKey.*/{ intposition=0; charnext_char; Trie_node*location=root; while(location!=NULL&& (next_char=target.key_letter(position))!='\0'){ //TerminatesearchforaNULLlocationorablankinthetarget. location=location->branch[alphabetic_order(next_char)]; //Movedowntheappropriatebranchofthetrie. position++; //Movetothenextcharacterofthetarget. } if(location!=NULL&&location->data!=NULL){ x=*(location->data); returnsuccess; } else returnnot_present;}4/21/202316.Main#include"Trie.h"voidmain(){ Triedict; dict.insert(Record("a")); dict.insert(Record("aa")); dict.insert(Record("ab")); dict.insert(Record("ac")); dict.insert(Record("aba")); dict.insert(Record("abc")); dict.insert(Record("abba")); dict.insert(Record("abaca"));

dict.insert(Record("baba")); dict.insert(Record("baa")); dict.insert(Record("bab")); dict.insert(Record("bac")); dict.insert(Record("ba")); dict.insert(Record("b"));4/21/202317.Main dict.insert(Record("caaba")); dict.insert(Record("ca")); dict.insert(Record("c")); dict.insert(Record("ca")); dict.insert(Record("cab")

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论