Skip to content

Homework for Binary Tree Expressions #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Tree Practice

## Creating a binary tree on paper
On paper, create a binary search tree to represent the expressions below.
## Creating a binary expression tree on paper
On paper, create a binary expression tree to represent the expressions below.
1. `3 + 2`
2. `3 + 2 - 10`
3. `4 * 3 + 2`
Expand Down
87 changes: 81 additions & 6 deletions tree-practice.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,96 @@
class TreeNode
attr_accessor :value, :left, :right

attr_accessor :value, :left, :right, :operator
def initialize(val)
@value = val
@left = nil
@right = nil
@operator = operator
end
end

# evaluates left middle right
def print_infix(node)
return if node == nil
print_infix(node.left)
print node.value + " "
print_infix(node.right)
end

root = TreeNode.new("+")
root.left = TreeNode.new("3")
root.right = TreeNode.new("2")
print_infix(root)
# evaluates middle left right
def print_prefix(node)
return if node == nil
print node.value + " "
print_prefix(node.left)
print_prefix(node.right)
end

# evaluates left right middle
def print_postfix(node)
return if node == nil
print_postfix(node.left)
print_postfix(node.right)
print node.value + " "
end

def print_operators(node)
return if node == nil
print_operators(node.left)
if node.left != nil || node.right != nil
print node.value + " "
end
print_operators(node.right)
end

def print_non_operators(node)
return if node == nil
print_non_operators(node.left)
if node.left == nil || node.right == nil
print node.value + " "
end
print_non_operators(node.right)
end

def find_operators(node, operator)
return if node == nil
count = 0
find_operators(node.left)
# search the whole tree and count up the operators
if node.value == operator
count += 1
end
find_operators(node.right)
end

# root = TreeNode.new("+")
# root.left = TreeNode.new("3")
# root.right = TreeNode.new("2")
# puts print_infix(root)

# root = TreeNode.new("-")
# root.left = TreeNode.new("+")
# root.right = TreeNode.new("10")
# root.left.left = TreeNode.new("3")
# root.left.right = TreeNode.new("2")

# root = TreeNode.new("+")
# root.left = TreeNode.new("*")
# root.right = TreeNode.new("2")
# root.left.left = TreeNode.new("4")
# root.left.right = TreeNode.new("3")
# puts print_infix(root)

root = TreeNode.new("-")
root.left = TreeNode.new("+")
root.right = TreeNode.new("%")
root.right.left = TreeNode.new("10")
root.right.right = TreeNode.new("5")
root.left.left = TreeNode.new("*")
root.left.right = TreeNode.new("2")
root.left.left.left = TreeNode.new("4")
root.left.left.right = TreeNode.new("3")






37 changes: 37 additions & 0 deletions tree-practice_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
gem 'minitest', '>= 5.0.0'
require 'minitest/pride'
require 'minitest/autorun'
require_relative 'tree-practice'


class TreeTest < Minitest::Test
@expression = TreeNode.new("-")
@expression.left = TreeNode.new("+")
@expression.right = TreeNode.new("%")
@expression.right.left = TreeNode.new("10")
@expression.right.right = TreeNode.new("5")
@expression.left.left = TreeNode.new("*")
@expression.left.right = TreeNode.new("2")
@expression.left.left.left = TreeNode.new("4")
@expression.left.left.right = TreeNode.new("3")

def test_infix_notation
assert_equal print_infix(@expression) "4 * 3 + 2 - 10 % 5"
end

def test_postfix_notation
assert_equal print_postfix(@expression), '4 3 * 2 + 10 5 % -'
end

def test_prefix_notation
assert_equal print_prefix(@expression), '- + * 4 3 2 % 10 5'
end

def test_print_operators
assert_equal print_operators(@expression), '* + - %'
end

def test_print_nonoperators
assert_equal print_non_operators(@expression), '4 3 2 10 5'
end
end