On paper, create a binary expression tree to represent the expressions below.
3 + 2
3 + 2 - 10
4 * 3 + 2
4 * 3 + 2 - 10 % 5
- Take a look at
tree-practice.rb
- Let's walk through how
3 + 2
was created as a binary tree. - Let's walk through how
print_infix
works. - Create
4 * 3 + 2 - 10 % 5
in code as a binary tree.
- Let's write a method that prints out all the operators that exist in the tree. Added Fun: If there are duplicate operators, don't print them separately, but instead print a tally of all operators found.
- Write a method that returns the count of non-operators (in this case, leaves) in the tree. Added Fun: print the level of the tree that each leaf appears on.
- Write methods to print the tree in prefix and postfix notations.
- Write a method that returns whether or not a given operator exists in the tree. Added fun: If the operator occurs more than once, return the count on how many times it occurs - you may return 0 if it doesn't exist in the tree.
- Added Fun: Evaluate an infix binary tree.
- Come up with at least 3 test cases for problems 1-4 above. Added Fun: #5 too.
- Code out solutions for problems 1-4 above. Added Fun: #5 too.
### Resources * https://en.wikipedia.org/wiki/Binary_expression_tree