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

Tree practice exercise #4

Open
wants to merge 3 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
146 changes: 136 additions & 10 deletions tree-practice.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class TreeNode
attr_accessor :value, :left, :right
attr_accessor :value, :left, :right

def initialize(val)
@value = val
@left = nil
@right = nil
end
def initialize(val)
@value = val
@left = nil
@right = nil
end
end

def print_infix(node)
Expand All @@ -15,7 +15,133 @@ def print_infix(node)
print_infix(node.right)
end

root = TreeNode.new("+")
root.left = TreeNode.new("3")
root.right = TreeNode.new("2")
print_infix(root)
def print_prefix(node)
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)
print node.value + " " unless node.left.nil? && node.right.nil?
operators(node.right)
end

# method that returns the count of non-operators (in this case, leaves)
def leaves(node, depth=0)
if node.left.nil? && node.right.nil?
puts "#{node.value}, depth: #{depth}"
return 1
else
leaves(node.left, depth+1) + leaves(node.right, depth+1)
end
end

# method that returns whether a given operator exists in the tree
def includes_op(node, op)
return true if node.value == op
if !node.left.nil? && !node.right.nil?
includes_op(node.left, op) || includes_op(node.right, op)
else
return false
end
end

def op_count(node, op)
count = 0
unless node.left.nil?
count += op_count(node.left, op)
count += op_count(node.right, op)
end
count += 1 if node.value == op
return count
end

################################################
# print all the things
root = TreeNode.new("-")
root.right = TreeNode.new("10")
root.left = TreeNode.new("+")
root.left.left = TreeNode.new("20")
root.left.right = TreeNode.new("2")
puts "Tree 1"
puts "infix:"
puts print_infix(root)
puts "operators:"
puts operators(root)
puts "leaves:"
puts leaves(root)
puts "prefix:"
puts print_prefix(root)
puts "postfix:"
puts print_postfix(root)
puts "includes operator '+'?"
puts includes_op(root, '+')
puts "includes operator '/'?"
puts includes_op(root, '/')
puts("*" * 30)

root2 = TreeNode.new("+")
root2.left = TreeNode.new("-")
root2.left.left = TreeNode.new("/")
root2.left.left.left = TreeNode.new("10")
root2.left.right = TreeNode.new("3")
root2.left.left.right = TreeNode.new("2")
root2.right = TreeNode.new("*")
root2.right.left = TreeNode.new("+")
root2.right.left.left = TreeNode.new("7")
root2.right.left.right = TreeNode.new("1")
root2.right.right = TreeNode.new("5")
puts "Tree 2"
puts "infix:"
puts print_infix(root2)
puts "operators:"
puts operators(root2)
puts "leaves:"
puts leaves(root2)
puts "prefix:"
puts print_prefix(root2)
puts "postfix:"
puts print_postfix(root2)
puts "includes operator '+'?"
puts includes_op(root2, '+')
puts "includes operator '%'?"
puts includes_op(root2, '%')
puts "count of + operators:"
puts op_count(root2, '+')
puts("*" * 30)

root3 = TreeNode.new("-")
root3.right = TreeNode.new("%")
root3.right.right = TreeNode.new("5")
root3.right.left = TreeNode.new("10")
root3.left = TreeNode.new("+")
root3.left.left = TreeNode.new("*")
root3.left.right = TreeNode.new("2")
root3.left.left.left = TreeNode.new("4")
root3.left.left.right = TreeNode.new("3")
puts "Tree 3"
puts "infix:"
puts print_infix(root3)
puts "operators:"
puts operators(root3)
puts "leaves:"
puts leaves(root3)
puts "prefix:"
puts print_prefix(root3)
puts "postfix:"
puts print_postfix(root3)
puts "includes operator '+'?"
puts includes_op(root3, '+')
puts "includes operator '/'?"
puts includes_op(root3, '/')