I'm having to create a binary expression tree for the postorder expression
XYZ+AB-C*/-
from what i know, with pupushing opperands into a stack and the popping two out when an operator is next in th list, my best attempt at the binary expression tree is this
-
/ \
X /
\
*
/ \
C -
/ \
A B
\
+
/ \
Y Z
is this correct? or am I completely wrong
Your stack shall look like this when talking about operations and their precedence.
X - ((A-B) * C) / (Y+Z)
((A-B) * C) / (Y+Z)
(A-B) * C
(A-B)
(Y+Z)
X
So the correct way is Stephan's answer.
Related
I am struggling with how to recursively traverse a binary tree in Haskell
It is rather clear how it's done when the return type is a list. But I don't get how is it done if the return type is a tree itself?
for example, if we have the tree and wish to exclude zeros:
5
/ \
0 2
/ \
0 1
and we would like to return a tree with the other numbers:
5
/ \
1 2
Then I figured you would do something like this:
modTree :: Tree -> Tree
modTree Void = Void
modTree (Node l x r)
| x == 0 = modTree l
| otherwise = Node (modTree l) x (modTree r)
My issue is that I know that this will only traverse the left side. But I fail to grasp how recursively call the function in Haskell for the entire tree.
Any suggestions would be highly appreciated.
One idea would be to bubble 0s down and left until they have no left child, then replace them with their right child. For example:
0 1 1 1
/ \ / \ / \ / \
1 2 -> 0 2 -> 3 2 -> 3 2
/ \ / \ / \ \
3 4 3 4 0 4 4
You need to take some care about what happens when there are multiple 0s on the path from root to leaf.
0
/ \
0 2
/ \
3 4
Probably the simplest (though perhaps not the most efficient) way to deal with this is to recursively drop 0s from the left child before beginning to bubble down.
0 0 0 3 3
/ \ / \ / \ / \ / \
0 2 -> 3 2 -> 3 2 -> 0 2 -> 4 2
/ \ / \ \ \
3 4 0 4 4 4
I encourage you to take a stab at implementing this yourself. If you have trouble, describing what you tried, where you got stuck, and why you think the problem you're seeing is insurmountable would make a good follow-up question.
The standard thing to do here is this:
if the root is non-zero we apply the algorithm recursively to each of its child branches and we're done (as you already indicate in your code)
if the root is zero with only one child we just return that child (transformed, of course)
if the root is zero with two child branches we apply the algorithm recursively to each of the two branches, then pull the rightmost value from the new left child and put it into the root.
To pull the rightmost value from a tree:
if it's a leaf, just remove the leaf
if it's a node, it doesn't have a right child, so just put its left child in its place
The whole thing is linear,(*) apparently.
modTree :: Tree -> Tree
modTree Void = Void
modTree (Node l x r)
| x == 0 = modZero (modTree l) (modTree r)
| otherwise = Node (modTree l) x (modTree r)
modZero Void r = r
modZero l Void = l
modZero l r = let (v, nl) = pullRightmost l in
Node nl v r
Implementing pullRightmost is left as an exercise.
(*)No spine is pulled from over more than once, because we are pulling from the tree which is already without any 0s, "after" the recursive call. So these will be another O(n) operations in total.
I'm trying to draw an abstract tree for the following Haskell function:
f t = t + t
twice f t = f(f(t))
twice f 1
The examples I've found online (e.g. below image) are quite simple to understand, but I think I'm getting lost when it comes to the names of functions.
The tree I currently have is:
But it just seems a bit incomplete or that I'm missing something?
If anyone could help/point me in the right direction or share any good resources I'd be grateful. Thanks in advance.
The expression twice f 1 is parsed as a pair of applications: first twice is applied to f, then that result is applied to 1.
There is no token in the expression that corresponds to application, as application is just represented by juxtaposition (two tokens next to each other). That doesn't mean, though, that there is no node in the tree to represent application. So, we start with a root node that represents the act of applying:
apply
This node has two children; the thing being applied, which is another application, and the thing being applied to.
apply
/ \
/ \
apply value
/ \ |
/ \ number "1"
/ \
value value
| |
identifier identifier
"twice" "f"
The structure of the tree encodes the precedence of function application. If your expression were twice (f 1), there would be no parentheses explicitly stored in the tree; rather, the structure of the tree itself would change.
apply
/ \
/ \
value apply
| / \
identifier / \
"twice" / \
value value
| |
identifier number "1"
"f"
I have been looking for an answer now and I can't find a solution. I am trying to use sympy.solve in Python 3 and I only want to see the real roots and all positive answers.
Sympy is able to solve what equation I want, but It shows a bunch of nonsense as the other answers. I have tried some of the other methods I found like solveset but they seem to still show the complex solutions
Image of Sympy solution
This is my code if that helps:
h, rho_c_s, T_c_s, k, pi, M, M_sun, m_H, m_e, mu_e = symbols(
r"h,\rho_\odot,T_\odot,k,\pi,M,M_\odot,m_H,m_e,\mu_e",positive=True,real=True)
rho_c = rho_c_s * (M / M_sun)**(-2 / 7)
T_c = T_c_s * (M / M_sun)**(4 / 7)
expr1 = (h**2) / (20 * m_e) * (3 / pi)**(2 / 3) * (rho_c /
(mu_e * m_H))**(5 / 3)
expr2 = (rho_c * k * T_c) / (mu_e * m_H)
expr = expr1 - expr2
eqn = Eq(expr, 0)
soln = solve(eqn, M, simplify=True,real=True,positive=True,)
init_printing()
soln
I appreciate your help.
Thanks
Well I figured it out.
You want to do:
soln[0]
And it will only give you:
Solution
Complex solutions are normal and intended. To hide the complex portions look here:
https://stackoverflow.com/a/73669399/11468876
I am relatively new to SAS with limited programming experience. I need to write code that searches for the value of a specific variable that will form an equality. For example, I need to find the value of k that makes the following algebraic equation hold:
A = B + {[(C - k(B)] / (1+k)} + {[(D - k(E)] / (1+k)^2}, etc.
In this equation, I know the values of A, B, C, D, etc. and need to search for a value of k (the discount rate) that fits the equality.
Here's the proc model code I'm trying to use:
proc model data = test noprint;
p = bv0 + ((e1 - (k * bv0)) / (1+k)) + ((e2 - (k * bv1)) / ((1+k)**2)) + ((e3 - (k * bv2)) / ((1+k)**3)) + ((e3 - k *(bv2)) * (1+g)) / (((1+k)**3) * (k - g));
ENDOGENOUS k;
solve k / out = est;
run;
When I run this code, I receive the following error message:
WARNING: No equations are defined in the model. (Check for missing VAR or ENDOGENOUS statement.)
ERROR: The following solve variables do not appear in any of the equations to be solved: k
Any help anyone can provide would be great! Thanks!
If p is supposed to be the name of an equation, try adding eq. prefix before p. If p is a variable that the expression on the right should be equal to, then replace p with eq.equation1 and put -p on the right side.
I'm trying to figure out how to do parse trees correctly to show whether a grammar is ambiguous or not.
my grammar is S -> xSy | ySx | SS | e
any help would be great. thanks in advance
below is my crack at it...
S
/ \
/ \
x y
/ \ / \
x x y y
As a hint, pretty much any grammar with a production of the form
S → SS
will be ambiguous, because if you want to produce three S nonterminals you can do so in two ways:
S S
/ \ / \
S S S S
/ \ / \
S S S S
Assuming those S's can actually produce strings of terminals, these two "gadgets" can be put into the parse tree to derive the same string in two different ways.
Hope this helps!