diff --git a/README.md b/README.md index 26e9c38..35cf72b 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. +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/binarytree_notes.jpeg b/binarytree_notes.jpeg new file mode 100644 index 0000000..0cfd94b Binary files /dev/null and b/binarytree_notes.jpeg differ diff --git a/binarytree_notes2.jpeg b/binarytree_notes2.jpeg new file mode 100644 index 0000000..5fe332e Binary files /dev/null and b/binarytree_notes2.jpeg differ diff --git a/tree-practice.rb b/tree-practice.rb index 6b10248..fb8992d 100644 --- a/tree-practice.rb +++ b/tree-practice.rb @@ -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)