diff --git a/README.md b/README.md index 26e9c38..eccee9d 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/tree-practice.rb b/tree-practice.rb index 6b10248..f24b7fe 100644 --- a/tree-practice.rb +++ b/tree-practice.rb @@ -1,13 +1,14 @@ 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) @@ -15,7 +16,81 @@ 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) +# 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") + + + + + + diff --git a/tree-practice_test.rb b/tree-practice_test.rb new file mode 100644 index 0000000..71a6276 --- /dev/null +++ b/tree-practice_test.rb @@ -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