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

Scissors laurel O #60

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tree Exercise

In this exercise you will implement, in Ruby, several Tree methods.
In this exercise you will implement, in Python, several Tree methods.

- `add(value)` - This method adds a key-value pair to the Binary Search Tree
- `find(value)` - This method returns the matching value for the given key if it is in the tree and `None` if the key is not found in the tree.
Expand Down
152 changes: 128 additions & 24 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,155 @@ def __init__(self, key, val = None):
self.value = val
self.left = None
self.right = None




class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
# Time/ Space Complexity for unbalanced tree: O(n)
#Time/ Space Complexity for unbalanced tree: O(log n)

def add(self, key, value = None):
pass
if self.root == None:
self.root = TreeNode(key, value)
else:
self._add(self.root, key, value)

def _add(self, current_node, key, value):
if current_node == None:
return TreeNode(key, value)

# Time Complexity:
# Space Complexity:
if key <= current_node.key:
current_node.left = self._add(current_node.left, key, value)
else:
current_node.right = self._add(current_node.right, key, value)
return current_node

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(self, key):
Comment on lines +35 to 37

Choose a reason for hiding this comment

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

👍

pass
if self.root == None:
return None
current = self.root
while current:
if current.key == key:
return current.value
elif key > current.key:
current = current.right
else:
current = current.left
return None

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(self):
Comment on lines +50 to 52

Choose a reason for hiding this comment

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

👍

pass
values =[]

if self.root == None:
return values

return self._in_order(self.root, values)

# Time Complexity:
# Space Complexity:
def _in_order(self, node, values):
if node == None:
return

self._in_order(node.left, values)
values.append({
'key': node.key,
'value': node.value
})
self._in_order(node.right, values)

return values

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):
Comment on lines +73 to 75

Choose a reason for hiding this comment

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

👍

pass
values = []

if self.root == None:
return values

return self._pre_order(self.root, values)

def _pre_order(self, node, values):
if node == None:
return

# Time Complexity:
# Space Complexity:
values.append({
'key': node.key,
'value': node.value
})
self._pre_order(node.left, values)
self._pre_order(node.right, values)

return values

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):
pass
values = []

if self.root == None:
return values

return self._post_order(self.root, values)

def _post_order(self, node, values):
if node == None:
return

self._post_order(node.left, values)
self._post_order(node.right, values)
values.append({
'key': node.key,
'value': node.value
})

# Time Complexity:
# Space Complexity:
return values

# Time Complexity: O(n)
# Space Complexity: O(log n) (balanced) or O(n) for unbalanced
def height(self):
pass
if self.root == None:
return 0

return self._height(self.root)

def _height(self, node):
if node == None:
return 0

l = self._height(node.left)
r = self._height(node.right)

return (1 + max(l, r))

# # Optional Method
# # Time Complexity:
# # Space Complexity:
# # Time Complexity: O(n^2)
# # Space Complexity: O(n)
## to-do: rewrite below using dequeue or linked list for linear time

def bfs(self):
pass
values = []
queue = []
if self.root:
queue.append(self.root)


while len(queue) > 0:
current_node = queue.pop(0)
if current_node.left:
queue.append(current_node.left)
if current_node.right:
queue.append(current_node.right)

values.append({
"key": current_node.key,
"value": current_node.value,
})
return values


# # Useful for printing
Expand Down