Skip to content

completed part 1-4 #7

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 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 67 additions & 4 deletions tree-practice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def initialize(val)
@value = val
@left = nil
@right = nil
@count = 0
end
end

Expand All @@ -15,7 +16,69 @@ 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)
# part 1
def operators(node)
return if node == nil
operators(node.left)
if node.left != nil || node.right != nil
print node.value + " "
end
operators(node.right)
end

# part 2
def countnonops(node, count = 0)
if node.left == nil && node.right == nil
return 1
else
countnonops(node.left, count += 1) + countnonops(node.right, count += 1)
end
end

# part 3
def prefix(node)
return if node == nil
print node.value + " "
prefix(node.left)
prefix(node.right)
end

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

# part 4
def find_op(node, operator)
if node.value == operator && node.value.match(/\D/) != nil
return true
elsif node.left != nil || node.right != nil
find_op(node.left, operator)
find_op(node.right, operator)
else
return false
end
end

root = TreeNode.new("-")
root.right = TreeNode.new("%")
root.right.left = TreeNode.new("10")
root.right.right = TreeNode.new("5")
root.left = TreeNode.new("+")
root.left.right = TreeNode.new("2")
root.left.left = TreeNode.new("*")
root.left.left.left = TreeNode.new("4")
root.left.left.right = TreeNode.new("3")
puts print_infix(root)
puts operators(root)
puts countnonops(root)
puts prefix(root)
puts postfix(root)
puts "find_op should return false"
puts find_op(root, ")")
puts "find_op should return true"
puts find_op(root, "-")
puts "find_op should ignore digit input and return false"
puts find_op(root, "9")