Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _remove_aux(self, node, key):
- """
- -------------------------------------------------------
- Attempts to find a value matching key in a BST node. Deletes the node
- if found and returns the sub-tree root.
- Private recursive operation called only by remove.
- Use: node, value = self._remove_aux(node, key)
- -------------------------------------------------------
- Preconditions:
- node - a bst node to search for key (_BSTNode)
- key - data to search for (?)
- Postconditions:
- returns
- node - the current node or its replacement (_BSTNode)
- value - value in node containing key, None otherwise.
- -------------------------------------------------------
- """
- if node is None:
- value = None
- elif key < node._data:
- node._left, value = self._remove_aux(node._left, key)
- elif key > node._data:
- node._right, value = self._remove_aux(node._right, key)
- else:
- value = node._data
- if node._left is None:
- node = node._right
- elif node._right is None:
- node = node._left
- else:
- if node._left._right is None:
- repl_node = node._left
- else:
- repl_node = self._delete_node_left(
- node._left, node._left._right)
- repl_node._left = node._left
- repl_node._right = node._right
- node = repl_node
- node._update_height()
- self._count -= 1
- if node is not None and value is not None:
- node._update_height()
- return node, value
- def _delete_node_left(self, parent, child):
- """
- -------------------------------------------------------
- Finds a replacement node for a node to be removed from the tree.
- Private operation called only by _remove_aux.
- Use: repl_node = self._delete_node_left(node, node._right)
- -------------------------------------------------------
- Preconditions:
- parent - node to search for largest value (_BSTNode)
- child - the right node of parent (_BSTNode)
- Postconditions:
- returns
- repl_node - the node that replaces the deleted node. This node
- is the node with the maximum value in the deleted node's left
- subtree (_BSTNode)
- -------------------------------------------------------
- """
- if child._right is None:
- repl_node = child
- parent._right = child._left
- else:
- repl_node = self._delete_node_left(child, child._right)
- parent._update_height()
- return repl_node
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement