Guest User

Untitled

a guest
Jul 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. enter the expression string a+b*c
  2. the expression is correct
  3. the postfix expression is - abc *+
  4. enter the value of a-1
  5. enter the value of b-2
  6. enter the value of c-3
  7. the postfix expression is -abc*+
  8. result= 7
  9.  
  10. +
  11. /
  12. * a
  13. /
  14. b c
  15.  
  16. +
  17. a
  18. *
  19. b
  20. c
  21.  
  22. Add
  23. +-- a
  24. +-- Mul
  25. +-- b
  26. +-- c
  27.  
  28. Add
  29. +---Sub
  30. | +---Div
  31. | | +---p
  32. | | +---q
  33. | +---y
  34. +---Mul
  35. +---b
  36. +---c
  37.  
  38. # Return the drawn tree as an array of lines.
  39. #
  40. # node ::= string
  41. # node ::= [string, node, node]
  42. def render_tree(node, prefix0 = "", prefix = "")
  43. if (node.is_a?(String))
  44. puts prefix0 + node # Value
  45. else
  46. puts prefix0 + node[0] # Operator
  47. render_tree(node[1], prefix + "+---", prefix + "| ")
  48. render_tree(node[2], prefix + "+---", prefix + " ")
  49. end
  50. end
  51. render_tree(["Add", ["Sub", ["Div", "p", "q"], "y"], ["Mul", "b", "c"]])
Add Comment
Please, Sign In to add comment