What you implemented is this (in pseudo code):
display(line) {
if no_x_in(line) {
print(line) // instance output and recursion stop
}
display(replace_first_x_with_0(line)) // recursive call
display(replace_first_x_with_1(line)) // recursive call
}
If the string in
linecontains noxsymbols anymore you can output the string and your recursive descent can stop.If not, the problem instance is reduced from a
linewith n times manyxsymbols into two smaller instances, each with n - 1 manyxsymbols,- one with the
xreplaced by a0symbol and - one with the
xreplaced by a1symbol.
- one with the
which result into a recursive call each. As there are only finite many x symbols in the finite input string, the recursive calls will stop at some point, and the resulting call tree is finite as well.
For your example the call tree is like this:
display('xx') -> issues calls to display('0x') and display('1x')
|
+-> display('0x') -> issues calls to display('00') and display('01')
| |
| +-> display('00') -> output, stop
| +-> display('01') -> output, stop
|
+-> display('1x') -> issues calls to display('10') and display('11')
|
+-> display('10') -> output, stop
+-> display('11') -> output, stop
Answer from mvw on Stack Overflowc - Visualizing recursion for binary tree - Stack Overflow
Advice on visualizing recursion
Depth First Search from the Ground Up: How to Visualize Recursion and Solve Problems with Depth-First Search
algorithm - Is there any way to have simple ascii visualization of binary search tree? - Stack Overflow
What is the difference between a binary tree and a binary search tree?
What is a complete binary tree?
When should I use a binary tree instead of an array or hash table?
Hey Guys,
I have been solving a lot of Tree related problems lately. Of course Recursion is a technique which will make it easy , right? The problem I am facing is that i just cannot visualize recursion through the tree. I mean everyone knows what is recursion but its not just "clicking". I have gone through a lot of YT videos and of course discussion section but all i can do is admire how amazingly brilliant folks are and how dumb i am. Idk if any of this makes sense, would appreciate some advice!
I think a recursive traversal will be easiest. With a non-recursive solution you end up having to manage the stack yourself.
Here's some code in C#, which you should be able to port to Python easily enough:
string Traverse(Node node)
{
string rslt = "";
bool hasRightNode = false;
bool hasLeftNode = false;
if (node.Right != null)
{
hasRightNode = true;
rslt = rslt + "(";
rslt = rslt + Traverse(node.Right);
}
if (node.Left != null)
{
hasLeftNode = true;
if (hasRightNode)
{
rslt = rslt + ",";
}
else
{
rslt = rslt + "(";
}
rslt = rslt + Traverse(node.Left);
}
if (hasLeftNode || hasRightNode)
{
rslt = rslt + ")";
}
rslt = rslt + node.Value;
return rslt;
}
The only thing missing is the final semicolon. You can call it with:
string format = Traverse(root) + ";";
Given the tree that you posted, that outputs the expected format string.
Note that I use string concatenation here, which is sub-optimal in C#. If this were a production program, I'd probably use a StringBuilder object to avoid concatenation. I'm not familiar enough with Python to say how best to compose strings in that language.
According to Mr.Jim Mischel sample code in C#, I added the following function in the Node class:
def R_postorder(self):
ret = ''
if self:
hasRightChild = False
hasLeftChild = False
if self.rightChild:
hasRightChild = True
ret += '('
ret += self.rightChild.RLV()
if self.leftChild:
hasLeftChild = True
if hasRightChild:
ret += ','
else:
ret += '('
ret += self.leftChild.RLV()
if hasRightChild or hasLeftChild:
ret += ')'
ret += str(self.data)
return ret
and I also added the R_postorder to the BST class:
def R_postorder(self):
ret = self.rootNode.RLV()
ret += ';'
return ret
By using the returned value of bst.R_postorder() as an input to create the tree_format variable, the right outcome would be achieved.