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

Space - Chelsea #20

Open
wants to merge 3 commits 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
166 changes: 148 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,172 @@ class Tree
attr_reader :root
def initialize
@root = nil
@inOrderArray = []
end

# Time Complexity:
# Space Complexity:
# Time Complexity: log n because it is binary to find where to add the node
# Space Complexity: worst case O(n) because we hit every node better average case like in a balanced tree it would be O(log n)
def add(key, value)
Comment on lines +20 to 22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if @root == nil
@root = TreeNode.new(key, value)
return
end

current = @root
add_helper(current, key, value)
end

# Time Complexity:
# Space Complexity:
def add_helper(current, key, value)
if current.key >= key
if current.left
current = current.left
add_helper(current, key, value)
else
newNode = TreeNode.new(key, value)
current.left = newNode
end
elsif current.key < key

if current.right
current = current.right
add_helper(current, key, value)
else
newNode = TreeNode.new(key, value)
current.right = newNode
end
end
end

# Time Complexity: log n due to the binary search
# Space Complexity: worst case O(n) because we hit every node better average case like in a balanced tree it would be O(log n)
def find(key)
Comment on lines +53 to 55

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if !@root
return nil
end

current = @root
return find_helper(current, key)
end

# Time Complexity:
# Space Complexity:
def find_helper(current, searchKey)
if current.key == searchKey
return current.value
end

if current.key > searchKey
current = current.left
return find_helper(current, searchKey)
elsif current.key < searchKey
current = current.right
return find_helper(current, searchKey)
end
end

# Time Complexity: O(n) because it has to go through every node.
# Space Complexity: worst case O(n) because we hit every node better average case like in a balanced tree it would be O(log n)
def inorder
Comment on lines +78 to 80

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if @root == nil
return []
end

current = @root
getInOrder(current)
return @inOrderArray
end

# Time Complexity:
# Space Complexity:
def getInOrder(node)
if node.left != nil
getInOrder(node.left)
end

@inOrderArray.push({:key=>node.key, :value=>node.value})

if node.right != nil
getInOrder(node.right)
end
end

# Time Complexity: O(n) because it has to go through every node.
# Space Complexity: worst case O(n) because we hit every node better average case like in a balanced tree it would be O(log n)
def preorder
Comment on lines +102 to 104

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The space complexity for these traversals is O(n) because we're building an array.

With recursion it would be O(n + n) = O(2n) = O(n) or average case O(n + log n) = O(n)

raise NotImplementedError
if @root == nil
return []
end

current = @root
getPreOrder(current)
return @inOrderArray
end

# Time Complexity:
# Space Complexity:
def getPreOrder(node)
@inOrderArray.push({:key=>node.key, :value=>node.value})

if node.left != nil
getPreOrder(node.left)
end

if node.right != nil
getPreOrder(node.right)
end
end

# Time Complexity: O(n) because it has to go through every node.
# Space Complexity: worst case O(n) because we hit every node, better average case like in a balanced tree it would be O(log n)
def postorder
Comment on lines +126 to 128

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 With the earlier note on the traversals.

raise NotImplementedError
if @root == nil
return []
end

current = @root
getPostOrder(current)
return @inOrderArray
end

# Time Complexity:
# Space Complexity:
def getPostOrder(node)

if node.left != nil
getPostOrder(node.left)
end

if node.right != nil
getPostOrder(node.right)
end

@inOrderArray.push({:key=>node.key, :value=>node.value})
end

# Time Complexity: O(n) because we hit eery node
# Space Complexity: worst case O(n) because we hit every node better average case like in a balanced tree it would be O(log n)
def height
Comment on lines +151 to 153

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if @root == nil
return 0
end

current = @root
height = 1

height = getHeight(current, height)
return height
end

def getHeight(node, height)

left = height
right = height

if node.left != nil
left = getHeight(node.left, height +1)
end

if node.right != nil
right = getHeight(node.right, height + 1)
end

if left >= right
return left
else
return right
end
end

# Optional Method
Expand Down