From 9a97081462dee65b6f6c05152a58a060c66ef07d Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 2 Jun 2021 15:44:34 -0700 Subject: [PATCH 1/2] add and find done --- lib/tree.rb | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..3c722df 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -19,13 +19,52 @@ def initialize # Time Complexity: # Space Complexity: def add(key, value) - raise NotImplementedError + # there may or may not be a root + # if there's no root then the key/value will be the new root + # otherwise check to see if value is bigger or smaller than the root value + # initialize new node with the key and value + # check if it's smaller or larger and if it's the last node of the branch + # if it's the last node then add it after that node + input = TreeNode.new(key,value) + if @root == nil + @root = input + return + else here = @root + end + while here != nil + if input.key < here.key + if here.left != nil + here = here.left + else here.left = input + return + end + else input.key > here.key + if here.right != nil + here = here.right + else here.right = input + return + end + end + end end # Time Complexity: # Space Complexity: def find(key) - raise NotImplementedError + if @root == nil + return nil + end + here = @root + while here != nil + if here.key == key + return here.value + elsif key < here.key + here = here.left + elsif key > here.key + here = here.right + end + end + return nil end # Time Complexity: From e0dfddca8c7a457e492083b7b2aa175cbcf0eae9 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Wed, 9 Jun 2021 15:06:59 -0700 Subject: [PATCH 2/2] all tests passing --- lib/tree.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 3c722df..b710d56 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -18,7 +18,7 @@ def initialize # Time Complexity: # Space Complexity: - def add(key, value) + def add(key, value = 0) # there may or may not be a root # if there's no root then the key/value will be the new root # otherwise check to see if value is bigger or smaller than the root value @@ -51,7 +51,7 @@ def add(key, value) # Time Complexity: # Space Complexity: def find(key) - if @root == nil + if @root.nil? return nil end here = @root @@ -70,27 +70,87 @@ def find(key) # Time Complexity: # Space Complexity: def inorder - raise NotImplementedError + values = [] + if @root.nil? + return values + end + return inorder_helper(@root, values) + end + + def inorder_helper(here, values) + if here.nil? + return values + end + + inorder_helper(here.left, values) + values.push({:key=>here.key, :value=>here.value}) + inorder_helper(here.right, values) + return values end + # Time Complexity: # Space Complexity: def preorder - raise NotImplementedError + values = [] + if @root.nil? + return values + end + return preorder_helper(@root, values) + end + + def preorder_helper(here, values) + if here.nil? + return values + end + + values.push({:key=>here.key, :value=>here.value}) + preorder_helper(here.left, values) + preorder_helper(here.right, values) + + return values end # Time Complexity: # Space Complexity: def postorder - raise NotImplementedError + values = [] + if @root.nil? + return values + end + return postorder_helper(@root, values) + end + + def postorder_helper(here, values) + if here.nil? + return values + end + + postorder_helper(here.left, values) + postorder_helper(here.right, values) + values.push({:key=>here.key, :value=>here.value}) + return values end # Time Complexity: # Space Complexity: def height - raise NotImplementedError + height = 0 + if @root.nil? + return height + end + return height_helper(@root) + end + + def height_helper(here) + if here.nil? + return 0 + end + return 1 + [height_helper(here.left), height_helper(here.right)].max end + + # Optional Method # Time Complexity: # Space Complexity: