Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
What's new
2
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dacio Romero
CS 1.3
Commits
aadba6e6
Verified
Commit
aadba6e6
authored
May 16, 2019
by
Dacio Romero
🔎
Browse files
Annotate binary tree complexity
parent
8bcddb3a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Lessons/source/binarytree.py
View file @
aadba6e6
...
...
@@ -86,8 +86,8 @@ class BinarySearchTree(object):
def
contains
(
self
,
item
:
Any
)
->
bool
:
"""Return True if this binary search tree contains the given item.
TODO:
Best case running time:
??? under what conditions?
TODO:
Worst case running time:
??? under what conditions?
Best case running time:
O(1) item is a the root or tree is empty.
Worst case running time:
O(h) where h is the height, item is at longest leaf.
"""
# Find a node with the given item, if any
node
=
self
.
_find_node_recursive
(
item
,
self
.
root
)
...
...
@@ -96,9 +96,11 @@ class BinarySearchTree(object):
def
search
(
self
,
item
:
Any
)
->
Optional
[
Any
]:
"""Return an item in this binary search tree matching the given item,
or None if the given item is not found.
TODO: Best case running time: ??? under what conditions?
TODO: Worst case running time: ??? under what conditions?"""
or None if the given item is not found.
Best case running time: O(1) item is at the root or tree is empty.
Worst case running time: O(h) where h is the height, item is at longest leaf.
"""
# Find a node with the given item, if any
node
=
self
.
_find_node_recursive
(
item
,
self
.
root
)
...
...
@@ -109,8 +111,10 @@ class BinarySearchTree(object):
def
insert
(
self
,
item
:
Any
)
->
None
:
"""Insert the given item in order into this binary search tree.
TODO: Best case running time: ??? under what conditions?
TODO: Worst case running time: ??? under what conditions?"""
Best case running time: O(1) item's position is at the root or tree is empty.
Worst case running time: O(h) where h is the height, item's position is at longest leaf.
"""
# Handle the case where the tree is empty
if
self
.
is_empty
():
self
.
root
=
BinaryTreeNode
(
item
)
...
...
@@ -135,8 +139,8 @@ class BinarySearchTree(object):
or None if the given item is not found. Search is performed iteratively
starting from the root node.
TODO:
Best case running time:
??? under what conditions?
TODO:
Worst case running time:
??? under what conditions?
Best case running time:
O(log n) item is in its children.
Worst case running time:
O(1) item is at the given node.
"""
# Start with the root node
node
=
self
.
root
...
...
@@ -161,8 +165,8 @@ class BinarySearchTree(object):
or None if the given item is not found. Search is performed recursively
starting from the given node (give the root node to start recursion).
TODO:
Best case running time:
??? under what conditions?
TODO:
Worst case running time:
??? under what conditions?
Best case running time:
O(log n) item is in its children.
Worst case running time:
O(1) item is at the given node.
"""
# Check if starting node exists
...
...
@@ -257,8 +261,10 @@ class BinarySearchTree(object):
)
->
None
:
"""Traverse this binary tree with recursive in-order traversal (DFS).
Start at the given node and visit each node with the given function.
TODO: Running time: ??? Why and under what conditions?
TODO: Memory usage: ??? Why and under what conditions?"""
Running time: O(n) every node is visited.
Memory usage: O(h) where is the height, call stack filled with height.
"""
if
node
is
None
:
return
...
...
@@ -290,8 +296,10 @@ class BinarySearchTree(object):
)
->
None
:
"""Traverse this binary tree with recursive pre-order traversal (DFS).
Start at the given node and visit each node with the given function.
TODO: Running time: ??? Why and under what conditions?
TODO: Memory usage: ??? Why and under what conditions?"""
Running time: O(n) every node is visited.
Memory usage: O(h) where is the height, call stack filled with height.
"""
if
node
is
None
:
return
...
...
@@ -323,8 +331,10 @@ class BinarySearchTree(object):
)
->
None
:
"""Traverse this binary tree with recursive post-order traversal (DFS).
Start at the given node and visit each node with the given function.
TODO: Running time: ??? Why and under what conditions?
TODO: Memory usage: ??? Why and under what conditions?"""
Running time: O(n) every node is visited.
Memory usage: O(h) where is the height, call stack filled with height.
"""
if
node
is
None
:
return
...
...
@@ -355,9 +365,11 @@ class BinarySearchTree(object):
visit
:
Visit
)
->
None
:
"""Traverse this binary tree with iterative level-order traversal (BFS).
Start at the given node and visit each node with the given function.
TODO: Running time: ??? Why and under what conditions?
TODO: Memory usage: ??? Why and under what conditions?"""
Start at the given node and visit each node with the given function.
Running time: O(n) every node is visited.
Memory usage: O(2^h) where is the height, queue is filled with nodes on a level.
"""
queue
=
Queue
()
queue
.
enqueue
(
start_node
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment