告诉你的问题,你在这里展示给我们的代码不是你的,而是别人的代码,而你只是想找一些东西作为你的作业。 但这不像是在这里工作。只给你一段代码,我们不会帮你。如果你可以做你的功课,但只是遇到其中一个问题的特殊问题,那么你可以在这里发帖,并且很可能得到帮助,了解你需要的主题。但是,如果你根本不知道如何解决你的家庭作业,我认为你在这个过程中浪费你的时间,至少在这一刻。重新开始,尝试了解基础知识。从那里开始,处理更复杂的事情。在程序中,没有办法只是一点点理解事物。要么你理解与否 - 如果没有,你永远不会成为一名软件开发人员。 对不起,但在这个阶段没有人可以帮助你,除了你自己 hello my friendsi have a problem about implementation Trie with C++the project which is included,must have the following1. Ability to create a hollow tree2. Ability to insert a new string tree3. Ability to search for a specific string in a tree4. The ability to find all the strings that begin with a particular substring5. Find the following string that contains a specific string.6. The ability to find all strings that end in a particular substring.7. List all fields in the tree8. View tree9. Exit the programnow when I run 2 options, with a word that contains the string, number printing letters and numbers that come from unknownnext question : What options using option 4, 5 and 6 do. Please indicate in practiceand how can i view tree??please Help me,I need this code.tanks#include <stdio.h>#include <stdlib.h>#include <string.h>#include<malloc.h>#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])// Alphabet size (# of symbols)#define ALPHABET_SIZE (26)// Converts key current character into index//tabdile kelide charachtere jari be index// use only 'a' through 'z' and lower case // faghat az a ta z va horofe kochak estefade mikonad#define CHAR_TO_INDEX(c) ((int)c - (int)'a')static int test;// trie nodetypedef struct trie_node trie_node_t;struct trie_node{ int value; trie_node_t *children[ALPHABET_SIZE];};// trie ADTtypedef struct trie trie_t;struct trie{ trie_node_t *root; int count;};// Returns new trie node (initialized to NULLs)//gereye derakhte jadid ra bar migardanad ( meghdar dehie avalie ba Null)trie_node_t *getNode(void){ trie_node_t *pNode = NULL; pNode = (trie_node_t *)malloc(sizeof(trie_node_t));//meghdare hafeze dar tire_node_t ra bar hasbe byte ba estefade az malloc be barname takhsis midahad,va noe in eshare gar az node node_trie_t mibashad//1 Argoman (meghdar fazaye morede niaz) ra bar migardanad ((meghdar fazaye moshakhasi ra be barname bar migardanad)) //size of => tole motaghayer va ya noe motaghayer ra bar hasbe byte mitavan be dast avard. if(!pNode)//hich hafezei be pnode takhsis dade nashode { printf("\n\n takhsise faza anjam nashod !! "); return NULL; } if( pNode ) { int i; pNode->value = 0; for(i = 0; i < ALPHABET_SIZE; i++) { pNode->children[i] = NULL; } } return pNode;}// Initializes trie (root is dummy node)void initialize(trie_t *pTrie) // methode meghdar dehie avalie az noe void ba 1 meghdar az noe eshare gar be name pTrie.{ pTrie->root = getNode(); pTrie->count = 0;}// If not present, inserts key into trie// If the key is prefix of trie node, just marks leaf nodevoid insert(trie_t *pTrie, char key[]){ int level; int length = strlen(key);//key= arayei az charechter ha, tole key ra dar lengh mirizim int index; trie_node_t *pCrawl; pTrie->count++; pCrawl = pTrie->root; for( level = 0; level < length; level++ ) { index = CHAR_TO_INDEX(key[level]); if( !pCrawl->children[index] ) { pCrawl->children[index] = getNode(); } pCrawl = pCrawl->children[index]; } // mark last node as leaf//alamat gozarie akharie gereh be onvane barg pCrawl->value = pTrie->count;}// Returns non zero, if key presents in trieint search(trie_t *pTrie, char key[]){ int level,i; int length = strlen(key); int index; trie_node_t *pCrawl; pCrawl = pTrie->root; for( level = 0; level < length; level++ ) { index = CHAR_TO_INDEX(key[level]); if( !pCrawl->children[index] ) { return 0; } pCrawl = pCrawl->children[index]; } return (0 != pCrawl && pCrawl->value);}//after having the prefix traverse the sub tree of trie to fetch and print the suggestionsvoid traverse(trie_node_t *pCrawl, char key[],int index){ int i; char *temp; if(!pCrawl) { return ; } if(pCrawl->value!=0&&pCrawl!=NULL) { printf("%s\n",key);} for(i=0;i<26;i++) { temp=(char *)malloc(sizeof(char)*strlen(key)+2); strcpy(temp,key); temp[strlen(key)]=(char)(97+i); temp[strlen(key)+1]='&#92&#48'; if(pCrawl->children[i]!=NULL) traverse(pCrawl->children[i], temp,i); free(temp);}}//travel down the trie upto the prefix and then look for suggestion using traverse function// harkat in derakht ta pishvand va sepas peyda kardane pishnehad ba estefade az tabe'e traverseint suggestions(trie_t *pTrie, char key[]) //{ int level,i; int length = strlen(key); int index; trie_node_t *pCrawl; pCrawl = pTrie->root; if(strcmp(key,"")==0) { printf("The input is NULL!!"); return 0; } for( level = 0; level < length; level++ ) { index = CHAR_TO_INDEX(key[level]); if( !pCrawl->children[index] ) { printf("Prefix is not present"); return 0; } pCrawl = pCrawl->children[index]; }traverse(pCrawl, key,index);}//checks whether the string contains lowercase only/*int check(char key[]){int i,status;i=0;status=-1;while(key[i]!='&#92&#48') { if(!(key[i]<='z'&&key[i]>='a')) { status=0; return status; } i++; }status =1;return 1;}*/// Driverint main(){ int i,status,prompt; char inputString[30]; prompt=1; trie_t trie; initialize(&trie);while(prompt!='4'){ printf("\nPress 1 to insert a string into the database\nPress 2 to look for suggestions\nPress 3 to search a string\nPress 4 to exit\n"); scanf("%d",&prompt); switch(prompt) { case 1 : printf("\nPlease enter the string\n"); scanf("%s",inputString); if(strcmp(inputString,"")==0) { printf("Empty strings are not allowed"); break; } /* if(check(inputString)==0) { printf("\nSorry Only lower case alphabets are allowed !!\n"); break; }*/ insert(&trie, inputString); strcpy(inputString,""); break; case 2 : printf("\nPlease enter the pattern\n"); scanf("%s",inputString); if(strcmp(inputString,"")==0) { printf("Empty strings are not allowed"); break; } /* if(check(inputString)==0) { printf("\nSorry Only lower case alphabets are allowed !!\n"); break; }*/ printf("\nThe patterns are ::\n"); suggestions(&trie,inputString); // strcpy(inputString,""); break; case 3 : printf("\nPlease enter the string to be searched\n"); scanf("%s",inputString); if(strcmp(inputString,"")==0) { printf("Empty strings are not allowed"); break; } /* if(check(inputString)==0) { printf("\nSorry Only lower case alphabets are allowed !!\n"); break; }*/ status=search(&trie,inputString); strcpy(inputString,""); if(status==0) printf("\nThe string you are looking is not present in the databse\n"); else printf("The string is present in the database"); break; case 4 : return 0; default: printf("Invalid option!!Please try again!!!!\n\n"); break; }} return 0;} 解决方案 This is not a very well posed question. First of all, you need to explain how is the code you show related to your question? What did you try to achieve, how, what did you try, what did you expect and what did you get instead, why do you feel it's wrong. Also, we cannot discuss "view tree" topic without knowing what is your platform, what UI or graphics library you are using, what's required for the view, etc.The quick look at your code suggests that you should better start over. First of all, you should not put and I/O in the classes like tree, no printf, nothing like that. You need to create a distinct Tree and TreeNode classes (note that your tag says you are writing in C++, not!), and all the methods should be either class instance or static methods, and those classes should be totally abstracted from I/O. For example, you can have a method for adding a node, but the input of the note string data should be done in some test/demo/debug class or function.Using those "case 1", "case 2", "case 4" is unacceptable. They don't tell anything to the one reading your code. At least, use some enumeration type. Anyway, it should not be in your tree-related classes. Also, avoid all those immediate constants, except most fundamental ones, like 0, 1, null, etc. All those 26, '\0', 97, hard-coded strings — are very bad, kills all the maintainability of your code. At least declare the explicit const objects. Don't show all that commented-out code, respect the reader just a bit…—SATelling from your questions, the code you are showing us here is not yours, but somebody else's and you are just looking for something to turn in as your homework.But that is not like things work here. We would not do you a favor by just giving you a piece of code. If you can do your homework, but just have a particular problem with one of the points, then you can post here and most likely get help understanding the subject you need. If, however, you have no clue at all how to solve your homework, I think you are wasting your time in that course, at least at this moment. Start all over again and try to get an understanding of the basics. From there, work on to more complex things. In program there is no way of understanding things just "a little". Either you understand it or not - and if not, you will never succeed in being a software developer.Sorry, but at this stage nobody can help you, except yourself. 这篇关于使用c ++实现实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 06:40
查看更多