lichenran1234

rope

Apr 11th, 2021 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. class RopeNode(object):
  2.     def __init__(self, chars=None, num_chars=0, parent=None):
  3.         self.left = None
  4.         self.right = None
  5.         # If leaf: number of chars
  6.         # If non-leaf: number of total chars in left sub-tree
  7.         self.num_chars = num_chars
  8.         self.chars = chars
  9.         self.parent = parent
  10.  
  11. class NewString(object):
  12.     def __init__(self, rank):
  13.         self.root = RopeNode()
  14.         self.size = 0
  15.         # All leaf nodes must have between [1, rank - 1] chars, except the root node.
  16.         self.rank = rank
  17.    
  18.     def read(self, index):
  19.         if index >= self.size:
  20.             return None
  21.        
  22.         cur_index = index
  23.         cur_node = self.root
  24.         while cur_node.left is not None:
  25.             if cur_index >= cur_node.num_chars:
  26.                 cur_node = cur_node.right
  27.                 cur_index -= cur_node.num_chars
  28.             else:
  29.                 cur_node = cur_node.left
  30.         return cur_node.chars[cur_index]
  31.    
  32.     def insert(self, char, index):
  33.         if index > self.size:
  34.             return False
  35.        
  36.         self.size += 1
  37.        
  38.         cur_index = index
  39.         cur_node = self.root
  40.         while cur_node.left is not None:
  41.             if cur_index > cur_node.num_chars:
  42.                 cur_node = cur_node.right
  43.                 cur_index -= cur_node.num_chars
  44.             else:
  45.                 cur_node.num_chars += 1
  46.                 cur_node = cur_node.left
  47.        
  48.         if cur_node.chars is None:
  49.             assert cur_node.num_chars == 0
  50.             assert cur_index == 0
  51.             cur_node.chars = []
  52.         cur_node.chars.insert(cur_index, char)
  53.         cur_node.num_chars += 1
  54.        
  55.         if cur_node.num_chars >= self.rank:
  56.             mid = cur_node.num_chars / 2
  57.             left_num_chars = mid
  58.             right_num_chars = cur_node.num_chars - mid
  59.             cur_node.left = RopeNode(cur_node.chars[:mid], left_num_chars, cur_node)
  60.             cur_node.right = RopeNode(cur_node.chars[mid:], right_num_chars, cur_node)
  61.             cur_node.num_chars = left_num_chars
  62.             cur_node.chars = None
  63.         return True
  64.    
  65.     def delete(self, index):
  66.         if index < 0 or index >= self.size:
  67.             return False
  68.        
  69.         self.size -= 1
  70.        
  71.         if self.root.left is None:
  72.             self.root.num_chars -= 1
  73.             del self.root.chars[index]
  74.             return True
  75.        
  76.         cur_index = index
  77.         cur_node = self.root
  78.         while cur_node.left is not None:
  79.             if cur_index >= cur_node.num_chars:
  80.                 cur_node = cur_node.right
  81.                 cur_index -= cur_node.num_chars
  82.             else:
  83.                 cur_node.num_chars -= 1
  84.                 cur_node = cur_node.left
  85.        
  86.         del cur_node.chars[cur_index]
  87.         cur_node.num_chars -= 1
  88.         if cur_node.num_chars == 0:
  89.             parent = cur_node.parent
  90.             sibling_node = None
  91.             if parent.left == cur_node:
  92.                 sibling_node = parent.right
  93.             else:
  94.                 sibling_node = parent.left
  95.             if parent.parent is None:
  96.                 self.root = sibling_node
  97.                 sibling_node.parent = None
  98.             else:
  99.                 grand_parent = parent.parent
  100.                 if grand_parent.left == parent:
  101.                     grand_parent.left = sibling_node
  102.                 else:
  103.                     grand_parent.right = sibling_node
  104.                 sibling_node.parent = grand_parent
  105.                
  106.         return True
Add Comment
Please, Sign In to add comment