[sword-devel] Best way to work with Genbooks

Troy A. Griffitts scribe at crosswire.org
Tue Mar 5 14:05:39 EST 2024


Hi David,

Traversing a tree without recursion usually required keeping a stack you can push onto and pop when you hit a leaf.  This is all handled for you in the call stack when you use recursion.  If you really do want to avoid recursion, I would recommend having a look at general strategies for tree traversal without recursion. Something like:

https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/

With recursion, our TreeIndexUtil printTree function serves as a great example:

https://crosswire.org/svn/sword/trunk/utilities/treeidxutil.cpp

Hope this helps. Blessings in your work,

Troy




On 3/5/24 10:13, David "Judah's Shadow" Blue wrote:
> On Monday, March 4, 2024 4:32:43 PM EST David "Judah's Shadow" Blue wrote:
>> Ok, I think I have a handle on how to generate the TOC. However, I'm not
>> seeing a method in the API docs that would give me the text for a given TOC
>> entry
> Well I thought i had a handle on generating a TOC, but I'm only getting down
> one branch. I want to avoid recursion if I can, but I can't figure out how to
> get back up the tree to traverse down the next branch. If I have to use
> recursion I can, but I'd really like to avoid it. Here's my code so far.
> 
> std::string Genbook::getTOC() {
>      sword::TreeKey *treeKey;
>      sword::SWModule *mod;
>      std::string toc = "";
> 
>      mod = swordLibrary->getModule(book.c_str());
> 
>      //Assume we don't have a TreeKey yet since we're just starting.
>      treeKey = dynamic_cast<sword::TreeKey*>(mod->getKey());
> 
>      if(treeKey->firstChild()) {
>          while(treeKey->nextSibling()) {
>              toc += treeKey->getText();
>              toc += "\n";
>              if(treeKey->hasChildren()) {
>                  treeKey->increment();
>              }
>          }
>      }
> 
>     return toc;
> 
> }
> 
> 
> 
> _______________________________________________
> sword-devel mailing list: sword-devel at crosswire.org
> http://crosswire.org/mailman/listinfo/sword-devel
> Instructions to unsubscribe/change your settings at above page

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://crosswire.org/pipermail/sword-devel/attachments/20240305/a86d4fec/attachment.htm>


More information about the sword-devel mailing list