Skip to content

Commit

Permalink
add is_empty check for bst
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Jan 31, 2020
1 parent 545f222 commit 6001fe1
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions algs4/bst.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def _keys(self, x, queue, lo, hi):
self._keys(x.right, queue, lo, hi)

def max(self):
if self.is_empty():
raise Exception("empty bst")
return self._max(self.root).key

def _max(self, x):
Expand All @@ -122,6 +124,8 @@ def _max(self, x):
return self._max(x.right)

def min(self):
if self.is_empty():
raise Exception("empty bst")
return self._min(self.root).key

def _min(self, x):
Expand Down Expand Up @@ -229,6 +233,10 @@ def _delete_min(self, x):
x.N = self._size(x.left) + self._size(x.right) + 1
return x

def is_empty(self):
return self.root == None


if __name__ == '__main__':
import sys

Expand Down

0 comments on commit 6001fe1

Please sign in to comment.