Advertisement
Guest User

Tree

a guest
Nov 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. class Tree(object):
  2.     class Position(object):
  3.         def element(self):
  4.             """ Trả về phần tử được lưu trữ tại vị trí """
  5.             raise NotImplemented("must be Implemented by subclass")
  6.  
  7.         def __eq__(self, other):
  8.             """return T if p stays at the same position"""
  9.             raise NotImplemented("must be Implemented by subclass")
  10.  
  11.         def __ne__(self, other):
  12.             return not (self == other)
  13.  
  14.         def root(self):
  15.             raise NotImplemented("must be Implemented by subclass")
  16.  
  17.         def is_root(self, p):
  18.             """"return True if p is root, otherwise return False"""
  19.             return self.root()
  20.  
  21.         def is_leaf(self, p):
  22.             return self.num_chilren(p) == 0
  23.  
  24.         def is_empty(self):
  25.             return len(self) == 0
  26.  
  27.         def __len__(self):
  28.             raise NotImplemented("must be Implemented by subclass")
  29.  
  30.         def parent(self, p):
  31.             raise NotImplemented("must be Implemented by subclass")
  32.  
  33.         def chilren(self, p):
  34.             raise NotImplemented("must be Implemented by subclass")
  35.  
  36.         def num_chilren(self):
  37.             raise NotImplemented("must be Implemented by subclass")
  38.  
  39.         def depth(self, p):
  40.             if self.is_root(p):
  41.                 return 0
  42.             else:
  43.                 return 1 + self.depth(self.parent(p))
  44.  
  45.         def _height_recursive(self, p):
  46.  
  47.             if self.is_leaf:
  48.                 return 0
  49.             else:
  50.                 return 1 + max(self._height_recursive(c) for c in self.chilren(p))
  51.  
  52.         def height(self, p = None):
  53.  
  54.             if p is None:
  55.                 p = self.root()
  56.             return self._height_recursive(p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement