
'''simple docstring'''
from __future__ import annotations

import math
import random
from typing import Any






class 	snake_case__       :







		"""simple docstring"""

		def __init__(      self				:			str      )						->			None:






				"""simple docstring"""







				snake_case  :       list[Any]     			=						[]
				snake_case  :       int     			=						0
				snake_case  :       int     			=						0



		def       lowerCAmelCase				(      self				:			List[Any]      )						->			bool:






				"""simple docstring"""







				return self.head == self.tail



		def       lowerCAmelCase				(      self				:			Optional[Any]  ,	UpperCamelCase__				:			Any      )						->			None:






				"""simple docstring"""







				self.data.append(UpperCamelCase__      )
				snake_case  :       Optional[int]     			=						self.tail + 1



		def       lowerCAmelCase				(      self				:			Union[str, Any]      )						->			Any:






				"""simple docstring"""







				snake_case  :       Tuple     			=						self.data[self.head]
				snake_case  :       Tuple     			=						self.head + 1
				return ret



		def       lowerCAmelCase				(      self				:			Any      )						->			int:






				"""simple docstring"""







				return self.tail - self.head



		def       lowerCAmelCase				(      self				:			Dict      )						->			None:






				"""simple docstring"""







				print(self.data      )
				print('''**************'''      )
				print(self.data[self.head : self.tail]      )







class 	snake_case__       :







		"""simple docstring"""

		def __init__(      self				:			Optional[int]  ,	UpperCamelCase__				:			Any      )						->			None:






				"""simple docstring"""







				snake_case  :       List[Any]     			=						data
				snake_case  :       MyNode | None     			=						None
				snake_case  :       MyNode | None     			=						None
				snake_case  :       int     			=						1



		def       lowerCAmelCase				(      self				:			List[str]      )						->			Any:






				"""simple docstring"""







				return self.data



		def       lowerCAmelCase				(      self				:			Tuple      )						->			MyNode | None:






				"""simple docstring"""







				return self.left



		def       lowerCAmelCase				(      self				:			Union[str, Any]      )						->			MyNode | None:






				"""simple docstring"""







				return self.right



		def       lowerCAmelCase				(      self				:			int      )						->			int:






				"""simple docstring"""







				return self.height



		def       lowerCAmelCase				(      self				:			str  ,	UpperCamelCase__				:			Any      )						->			None:






				"""simple docstring"""







				snake_case  :       List[str]     			=						data



		def       lowerCAmelCase				(      self				:			Any  ,	UpperCamelCase__				:			MyNode | None      )						->			None:






				"""simple docstring"""







				snake_case  :       Optional[int]     			=						node



		def       lowerCAmelCase				(      self				:			List[str]  ,	UpperCamelCase__				:			MyNode | None      )						->			None:






				"""simple docstring"""







				snake_case  :       List[str]     			=						node



		def       lowerCAmelCase				(      self				:			str  ,	UpperCamelCase__				:			int      )						->			None:






				"""simple docstring"""







				snake_case  :       str     			=						height







def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							int:


		'''simple docstring'''


		if node is None:
				return 0
		return node.get_height()




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)  ->							int:


		'''simple docstring'''


		if a > b:
				return a
		return b




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							MyNode:


		'''simple docstring'''


		print('''left rotation node:''' ,       node.get_data()			)
		snake_case  :       Union[str, Any]     			=						node.get_left()
		assert ret is not None
		node.set_left(ret.get_right()			)
		ret.set_right(SCREAMING_SNAKE_CASE__			)
		snake_case  :       Any     			=						my_max(get_height(node.get_right()			) ,       get_height(node.get_left()			)			) + 1
		node.set_height(SCREAMING_SNAKE_CASE__			)
		snake_case  :       Any     			=						my_max(get_height(ret.get_right()			) ,       get_height(ret.get_left()			)			) + 1
		ret.set_height(SCREAMING_SNAKE_CASE__			)
		return ret




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							MyNode:


		'''simple docstring'''


		print('''right rotation node:''' ,       node.get_data()			)
		snake_case  :       Union[str, Any]     			=						node.get_right()
		assert ret is not None
		node.set_right(ret.get_left()			)
		ret.set_left(SCREAMING_SNAKE_CASE__			)
		snake_case  :       str     			=						my_max(get_height(node.get_right()			) ,       get_height(node.get_left()			)			) + 1
		node.set_height(SCREAMING_SNAKE_CASE__			)
		snake_case  :       int     			=						my_max(get_height(ret.get_right()			) ,       get_height(ret.get_left()			)			) + 1
		ret.set_height(SCREAMING_SNAKE_CASE__			)
		return ret




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							MyNode:


		'''simple docstring'''


		snake_case  :       List[str]     			=						node.get_left()
		assert left_child is not None
		node.set_left(left_rotation(SCREAMING_SNAKE_CASE__			)			)
		return right_rotation(SCREAMING_SNAKE_CASE__			)




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							MyNode:


		'''simple docstring'''


		snake_case  :       List[str]     			=						node.get_right()
		assert right_child is not None
		node.set_right(right_rotation(SCREAMING_SNAKE_CASE__			)			)
		return left_rotation(SCREAMING_SNAKE_CASE__			)




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)  ->							MyNode | None:


		'''simple docstring'''


		if node is None:
				return MyNode(SCREAMING_SNAKE_CASE__			)
		if data < node.get_data():
				node.set_left(insert_node(node.get_left() ,       SCREAMING_SNAKE_CASE__			)			)
				if (
				    get_height(node.get_left()			) - get_height(node.get_right()			) == 2
				):  # an unbalance detected
						snake_case  :       Optional[Any]     			=						node.get_left()
						assert left_child is not None
						if (
						    data < left_child.get_data()
						):  # new node is the left child of the left child
								snake_case  :       int     			=						right_rotation(SCREAMING_SNAKE_CASE__			)
						else:
								snake_case  :       Tuple     			=						lr_rotation(SCREAMING_SNAKE_CASE__			)
		else:
				node.set_right(insert_node(node.get_right() ,       SCREAMING_SNAKE_CASE__			)			)
				if get_height(node.get_right()			) - get_height(node.get_left()			) == 2:
						snake_case  :       Union[str, Any]     			=						node.get_right()
						assert right_child is not None
						if data < right_child.get_data():
								snake_case  :       Optional[int]     			=						rl_rotation(SCREAMING_SNAKE_CASE__			)
						else:
								snake_case  :       Tuple     			=						left_rotation(SCREAMING_SNAKE_CASE__			)
		snake_case  :       Union[str, Any]     			=						my_max(get_height(node.get_right()			) ,       get_height(node.get_left()			)			) + 1
		node.set_height(SCREAMING_SNAKE_CASE__			)
		return node




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							Any:


		'''simple docstring'''


		while True:
				snake_case  :       Tuple     			=						root.get_right()
				if right_child is None:
						break
				snake_case  :       Dict     			=						right_child
		return root.get_data()




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							Any:


		'''simple docstring'''


		while True:
				snake_case  :       str     			=						root.get_left()
				if left_child is None:
						break
				snake_case  :       Optional[Any]     			=						left_child
		return root.get_data()




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)  ->							MyNode | None:


		'''simple docstring'''


		snake_case  :       List[Any]     			=						root.get_left()
		snake_case  :       Any     			=						root.get_right()
		if root.get_data() == data:
				if left_child is not None and right_child is not None:
						snake_case  :       Any     			=						get_left_most(SCREAMING_SNAKE_CASE__			)
						root.set_data(SCREAMING_SNAKE_CASE__			)
						root.set_right(del_node(SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)			)
				elif left_child is not None:
						snake_case  :       Union[str, Any]     			=						left_child
				elif right_child is not None:
						snake_case  :       List[str]     			=						right_child
				else:
						return None
		elif root.get_data() > data:
				if left_child is None:
						print('''No such data'''			)
						return root
				else:
						root.set_left(del_node(SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)			)
		else:  # root.get_data() < data
				if right_child is None:
						return root
				else:
						root.set_right(del_node(SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)			)

		if get_height(SCREAMING_SNAKE_CASE__			) - get_height(SCREAMING_SNAKE_CASE__			) == 2:
				assert right_child is not None
				if get_height(right_child.get_right()			) > get_height(right_child.get_left()			):
						snake_case  :       Optional[int]     			=						left_rotation(SCREAMING_SNAKE_CASE__			)
				else:
						snake_case  :       Optional[Any]     			=						rl_rotation(SCREAMING_SNAKE_CASE__			)
		elif get_height(SCREAMING_SNAKE_CASE__			) - get_height(SCREAMING_SNAKE_CASE__			) == -2:
				assert left_child is not None
				if get_height(left_child.get_left()			) > get_height(left_child.get_right()			):
						snake_case  :       List[Any]     			=						right_rotation(SCREAMING_SNAKE_CASE__			)
				else:
						snake_case  :       Union[str, Any]     			=						lr_rotation(SCREAMING_SNAKE_CASE__			)
		snake_case  :       Tuple     			=						my_max(get_height(root.get_right()			) ,       get_height(root.get_left()			)			) + 1
		root.set_height(SCREAMING_SNAKE_CASE__			)
		return root






class 	snake_case__       :







		"""simple docstring"""

		def __init__(      self				:			Any      )						->			None:






				"""simple docstring"""







				snake_case  :       MyNode | None     			=						None



		def       lowerCAmelCase				(      self				:			Any      )						->			int:






				"""simple docstring"""







				return get_height(self.root      )



		def       lowerCAmelCase				(      self				:			str  ,	UpperCamelCase__				:			Any      )						->			None:






				"""simple docstring"""







				print('''insert:''' + str(UpperCamelCase__      )      )
				snake_case  :       Dict     			=						insert_node(self.root  ,	UpperCamelCase__      )



		def       lowerCAmelCase				(      self				:			Optional[int]  ,	UpperCamelCase__				:			Any      )						->			None:






				"""simple docstring"""







				print('''delete:''' + str(UpperCamelCase__      )      )
				if self.root is None:
						print('''Tree is empty!'''      )
						return
				snake_case  :       List[Any]     			=						del_node(self.root  ,	UpperCamelCase__      )



		def __str__(      self				:			Dict  ,	)						->			str:  # a level traversale, gives a more intuitive look on the tree






				"""simple docstring"""







				snake_case  :       Optional[int]     			=						''''''
				snake_case  :       Dict     			=						MyQueue()
				q.push(self.root      )
				snake_case  :       List[Any]     			=						self.get_height()
				if layer == 0:
						return output
				snake_case  :       List[str]     			=						0
				while not q.is_empty():
						snake_case  :       Any     			=						q.pop()
						snake_case  :       int     			=						''' ''' * int(math.pow(2  ,	layer - 1      )      )
						output += space
						if node is None:
								output += "*"
								q.push(UpperCamelCase__      )
								q.push(UpperCamelCase__      )
						else:
								output += str(node.get_data()      )
								q.push(node.get_left()      )
								q.push(node.get_right()      )
						output += space
						snake_case  :       List[Any]     			=						cnt + 1
						for i in range(100      ):
								if cnt == math.pow(2  ,	UpperCamelCase__      ) - 1:
										snake_case  :       Optional[int]     			=						layer - 1
										if layer == 0:
												output += "\n*************************************"
												return output
										output += "\n"
										break
				output += "\n*************************************"
				return output







def    _UpperCamelCase						(       )  ->							None:


		'''simple docstring'''


		import doctest

		doctest.testmod()


if __name__ == "__main__":
						_test()
						lowercase__						       =      AVLtree()
						lowercase__						       =      list(range(1_0))
						random.shuffle(lst)
						for i in lst:
												t.insert(i)
												print(str(t))
						random.shuffle(lst)
						for i in lst:
												t.del_node(i)
												print(str(t))




'''simple docstring'''
from __future__ import annotations

from PIL import Image

# Define glider example
lowercase__						       =      [
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
lowercase__						       =      [[0, 1, 0], [0, 1, 0], [0, 1, 0]]




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__			)  ->							list[list[int]]:


		'''simple docstring'''


		snake_case  :       Optional[Any]     			=						[]
		for i in range(len(SCREAMING_SNAKE_CASE__			)			):
				snake_case  :       str     			=						[]
				for j in range(len(cells[i]			)			):
						# Get the number of live neighbours
						snake_case  :       Any     			=						0
						if i > 0 and j > 0:
								neighbour_count += cells[i - 1][j - 1]
						if i > 0:
								neighbour_count += cells[i - 1][j]
						if i > 0 and j < len(cells[i]			) - 1:
								neighbour_count += cells[i - 1][j + 1]
						if j > 0:
								neighbour_count += cells[i][j - 1]
						if j < len(cells[i]			) - 1:
								neighbour_count += cells[i][j + 1]
						if i < len(SCREAMING_SNAKE_CASE__			) - 1 and j > 0:
								neighbour_count += cells[i + 1][j - 1]
						if i < len(SCREAMING_SNAKE_CASE__			) - 1:
								neighbour_count += cells[i + 1][j]
						if i < len(SCREAMING_SNAKE_CASE__			) - 1 and j < len(cells[i]			) - 1:
								neighbour_count += cells[i + 1][j + 1]

						# Rules of the game of life (excerpt from Wikipedia):
						# 1. Any live cell with two or three live neighbours survives.
						# 2. Any dead cell with three live neighbours becomes a live cell.
						# 3. All other live cells die in the next generation.
						#    Similarly, all other dead cells stay dead.
						snake_case  :       List[Any]     			=						cells[i][j] == 1
						if (
						    (alive and 2 <= neighbour_count <= 3)
						    or not alive
						    and neighbour_count == 3
						):
								next_generation_row.append(1			)
						else:
								next_generation_row.append(0			)

				next_generation.append(SCREAMING_SNAKE_CASE__			)
		return next_generation




def    _UpperCamelCase						(       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__			)  ->							list[Image.Image]:


		'''simple docstring'''


		snake_case  :       Tuple     			=						[]
		for _ in range(SCREAMING_SNAKE_CASE__			):
				# Create output image
				snake_case  :       List[Any]     			=						Image.new('''RGB''' ,       (len(cells[0]			), len(SCREAMING_SNAKE_CASE__			))			)
				snake_case  :       Optional[Any]     			=						img.load()

				# Save cells to image
				for x in range(len(SCREAMING_SNAKE_CASE__			)			):
						for y in range(len(cells[0]			)			):
								snake_case  :       str     			=						255 - cells[y][x] * 255
								snake_case  :       List[str]     			=						(colour, colour, colour)

        # Save image
				images.append(SCREAMING_SNAKE_CASE__			)
				snake_case  :       Dict     			=						new_generation(SCREAMING_SNAKE_CASE__			)
		return images


if __name__ == "__main__":
						lowercase__						       =      generate_images(GLIDER, 1_6)
						images[0].save("out.gif", save_all=True, append_images=images[1:])


