Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. class Node:
  2.  
  3. RED = True
  4. BLACK = False
  5.  
  6. def __init__(self, key, color=RED):
  7. if not type(color) == bool:
  8. raise TypeError('Bad value for color parameter, expected True/False but given %s'% color)
  9. self.color = color
  10. self.key = key
  11. self.left = self.right = self.parent = NilNode.instance()
  12.  
  13. def __str__(self, level=0, indent=' '):
  14. s = level * indent + str(self.key)
  15. if self.left:
  16. s = s + '\n' + self.left.__str__(level + 1, indent)
  17. if self.right:
  18. s = s + '\n' + self.right.__str__(level + 1, indent)
  19. return s
  20.  
  21. def __nonzero__(self):
  22. return True
  23.  
  24. def __bool__(self):
  25. return True
  26.  
  27.  
  28. class NilNode(Node):
  29.  
  30. __instance__ = None
  31.  
  32. @classmethod
  33. def instance(self):
  34. if self.__instance__ is None:
  35. self.__instance__ = NilNode()
  36. return self.__instance__
  37.  
  38. def __init__(self):
  39. self.color = Node.BLACK
  40. self.key = None
  41. self.left = self.right = self.parent = None
  42.  
  43. def __nonzero__(self):
  44. return False
  45.  
  46. def __bool__(self):
  47. return False
  48.  
  49.  
  50. class RedBlackTree:
  51.  
  52. def __init__(self):
  53. self.root = NilNode.instance()
  54. self.size = 0
  55.  
  56. def __str__(self):
  57. return '(root.size = %d)\n' % self.size + str(self.root)
  58.  
  59. def add(self, key):
  60. self.insert(Node(key))
  61.  
  62. def insert(self, x):
  63. self.__insert_helper(x)
  64.  
  65. x.color = Node.RED
  66. while x != self.root and x.parent.color == Node.RED:
  67. if x.parent == x.parent.parent.left:
  68. y = x.parent.parent.right
  69. if y and y.color == Node.RED:
  70. x.parent.color = Node.BLACK
  71. y.color = Node.BLACK
  72. x.parent.parent.color = Node.RED
  73. x = x.parent.parent
  74. else:
  75. if x == x.parent.right:
  76. x = x.parent
  77. self.__left_rotate(x)
  78. x.parent.color = Node.BLACK
  79. x.parent.parent.color = Node.RED
  80. self.__right_rotate(x.parent.parent)
  81. else:
  82. y = x.parent.parent.left
  83. if y and y.color == Node.RED:
  84. x.parent.color = Node.BLACK
  85. y.color = Node.BLACK
  86. x.parent.parent.color = Node.RED
  87. x = x.parent.parent
  88. else:
  89. if x == x.parent.left:
  90. x = x.parent
  91. self.__right_rotate(x)
  92. x.parent.color = Node.BLACK
  93. x.parent.parent.color = Node.RED
  94. self.__left_rotate(x.parent.parent)
  95. self.root.color = Node.BLACK
  96.  
  97. def delete(self, z):
  98. if not z.left or not z.right:
  99. y = z
  100. else:
  101. y = self.successor(z)
  102. if not y.left:
  103. x = y.right
  104. else:
  105. x = y.left
  106. x.parent = y.parent
  107.  
  108. if not y.parent:
  109. self.root = x
  110. else:
  111. if y == y.parent.left:
  112. y.parent.left = x
  113. else:
  114. y.parent.right = x
  115.  
  116. if y != z:
  117. z.key = y.key
  118.  
  119. if y.color == Node.BLACK:
  120. self.__delete_fixup(x)
  121.  
  122. self.size -= 1
  123. return y
  124.  
  125. def minimum(self, x=None):
  126. if x is None:
  127. x = self.root
  128. while x.left:
  129. x = x.left
  130. return x
  131.  
  132. def maximum(self, x=None):
  133. if x is None:
  134. x = self.root
  135. while x.right:
  136. x = x.right
  137. return x
  138.  
  139. def successor(self, x):
  140. if x.right:
  141. return self.minimum(x.right)
  142. y = x.parent
  143. while y and x == y.right:
  144. x = y
  145. y = y.parent
  146. return y
  147.  
  148. def predecessor(self, x):
  149. if x.left:
  150. return self.maximum(x.left)
  151. y = x.parent
  152. while y and x == y.left:
  153. x = y
  154. y = y.parent
  155. return y
  156.  
  157. def inorder_walk(self, x=None):
  158. if x is None:
  159. x = self.root
  160. x = self.minimum()
  161. while x:
  162. yield x.key
  163. x = self.successor(x)
  164.  
  165. def reverse_inorder_walk(self, x=None):
  166. if x is None:
  167. x = self.root
  168. x = self.maximum()
  169. while x:
  170. yield x.key
  171. x = self.predecessor(x)
  172.  
  173. def search(self, key, x=None):
  174. if x is None:
  175. x = self.root
  176. while x and x.key != key:
  177. if key < x.key:
  178. x = x.left
  179. else:
  180. x = x.right
  181. return x
  182.  
  183. def is_empty(self):
  184. return bool(self.root)
  185.  
  186. def black_height(self, x=None):
  187. if x is None:
  188. x = self.root
  189. height = 0
  190. while x:
  191. x = x.left
  192. if not x or x.is_black():
  193. height += 1
  194. return height
  195.  
  196. def __left_rotate(self, x):
  197. if not x.right:
  198. raise 'x.right is nil!'
  199. y = x.right
  200. x.right = y.left
  201. if y.left:
  202. y.left.parent = x
  203. y.parent = x.parent
  204. if not x.parent:
  205. self.root = y
  206. else:
  207. if x == x.parent.left:
  208. x.parent.left = y
  209. else:
  210. x.parent.right = y
  211. y.left = x
  212. x.parent = y
  213.  
  214. def __right_rotate(self, x):
  215. if not x.left:
  216. raise 'x.left is nil!'
  217. y = x.left
  218. x.left = y.right
  219. if y.right:
  220. y.right.parent = x
  221. y.parent = x.parent
  222. if not x.parent:
  223. self.root = y
  224. else:
  225. if x == x.parent.left:
  226. x.parent.left = y
  227. else:
  228. x.parent.right = y
  229. y.right = x
  230. x.parent = y
  231.  
  232. def __insert_helper(self, z):
  233. y = NilNode.instance()
  234. x = self.root
  235. while x:
  236. y = x
  237. if det(x.key, x.key.other_end, z.key) < 0:
  238. x = x.left
  239. else:
  240. x = x.right
  241.  
  242. z.parent = y
  243. if not y:
  244. self.root = z
  245. else:
  246. if det(y.key, y.key.other_end, z.key) < 0:
  247. y.left = z
  248. else:
  249. y.right = z
  250.  
  251. self.size += 1
  252.  
  253. def __delete_fixup(self, x):
  254. while x != self.root and x.color == Node.BLACK:
  255. if x == x.parent.left:
  256. w = x.parent.right
  257. if w.color == Node.RED:
  258. w.color = Node.BLACK
  259. x.parent.color = Node.RED
  260. self.__left_rotate(x.parent)
  261. w = x.parent.right
  262. if w.left.color == Node.BLACK and w.right.color \
  263. == Node.BLACK:
  264. w.color = Node.RED
  265. x = x.parent
  266. else:
  267. if w.right.color == Node.BLACK:
  268. w.left.color = Node.BLACK
  269. w.color = Node.RED
  270. self.__right_rotate(w)
  271. w = x.parent.right
  272. w.color = x.parent.color
  273. x.parent.color = Node.BLACK
  274. w.right.color = Node.BLACK
  275. self.__left_rotate(x.parent)
  276. x = self.root
  277. else:
  278. w = x.parent.left
  279. if w.color == Node.RED:
  280. w.color = Node.BLACK
  281. x.parent.color = Node.RED
  282. self.__right_rotate(x.parent)
  283. w = x.parent.left
  284. if w.right.color == Node.BLACK and w.left.color \
  285. == Node.BLACK:
  286. w.color = Node.RED
  287. x = x.parent
  288. else:
  289. if w.left.color == Node.BLACK:
  290. w.right.color = Node.BLACK
  291. w.color = Node.RED
  292. self.__left_rotate(w)
  293. w = x.parent.left
  294. w.color = x.parent.color
  295. x.parent.color = Node.BLACK
  296. w.left.color = Node.BLACK
  297. self.__right_rotate(x.parent)
  298. x = self.root
  299. x.color = Node.BLACK
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement