Note that there are some explanatory texts on larger screens.

plurals
  1. POusing backtracking with tree
    primarykey
    data
    text
    <p>I'm using the backtracking algorithm in tree thanks to a stack (with push and pop). It works but i've got a problem. The path given by the stack is "wrong".</p> <pre><code>bool Prefix(Node*root, stack *tas, char *search) { if (!isEmpty(root)) { if (strcmp(search,root-&gt;data) != 0) push(tas, root-&gt;data, search, root); if (strcmp(search,root-&gt;data) == 0) { return True ; } Prefix(root-&gt;left, tas, search); Prefix(root-&gt;right, tas, search); } return False; } </code></pre> <p>For example, i've got a tree as :</p> <pre><code> Root / \ A B / \ / \ C D E F </code></pre> <p>When I want the path of C for example, this function returns R A B (ok for R and A, not B).</p> <p>I don't know if it comes from this function or the push() function. If you don't see any error in the function, I will paste push() but it's quite long.</p> <p>Edit: I better understand now, I've added to the function :<br> if node is a leaf, pop(). If I search F, it returns me R A B F instead of R B F. I don't konw how to avoid to keep A in the stack.</p> <p>edit2 :</p> <p>Here is the code : (returns R A B F instead of R B F)</p> <pre><code>bool Prefix(Node*root, stack *tas, char *search) { if (!isEmpty(root)) { if (strcmp(search,root-&gt;data) == 0) return True ; if (LeafOrNot(root) == True ){ //it's a leaf, pop() pop(tas); if (Prefix(root-&gt;left, tas, search)) return True; if (Prefix(root-&gt;right, tas, search)) return True; } return False; } </code></pre> <p>I don't understand how I can pop traversing child node to obtain the good result, maybe adding a else to if (Prefix(root->left, tas, search)) ? Anyone have an idea ?</p> <p>thanks !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload