讲解:Java CompactPrefixTreesubtasksJava

Introduction只改了CompactPrefixTree.java中的内容,将代码替换一下就可以了,test是单元测试输出结果,正确的。RequirementCS545-­‐01/CS245-­‐02 Project 2: subtasksHow you approach this project is up to you. What I recommend below is just one ofmany possible ways to work on the project, you do not have to follow thesesuggestions.Note: you may not modify signatures of methods in the starter code, but you mayadd additional / helper methods in class CompactPrefixTree.Suggested steps:1) Go over the project description and your notes from the class.2) Look at the compact prefix tree example given in the description (that has thefollowing words: ape, apple, cart, cat, cats, demon, demons, dog) and think aboutthe following:• How would you print all words stored in this tree?• How you can check whether a particular word is in the tree? For instance, howwould you check if “apple” is in the tree? How would you check whether “app” isin the tree? (it is not)• How would you check to see if a given prefix is a prefix of some word in thedictionary? For instance, how would you check if “c” is a prefix of some word?How about “do” or “al”?3) Click on the given link to get the starter code, and carefully look at the startercode. Don’t hesitate to go the instructor or TAs for help.4) Start coding by writing several helper methods in this class (without them,writing and/or testing other methods will be difficult).• Write a method (let’s say, we call it getIndexOfCharacter ) that takes acharacter and returns its index in the array of node’s children. Array has 26 cells,each corresponds to one character: index 0 to ‘a’, index 1 to ‘b’, index 2 to ‘c’ andso on. See the project description on how to compute this index. Test thismethod separately; you will need it for many methods.• Write a helper method findLongestCommonPrefix that computes the longestcommon prefix of two strings. Test it thoroughly.• Add a temporary method in class CompactPrefixTree where you construct a verysimple prefix tree manually (so that you could write and test methods print,printTree, check, checkPrefix before you get the add method to work).To give you an idea of how this method should look like, here is the code thatadds two children “ap” and “ca” to the root of the tree.Node child1 = new Node();child1.prefix = “ap”;root.children[getIndexOfCharacter(‘a’)] = child1;Node child2 = new Node();child2.prefix = “ca”;root.children[getIndexOfCharacter(‘c’)] = child2;You can add children to child1 and child2 using the same approach. Irecommend manually building the example from the description this way, sothat you can debug your methods on it.Note: this method is for debugging purposes only until you write (and test) theadd method. You can remove it from your code once you write the add method.5) Fill in code in the print method: private void print(String s, Node node)that prints all words stored in the tree. It should be recursive:-­‐ if this node’s valid bit is true , you should print sJava 代写CompactPrefixTreesubtasks代写留学生Java课程设计+node.prefix (sinceparameter s is the string obtained by concatenating all the prefixes on thepath from the root to this node)-­‐ iterate over all the children of this node, and call this method recursively oneach child (think of what string to pass as a parameter!)6) Fill in code in the printTree method that prints all nodes in the compact prefixtree, using indentations to show the hierarchy (see the subsection of the projectdescription called “Printing the structure of the tree”. Also see my lectures slideswhere I talked about printing a human readable tree). The method should berecursive.7) Create a CompactPrefixTree in the Driver and add data to it by calling the methodyou wrote earlier that adds nodes manually. Call methods print and printTree onthis tree and see if the result looks correct. Commit/push this version of the code togithub.8) Now write methods check and checkPrefix. Examples you did in 2) should helpwith that. Thoroughly test them on the tree you constructed manually.Commit/push this version of the code to github. Note: these methods should beresursive.9) Do more examples on paper on how to add a new word into the tree. Think of itrecursively, and identify all the different cases you need to consider when you inserta new word.10) Start writing the code of “add”. The method should be recursive. This method isthe most interesting one of all the methods in this project, and might take you thelongest to write (the code is not long, but the algorithm is interesting).If you are confused about why this method returns a Node, look at the lecture onbinary search trees + look at the code of insert (for binary search trees).ü Write code for simple cases:-­‐ When a new word is inserted into a null tree;-­‐ When a new word equals the prefix stored at the node-­‐ When a new word starts with the prefix stored at the rootü Test this method on these simple cases. Commit/push this version of thecode to github.ü Move onto a more complex case, when you need to find the longestcommon prefix. Example: when the new word is “armor” and the prefixof the node is “ap”. In this case, you need to find the longest commonprefix between “armor” and “ap” and update the tree accordingly (seeproject description for details).ü Test the add method thoroughly on many examples. Only when it works,move onto suggest. Commit/push this version of the code to github.11) Work on suggest and test it. Commit/push this version of the code to github.12) Write other methods as needed (the constructor that takes a filename, the printmethod that prints to a file etc.). Commit/push the code to github.13) Run the basic tests provided by the instructor; I recommend adding your owntests. Commit/push the code to github.14) Clean up your code, test everything again, write a readme that explains howyour suggest method works, and submit the code to github. Go to your private repoon github and make sure your most recent java code is there.15) If there is time, try to make your suggest method better. Add the description ofthis project to you resume!& 转自:http://ass.3daixie.com/2018052610005901.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • By clicking to agree to this Schedule 2, which is hereby ...
    qaz0622阅读 1,484评论 0 2
  • 再进一步,再坚强一点,那就是成功!——题记 沿着一条崎岖的小路向前漆黑的摸索前进,黑暗中,一切是那么迷茫,前方的路...
    泳涛阅读 170评论 0 0
  • 说起近期最残酷最现实的电影,《妈阁是座城》绝对要算一部。这部电影改编自严歌苓的同名小说,以一个女性的视角,讲述了三...
    A忆思特教育赵敏阅读 288评论 0 0
  • 不会用色 不会深入过渡 不够自然 简直辣眼睛了! 今天 我尽洪荒之力给大家 写 !教! 程! ▽ 宝宝们 不要太爱...
    绘画干货分享阅读 872评论 2 19
  • 丰衣足食 居者有其屋 行万里路 衣食住行 人的基本需要 农民 司机 纺织工人 建筑工人 他们的劳动 有无可争议的价...
    金色麦浪_4aee阅读 272评论 1 3