Docstrings
You are using comments instead of doctstringsdocstrings, which is not a good tone, as I think. You can read more about docstring in PEP 257. Also it's not necessary to write class name in docstring. For example, you can write Implements the operations in Red Black Tree
instead of class RedBlackTree implements the operations in Red Black Tree
.
Type hinting
Your code doesn't have type hinting at all. It's not really a bad thing, but it's always better to do type hinting. You can read more about why you may want to do it in PEP 484 and how to do it here.
Double underscores in method names
It's not really necessary to write __
before the method name, because _
is already enough. But it's a matter of preference.
Node colors
I'd use enum instead of 0/1 for colors. Your code will be more understandable if you write self.color = Color.Red
instead of self.color = 1
.
Node.data
Data is very wide and uninformative name. If data
holds key
, why don't you name it key
?
sys.stdout
I suppose that there's no need to overcomplicate your code by using sys.stdout.write
instead of print
, which also prints to stdout.
RedBlackTree.pretty_print()
You should defenitelydefinitely read about dunders. They will allow you to integrate your class with default python methods. In this particular case dunders will allow you to use print(some_tree)
instead of some_tree.pretty_print()
.
Long methods
Some of your methods are really long and difficult to read. For example, __fix_delete
. Also comments like # case 3.1
are uninformative. I guess you should extract each case in separate function and write a little bit more about each case in docstrings.
Style comments
I think that every Python programmer should use pylint. It's a good tool to find small issues like too long lines, absence of spaces after commas, etc. It's really helpful, but try not to become pylint maniac - it's not always necessary to achieve 10/10.