Posts

Showing posts from September, 2018

Revised Binary Tree, with the pytest file

Image
Finally went through and fixed my spaghetti monster.

I might go back and add recursions, but for the purpose of showing readers how I made the code, while loops are much easier to follow, in my opinion.

The diagram of how I decided to do delete from a parent node with two children.




Python Binary Tree:

from random import randint # used in manual tests at bottom

class BinaryTreeNode(object):

def __init__(self, key, value, left, right):
        self.key = key
        self.value = value
        self.left = left
        self.right = right

def __repr__(self):
        nleft = self.left and self.left.value or None
        nright = self.right and self.right.value or None
        return f"{nleft} <--- ( {self.key} : {self.value} ) ---> {nright}"

class BinaryTree(object):
def __init__(self, median):
        self.median = median
        self.root_node = BinaryTreeNode(self.median, 'binary_root_node', None, None)
        self._size = 0

def set(self, akey, value):
""&q…