Skip to content
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

Here's this! #10

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
2 changes: 1 addition & 1 deletion 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.
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
Binary file added binarytree_notes.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added binarytree_notes2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 90 additions & 1 deletion tree-practice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,96 @@ def print_infix(node)
print_infix(node.right)
end

def print_prefix(node)
# base case
return if node == nil
print node.value + " "
print_prefix(node.left)
print_prefix(node.right)
end

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

def operators(node)
return if node == nil
operators(node.left)
if node.value == "+" || node.value == "-" || node.value == "*" || node.value == "/" || node.value == "%"
print node.value + " "
end
operators(node.right)
end

def non_operators(node)
return if node == nil
non_operators(node.left)
unless node.value == "+" || node.value == "-" || node.value == "*" || node.value == "/" || node.value == "%"
print node.value + " "
end
non_operators(node.right)
end

def find_operator(node, operator)
if node == nil
return false
elsif node.value == operator
return true
end
find_operator(node.left, operator)
find_operator(node.right, operator)
end

## see other test cases on paper

### trees we made in class ###
root = TreeNode.new("+")
root.left = TreeNode.new("3")
root.right = TreeNode.new("2")
print_infix(root)
# print_infix(root)

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

puts ""

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

# puts ""

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

# puts ""

### testing out methods
#
# operators(root4)
# non_operators(root4)
puts find_operator(root4, "-")

# print_infix(root4)
# puts ""
# print_prefix(root4)
# puts ""
# print_postfix(root4)