讲解: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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容

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