Skip to content

Commit

Permalink
bin search tree
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Dec 19, 2019
1 parent 148c53c commit f96101a
Show file tree
Hide file tree
Showing 5 changed files with 767 additions and 58 deletions.
21 changes: 21 additions & 0 deletions exercises/trees/bin_tree_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,27 @@ def fun_rec(self):
return ret
#/jupman-raise

def bin_insert_rec(self, m):
""" Assuming the tree is a binary search tree of integer numbers,
MODIFIES the tree by inserting a new node with the value m
in the appropriate position. Node is always added as a leaf.
- MUST EXECUTE IN O(height(t))
- NOTE: with big trees a recursive solution would surely
exceed the call stack, but here we don't mind
"""
#jupman-raise
if m < self._data:
if self.left() == None:
self.insert_left(m)
else:
self._left.bin_insert_rec(m)
else:
if self.right() == None:
self.insert_right(m)
else:
self.right().bin_insert_rec(m)
#/jupman-raise

def sum_stack(self):
""" Supposing the tree holds integer numbers in all nodes,
Expand Down
Binary file added exercises/trees/img/bt-bin-search.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
316 changes: 316 additions & 0 deletions exercises/trees/img/bt-bin-search.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f96101a

Please sign in to comment.