#!/usr/bin/env python3 """ binary_search_tree.py """ class BinarySearchTree(object): def __init__(self): """Creates a tree with an empty root reference and a size of 0""" pass def length(self): """Returns the size of the tree (how many nodes)""" pass def __len__(self): """Also returns the size of the tree. Allows us to use the len() method in Python.""" pass def __iter__(self): """Allows us to iterate through the binary tree. ?!""" return self.root.__iter__() def put(self, key, val): """Puts a key-value pair into the binary tree at the correct location. Makes use of the recursive _put() helper function. """ pass def _put(self, key, val, current_node): """This recursive method takes the key-value pair and the currentNode, and works its way down through the tree until we find the correct location to put a TreeNode with the key-value. Note the _ in front of the method name. This is a "code" for indicating that this method is meant for internal use (by the class itself) only, and not used by any external callers. """ pass def get(self, key): """This tries to find the TreeNode associated with a given key. If we have a root value, call the recursive _get() helper function and store the resulting value in res for return. """ pass def _get(self, key, current_node): """Another beautiful recursive method that digs in to find a TreeNode with the given key. Note the _ in front of the method name. This is a "code" for indicating that this method is meant for internal use (by the class itself) only, and not used by any external callers. """ pass class TreeNode(object): """This class, as written, uses the fact that the Python value None has a Boolean value of False when used in boolean expressions. """ def __init__(self,key,val,left=None,right=None, parent=None): pass def has_left_child(self): """Returns False if no left child, otherwise, returns a reference to the left_child """ pass def has_right_child(self): pass def is_left_child(self): """Returns True if this node is the left child of a parent """ pass def is_right_child(self): pass def is_root(self): """Return true if this is the root node """ pass def is_leaf(self): """Return true if we're a leaf node """ pass def has_any_children(self): """Return true if at least one child exists """ pass def has_both_children(self): """Return true if both children exist""" pass def replace_node_data(self, key, value, lc, rc): """Replaces the node data for a specific node""" self.key = key self.payload = value self.left_child = lc self.right_child = rc if self.has_left_child(): self.left_child.parent = self if self.has_right_child(): self.right_child.parent = self def __iter__(self): """Recursive function that allows us to iterate through the BST keys using a loop! What?!! """ if self: if self.has_left_child(): for elem in self.left: yield elem yield self.key if self.has_right_child(): for elem in self.right: yield elem def __repr__(self): return "TreeNode[key=" + str(self.key) \ + ",value=" + str(self.value) \ + ",left_child=" + str(self.left_child) \ + ",right_child=" + str(self.right_child) + "]" def main(): bst = BinarySearchTree() if __name__ == "__main__": main()