Skip to content

Commit

Permalink
fix typo in red black bst
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Feb 1, 2020
1 parent 6001fe1 commit 46811de
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 7 additions & 3 deletions algs4/frequency_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
"""

import sys
from algs4.sequential_search_st import SequentialSearchST
# from algs4.sequential_search_st import SequentialSearchST
#from algs4.binary_search_st import BinarySearchST
from algs4.red_black_bst import RedBlackBST

minlen = int(sys.argv[1])
st = SequentialSearchST()
# st = SequentialSearchST()
#st = BinarySearchST()
st = RedBlackBST()

for line in sys.stdin:
words = line.split()
Expand All @@ -40,7 +44,7 @@

maxstr = ""
st.put(maxstr, 0)
for word in st.keys():
for word in st.Keys():
if st.get(word) > st.get(maxstr):
maxstr = word
print(maxstr, " ", st.get(maxstr))
13 changes: 7 additions & 6 deletions algs4/red_black_bst.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def level_order(self):
queue.enqueue(x.right)
return keys

def keys(self):
def Keys(self):
"""
Returns all keys in the symbol table
To iterate over all of the keys in the symbol table named {@code st},
Expand Down Expand Up @@ -309,7 +309,7 @@ def _delete(self, h, key):
h = self.move_red_right(h)

if key == h.key:
x = self.min(h.right)
x = self._min(h.right)
h.key = x.key
h.val = x.val
h.right = self._delete_min(h.right)
Expand All @@ -325,17 +325,17 @@ def delete_min(self):
if not self.is_red(self.root.left) and not self.is_red(self.root.right):
self.root.color = RedBlackBST.RED

self.root = self._deleteMin(self.root)
if not is_empty(self.root):
self.root.color = RedBlackBST.BLACk
self.root = self._delete_min(self.root)
if not self.is_empty():
self.root.color = RedBlackBST.BLACK

def _delete_min(self, h):
if h.left is None:
return None
if not self.is_red(h.left) and not self.is_red(h.left.left):
h = self.move_red_left(h)

h.left = self._deleteMin(h.left)
h.left = self._delete_min(h.left)
return self.balance(h)

def balance(self, h):
Expand All @@ -348,6 +348,7 @@ def balance(self, h):
h.size = self._size(h.left) + self._size(h.right) + 1
return h


if __name__ == '__main__':
import sys

Expand Down
2 changes: 1 addition & 1 deletion algs4/sequential_search_st.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def delete(self, key):
prev = curr
curr = curr.next

def keys(self):
def Keys(self):
return STKeyIterator(self.first)

def is_empty(self):
Expand Down

0 comments on commit 46811de

Please sign in to comment.