Advertisement
Nyktos

e4c

Apr 18th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. class BTNode(object):
  2.     """A node in a binary tree."""
  3.  
  4.     def __init__(self, item, left=None, right=None):
  5.         """(BTNode, object, BTNode, BTNode) -> NoneType
  6.        Initialize this node to store item and have children left and right,
  7.        as well as depth 0.
  8.        """
  9.         self.item = item
  10.         self.left = left
  11.         self.right = right
  12.         self.depth = 0  # the depth of this node in a tree
  13.  
  14.     def __str__(self):
  15.         """(BTNode) -> str
  16.        Return a "sideways" representation of the subtree rooted at thisnode,
  17.        with right subtrees above parents above left subtrees and each node on
  18.        its own line, preceded by as many TAB characters as the node's depth.
  19.        """
  20.         # If there is a right subtree, add an extra TAB character in front of
  21.         # every node in its string representation, with an extra newline at the
  22.         # end (ready to be concatenated with self.item).
  23.         if self.right:
  24.             str_right = "\t" + str(self.right).replace("\n", "\n\t") + "\n"
  25.         else:
  26.             str_right = ""
  27.             # The string representation of self.item: if item is a string, use
  28.             # repr(item) so that quotes are included and special characters are
  29.             # treated correctly -- just like in a list; else use str(item).
  30.         if isinstance(self.item, str):
  31.             str_item = repr(self.item)
  32.         else:
  33.             str_item = str(self.item)
  34.             # If there is a left subtree, add an extra TAB character in front of
  35.             # every node in its string representation, with an extra newline at the
  36.             # beginning (ready to be concatenated with self.item).
  37.         if self.left:
  38.             str_left = "\n\t" + str(self.left).replace("\n", "\n\t")
  39.         else:
  40.             str_left = ""
  41.         # Put the right subtree above the root above the left subtree.
  42.         return str_right + str_item + str_left
  43.  
  44.     def sum_to_deepest(self):
  45.         """(BTNode) -> int
  46.        Return the sum of the keys on a deepest path from this node to a
  47.        leaf. Return the maximum sum if many leaves have the same depth.
  48.        """
  49.         return self._depth_and_sum(0, 0)[1]
  50.  
  51.     def _depth_and_sum(self, self_depth, sum_so_far):
  52.         #self_depth is the depth of this node. sum_so_far is the sum of
  53.         #items above this node. Return a tuple of: the depth of the
  54.         #deepest leaf in this node's subtree, and the sum of items down
  55.         #to that node.
  56.         if self.left is None and self.right is None:
  57.             return self_depth, sum_so_far + self.item
  58.         elif self.left is None:
  59.             return self.right._depth_and_sum(self_depth + 1,
  60.                                              sum_so_far + self.item)
  61.         elif self.right is None:
  62.             return self.left._depth_and_sum(self_depth + 1,
  63.                                             sum_so_far + self.item)
  64.         else:
  65.             #When comparing tuples, max will first look at the first
  66.             #element (the depth), and then the second (the sum). Thus it
  67.             #will return the largest of all sums to elements tied for
  68.             #deepest.
  69.             return max(self.left._depth_and_sum(self_depth + 1,
  70.                                                 sum_so_far + self.item),
  71.                        self.right._depth_and_sum(self_depth + 1,
  72.                                                  sum_so_far + self.item))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement