bst_linked.py - -bst_linked.py Linked version of the BST ADT-Author David Brown ID 999999999 Email [email protected]_updated = Imports from copy import

bst_linked.py - -bst_linked.py Linked version of the BST...

This preview shows page 1 - 4 out of 25 pages.

""" ------------------------------------------------------- bst_linked.py Linked version of the BST ADT. ------------------------------------------------------- Author: David Brown ID: 999999999 Email: [email protected] __updated__ = "2017-01-28" ------------------------------------------------------- """ # Imports from copy import deepcopy from stack_array import Stack class _BSTNode: def __init__(self, value): """ ------------------------------------------------------- Creates a node containing a copy of value. Use: node = _BSTNode(value) ------------------------------------------------------- Preconditions: value - data for the node (?) Postconditions: Initializes a BST node containing value. Child pointers are None, height is 1. ------------------------------------------------------- """ self._data = deepcopy(value) self._left = None self._right = None self._height = 1 return def _update_height(self): """ ------------------------------------------------------- Updates the height of the current node. Use: node._update_height() ------------------------------------------------------- Postconditions: _height is 1 plus the maximum of the node's (up to) two children. ------------------------------------------------------- """ if self._left is None: left_height = 0 else: left_height = self._left._height if self._right is None: right_height = 0 else: right_height = self._right._height self._height = max(left_height, right_height) + 1 return def __str__(self): """ USE FOR TESTING ONLY -------------------------------------------------------
Image of page 1

Subscribe to view the full document.

Returns node height and value as a string - for debugging. ------------------------------------------------------- """ return "h: {}, v: {}".format(self._height, self._data) class BST: def __init__(self): """ ------------------------------------------------------- Initializes an empty BST. Use: bst = BST() ------------------------------------------------------- Postconditions: Initializes an empty bst. ------------------------------------------------------- """ self._root = None self._count = 0 return def is_empty(self): """ ------------------------------------------------------- Determines if bst is empty. Use: b = bst.is_empty() ------------------------------------------------------- Postconditions: returns True if bst is empty, False otherwise. ------------------------------------------------------- """ return self._root is None def __len__(self): """ ------------------------------------------------------- Returns the number of nodes in the BST. Use: n = len(bst) ------------------------------------------------------- Postconditions: returns the number of nodes in the BST. ------------------------------------------------------- """ return self._count def insert(self, value): """ ------------------------------------------------------- Inserts a copy of value into the bst. Use: b = bst.insert(value) ------------------------------------------------------- Preconditions: value - data to be inserted into the bst (?) Postconditions: returns inserted - True if value is inserted into the BST, False otherwise. Values may appear only once in a tree. (boolean) ------------------------------------------------------- """ self._root, inserted = self._insert_aux(self._root, value) return inserted
Image of page 2
def _insert_aux(self, node, value): """ ------------------------------------------------------- Inserts a copy of _data into node. Private recursive operation called only by insert. Use: node, inserted = self._insert_aux(node, value) ------------------------------------------------------- Preconditions: node - a bst node (_BSTNode) value - data to be inserted into the node (?) Postconditions: returns node - the current node (_BSTNode) inserted - True if value is inserted into the BST, False otherwise. Values may appear only once in a tree. (boolean) ------------------------------------------------------- """ if node is None: # Base case: add a new node containing the value. node = _BSTNode(value) self._count += 1 inserted = True elif node._data > value: # General case: check the left subtree. node._left, inserted = self._insert_aux(node._left, value) elif node._data < value: # General case: check the right subtree. node._right, inserted = self._insert_aux(node._right, value) else: # Base case: value is already in the BST. inserted = False if inserted: # Update the node height if any of its children have been changed. node._update_height() return node, inserted def retrieve(self, key): """ ------------------------------------------------------- Retrieves a copy of a value matching key in a BST. (Iterative) Use: v = bst.retrieve(key) ------------------------------------------------------- Preconditions: key - data to search for (?) Postconditions: returns value - value in the node containing key, otherwise None (?) ------------------------------------------------------- """ assert self._root is not None, "Cannot retrieve from an empty BST" node = self._root value = None while node is not None and value is None: if node._data > key: node = node._left elif node._data < key:
Image of page 3

Subscribe to view the full document.

Image of page 4
  • Fall '13
  • sta
  • Graph Theory, Control flow, Tree structure

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes