-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Jessica #29
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
base: master
Are you sure you want to change the base?
Space - Jessica #29
Changes from all commits
6484372
68328a0
8e38c0e
0dcd99a
030e4e6
8da466b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -14,42 +14,121 @@ class Tree | |||
| attr_reader :root | ||||
| def initialize | ||||
| @root = nil | ||||
| @nodes = [] | ||||
| end | ||||
|
|
||||
| def add_helper(current, key) | ||||
| if key <= current.key | ||||
| return current if current.left.nil? | ||||
| add_helper(current.left, key) | ||||
| else | ||||
| return current if current.right.nil? | ||||
| add_helper(current.right, key) | ||||
| end | ||||
| end | ||||
|
|
||||
| # Time Complexity: | ||||
| # Space Complexity: | ||||
| def add(key, value) | ||||
| raise NotImplementedError | ||||
| if @root == nil | ||||
| @root = TreeNode.new(key, value) | ||||
| return | ||||
| end | ||||
|
|
||||
| node = add_helper(@root, key) | ||||
| if key <= node.key | ||||
| node.left = TreeNode.new(key, value) | ||||
| else | ||||
| node.right = TreeNode.new(key, value) | ||||
| end | ||||
| end | ||||
|
|
||||
| def find_helper(current, key) | ||||
| if key <= current.key | ||||
| return current.value if current.key == key | ||||
| find_helper(current.left, key) | ||||
| elsif key > current.key | ||||
| return current.value if current.key == key | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can't be true.
Suggested change
|
||||
| find_helper(current.right, key) | ||||
| end | ||||
| end | ||||
|
|
||||
| # Time Complexity: | ||||
| # Space Complexity: | ||||
| def find(key) | ||||
| raise NotImplementedError | ||||
| return nil if @root.nil? | ||||
| return @root.value if @root.key == key | ||||
|
|
||||
| current = @root | ||||
| find_key = find_helper(current, key) | ||||
|
|
||||
| return find_key | ||||
| end | ||||
|
|
||||
| # inorder helper | ||||
| def traverse_inorder(tree) | ||||
| traverse_inorder(tree.left) if tree.left | ||||
| @nodes.push({key: tree.key, value: tree.value}) | ||||
| traverse_inorder(tree.right) if tree.right | ||||
| end | ||||
|
|
||||
| # Time Complexity: | ||||
| # Space Complexity: | ||||
| # left, root, right | ||||
| def inorder | ||||
|
Comment on lines
75
to
78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Time/space complexities? |
||||
| raise NotImplementedError | ||||
| return [] if @root.nil? | ||||
| traverse_inorder(@root) | ||||
| return @nodes | ||||
| end | ||||
|
|
||||
| # preorder helper | ||||
| def traverse_preorder(tree) | ||||
| @nodes.push({key: tree.key, value: tree.value}) | ||||
| traverse_preorder(tree.left) if tree.left | ||||
| traverse_preorder(tree.right) if tree.right | ||||
| end | ||||
|
|
||||
| # Time Complexity: | ||||
| # Space Complexity: | ||||
| def preorder | ||||
|
Comment on lines
91
to
93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Time/space complexities? |
||||
| raise NotImplementedError | ||||
| return [] if @root.nil? | ||||
| traverse_preorder(@root) | ||||
| return @nodes | ||||
| end | ||||
|
|
||||
| # postorder helper | ||||
| def traverse_postorder(tree) | ||||
| traverse_postorder(tree.left) if tree.left | ||||
| traverse_postorder(tree.right) if tree.right | ||||
| @nodes.push({key: tree.key, value: tree.value}) | ||||
| end | ||||
|
|
||||
| # Time Complexity: | ||||
| # Space Complexity: | ||||
| def postorder | ||||
|
Comment on lines
106
to
108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Time/space complexities? |
||||
| raise NotImplementedError | ||||
| return [] if @root.nil? | ||||
| traverse_postorder(@root) | ||||
| return @nodes | ||||
| end | ||||
|
|
||||
| def height_helper(tree, height) | ||||
| return height_helper(tree.left, height) if tree.left | ||||
| return height_helper(tree.right, height) if tree.right | ||||
| return height + 1 | ||||
| end | ||||
|
|
||||
| # Time Complexity: | ||||
| # Space Complexity: | ||||
| def height | ||||
|
Comment on lines
120
to
122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Time/space complexities? |
||||
| raise NotImplementedError | ||||
| return 0 if @root.nil? | ||||
| return 1 if (@root.left.nil? && @root.right.nil?) | ||||
|
|
||||
| left_height = 1 | ||||
| height_helper(@root.left, 1) if @root.left | ||||
| right_height = 1 | ||||
| height_helper(@root.right, 1) if @root.right | ||||
|
|
||||
| return left_height >= right_height ? left_height : right_height | ||||
| end | ||||
|
|
||||
| # Optional Method | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't actually work, you only can construct a 3-node tree your way.
Consider: