Java Help Recursion & Binary Trees -


i new java, , new recursion , binary trees. building program takes text document , stores in binary tree. need take string , find out how many times appears in text.

my problem(s) either while adding data and/or when searching data string.

i have decided store string , frequency in each node built. add methods follows:

public void add(string newword) {      //change word case make comparing easier     newword = newword.touppercase();       root = recursiveadd(root, newword); }  /**  * takes root , recurses until root null (base case)  * frequency incremented if data being added, or if  * exits. if data not present, method recurses  */ private node recursiveadd(node subtree, string newword) {      //base case: root null     //empty trees have node created, set, , incr freq     if (subtree == null) {           subtree = new node();         subtree.setstoredword(newword);         subtree.incrfreqcount();          return subtree;      }       int comparison = newword.compareto(subtree.getstoredword());      //for word in tree, increment frequency     if (comparison == 0) {          if(newword.equalsignorecase("translyvania"))         system.out.println("entered same word incrementation");          subtree.incrfreqcount();         return subtree;          //the root comes before new word,          //move on right child     } else if(comparison < 0) {          subtree.setlchild(recursiveadd(subtree.getlchild(), newword));      } else { //if(comparison > 0) {          subtree.setrchild(recursiveadd(subtree.getrchild(), newword));      }     return subtree; } 

i can't seem tell problem is. word searching, says occurs 16 times(what should get) , says 1 time. doesn't seem consistent @ , value changes seemingly no reason (though know there must one).

once tree built, take string searching for, , pass through these methods.

public void wordsearch(string lookforword){      lookforword = lookforword.touppercase();     wordsearchrecur(root, lookforword);  }    private boolean wordsearchrecur(node subtree, string lookforword){      //base case     // root same string     if(subtree == null){         system.out.println("the word \"" + lookforword + "\" not "                 + "found in text");         return false;     }      int comparison = lookforword.compareto(subtree.getstoredword());      if(comparison == 0){         system.out.println("the word \"" + lookforword + "\" found " +                  subtree.getfreqcount() + " times in text");         return true;           //alphabetically before, move left branch     } else if (comparison < 0){          system.out.println("move left");         return wordsearchrecur(subtree.getlchild(), lookforword);          //alphabetically after, move right branch     } else { // if(comparison > 0){         system.out.println("move right");         return wordsearchrecur(subtree.getrchild(), lookforword);     }     } 

i can't understand why reaching end of wordsearchrecur() method. shouldn't returning before ever gets point? output showing reaches there several times.

i know missing huge parts of these concepts, looking @ of previous posts isn't helping. must have spent 3 hours on looking answer on stack, not mention other websites.

please help!

edit: edited code include have changed of @joop eggen have frequency calculated correctly during recursiveadd(), during wordsearchrecur() frequency not seem follow node. when comparison == 0, freqcount still 1.

solved: after of @joop eggen further problems result of oversight. thank help.

you profit first reducing code simplest form.

public void add(string word) {      //change word case make comparing easier     word = word.touppercase();      root = recursiveadd(root, word); }  /**  * takes sub-tree , recurses until sub-tree null (base case)  * frequency incremented if data being added, or if  * exists. if data not present, method recurses  */ private node recursiveadd(node subtree, string word) {      // base case: subtree null     if (subtree == null) {         node node = new node();         node.setstoredword(word);         node.incrfreqcount();         return node;     }      int comparison = word.compareto(subtree.getstoredword());     if (comparison == 0) {         // data in tree, increment frequency         subtree.incrfreqcount();     } else if (comparison  < 0) {         subtree.setlchild(recursiveadd(subtree.getlchild(), word);     } else /*if (comparison > 0)*/ {         subtree.setrchild(recursiveadd(subtree.getrchild(), word);     }     return subtree; } 

as searching:

public void wordsearch(string lookedforword){        lookedforword = lookedforword.touppercase();     wordsearchrecur(root, lookedforword); }  private boolean wordsearchrecur(node subtree, string word){      if (subtree == null) {         system.out.println("the word \"" + word + "\" not "                 + "found in text");         return false;     }      int comparison = word.compareto(root.getstoredword();     if (comparison == 0){         system.out.println("the word \"" + word + "\" found " +                  subtree.getfreqcount() + " times in text");         return true;     } else if (comparison < 0) {         return wordsearchrecur(subtree.getlchild(), word);     } else /*if (comparison > 0)*/ {         wordsearchrecur(subtree.getrchild(), word);     }    } 

this helps keeping errors @ bay, there less check/go wrong.

as did touppercase in both cases, , in rewrite comparison done same (you had equals , turned around compareto parameters, should work.

in fact code shown looks quite alright. make recursive dumptree(node subtree, string indentation). , check every step.

possibly edited code here bit, , braces { } misplaced.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -