2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-05-25 22:20:37 +00:00
|
|
|
from random import random
|
|
|
|
|
|
|
|
|
2020-01-03 14:25:36 +00:00
|
|
|
class Node:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
Treap's node
|
2019-10-18 07:39:37 +00:00
|
|
|
Treap is a binary tree by value and heap by priority
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def __init__(self, value: int | None = None):
|
2019-10-18 07:39:37 +00:00
|
|
|
self.value = value
|
2019-05-25 22:20:37 +00:00
|
|
|
self.prior = random()
|
2021-09-07 11:37:03 +00:00
|
|
|
self.left: Node | None = None
|
|
|
|
self.right: Node | None = None
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2021-02-04 16:59:38 +00:00
|
|
|
def __repr__(self) -> str:
|
2019-10-18 07:39:37 +00:00
|
|
|
from pprint import pformat
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
if self.left is None and self.right is None:
|
2020-01-03 14:25:36 +00:00
|
|
|
return f"'{self.value}: {self.prior:.5}'"
|
2019-10-18 07:39:37 +00:00
|
|
|
else:
|
|
|
|
return pformat(
|
2020-01-18 12:24:33 +00:00
|
|
|
{f"{self.value}: {self.prior:.5}": (self.left, self.right)}, indent=1
|
2019-10-18 07:39:37 +00:00
|
|
|
)
|
|
|
|
|
2021-02-04 16:59:38 +00:00
|
|
|
def __str__(self) -> str:
|
2019-10-18 07:39:37 +00:00
|
|
|
value = str(self.value) + " "
|
|
|
|
left = str(self.left or "")
|
|
|
|
right = str(self.right or "")
|
|
|
|
return value + left + right
|
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
2019-10-18 07:39:37 +00:00
|
|
|
We split current tree into 2 trees with value:
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
Left tree contains all values less than split value.
|
|
|
|
Right tree contains all values greater or equal, than split value
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
if root is None: # None tree is split into 2 Nones
|
2021-02-04 16:59:38 +00:00
|
|
|
return None, None
|
2019-10-18 07:39:37 +00:00
|
|
|
elif root.value is None:
|
2021-02-04 16:59:38 +00:00
|
|
|
return None, None
|
2019-05-25 22:20:37 +00:00
|
|
|
else:
|
2019-10-18 07:39:37 +00:00
|
|
|
if value < root.value:
|
|
|
|
"""
|
|
|
|
Right tree's root will be current node.
|
|
|
|
Now we split(with the same value) current node's left son
|
|
|
|
Left tree: left part of that split
|
|
|
|
Right tree's left son: right part of that split
|
|
|
|
"""
|
|
|
|
left, root.left = split(root.left, value)
|
2021-02-04 16:59:38 +00:00
|
|
|
return left, root
|
2019-10-18 07:39:37 +00:00
|
|
|
else:
|
|
|
|
"""
|
|
|
|
Just symmetric to previous case
|
|
|
|
"""
|
|
|
|
root.right, right = split(root.right, value)
|
2021-02-04 16:59:38 +00:00
|
|
|
return root, right
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def merge(left: Node | None, right: Node | None) -> Node | None:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
We merge 2 trees into one.
|
2019-10-18 07:39:37 +00:00
|
|
|
Note: all left tree's values must be less than all right tree's
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
2019-10-22 17:13:48 +00:00
|
|
|
if (not left) or (not right): # If one node is None, return the other
|
2019-05-25 22:20:37 +00:00
|
|
|
return left or right
|
2019-10-18 07:39:37 +00:00
|
|
|
elif left.prior < right.prior:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
Left will be root because it has more priority
|
|
|
|
Now we need to merge left's right son and right tree
|
|
|
|
"""
|
2019-10-18 07:39:37 +00:00
|
|
|
left.right = merge(left.right, right)
|
2019-05-25 22:20:37 +00:00
|
|
|
return left
|
|
|
|
else:
|
|
|
|
"""
|
|
|
|
Symmetric as well
|
|
|
|
"""
|
2019-10-18 07:39:37 +00:00
|
|
|
right.left = merge(left, right.left)
|
2019-05-25 22:20:37 +00:00
|
|
|
return right
|
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def insert(root: Node | None, value: int) -> Node | None:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
Insert element
|
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
Split current tree with a value into left, right,
|
2019-05-25 22:20:37 +00:00
|
|
|
Insert new node into the middle
|
2019-10-18 07:39:37 +00:00
|
|
|
Merge left, node, right into root
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
2019-10-18 07:39:37 +00:00
|
|
|
node = Node(value)
|
|
|
|
left, right = split(root, value)
|
|
|
|
return merge(merge(left, node), right)
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def erase(root: Node | None, value: int) -> Node | None:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
Erase element
|
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
Split all nodes with values less into left,
|
|
|
|
Split all nodes with values greater into right.
|
|
|
|
Merge left, right
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
2019-10-22 17:13:48 +00:00
|
|
|
left, right = split(root, value - 1)
|
2019-10-18 07:39:37 +00:00
|
|
|
_, right = split(right, value)
|
|
|
|
return merge(left, right)
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def inorder(root: Node | None) -> None:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
Just recursive print of a tree
|
|
|
|
"""
|
2019-10-22 17:13:48 +00:00
|
|
|
if not root: # None
|
2019-05-25 22:20:37 +00:00
|
|
|
return
|
2019-10-18 07:39:37 +00:00
|
|
|
else:
|
|
|
|
inorder(root.left)
|
2020-09-10 08:31:26 +00:00
|
|
|
print(root.value, end=",")
|
2019-10-18 07:39:37 +00:00
|
|
|
inorder(root.right)
|
2019-05-25 22:20:37 +00:00
|
|
|
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def interact_treap(root: Node | None, args: str) -> Node | None:
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
|
|
|
Commands:
|
2019-10-18 07:39:37 +00:00
|
|
|
+ value to add value into treap
|
|
|
|
- value to erase all nodes with value
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> root = interact_treap(None, "+1")
|
2019-10-18 07:39:37 +00:00
|
|
|
>>> inorder(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
1,
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> root = interact_treap(root, "+3 +5 +17 +19 +2 +16 +4 +0")
|
2019-10-18 07:39:37 +00:00
|
|
|
>>> inorder(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
0,1,2,3,4,5,16,17,19,
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> root = interact_treap(root, "+4 +4 +4")
|
2019-10-18 07:39:37 +00:00
|
|
|
>>> inorder(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
0,1,2,3,4,4,4,4,5,16,17,19,
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> root = interact_treap(root, "-0")
|
2019-10-18 07:39:37 +00:00
|
|
|
>>> inorder(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
1,2,3,4,4,4,4,5,16,17,19,
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> root = interact_treap(root, "-4")
|
2019-10-18 07:39:37 +00:00
|
|
|
>>> inorder(root)
|
2020-09-10 08:31:26 +00:00
|
|
|
1,2,3,5,16,17,19,
|
2022-10-12 22:54:20 +00:00
|
|
|
>>> root = interact_treap(root, "=0")
|
2019-10-18 07:39:37 +00:00
|
|
|
Unknown command
|
2019-05-25 22:20:37 +00:00
|
|
|
"""
|
2019-10-18 07:39:37 +00:00
|
|
|
for arg in args.split():
|
|
|
|
if arg[0] == "+":
|
|
|
|
root = insert(root, int(arg[1:]))
|
|
|
|
|
|
|
|
elif arg[0] == "-":
|
|
|
|
root = erase(root, int(arg[1:]))
|
|
|
|
|
2019-05-25 22:20:37 +00:00
|
|
|
else:
|
|
|
|
print("Unknown command")
|
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
return root
|
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2021-02-04 16:59:38 +00:00
|
|
|
def main() -> None:
|
2019-10-18 07:39:37 +00:00
|
|
|
"""After each command, program prints treap"""
|
|
|
|
root = None
|
2019-10-22 17:13:48 +00:00
|
|
|
print(
|
2020-05-22 06:10:11 +00:00
|
|
|
"enter numbers to create a tree, + value to add value into treap, "
|
|
|
|
"- value to erase all nodes with value. 'q' to quit. "
|
2019-10-22 17:13:48 +00:00
|
|
|
)
|
2019-10-18 07:39:37 +00:00
|
|
|
|
|
|
|
args = input()
|
2019-10-22 17:13:48 +00:00
|
|
|
while args != "q":
|
2022-10-12 22:54:20 +00:00
|
|
|
root = interact_treap(root, args)
|
2019-10-18 07:39:37 +00:00
|
|
|
print(root)
|
|
|
|
args = input()
|
|
|
|
|
|
|
|
print("good by!")
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-05-25 22:20:37 +00:00
|
|
|
if __name__ == "__main__":
|
2019-10-18 07:39:37 +00:00
|
|
|
import doctest
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
doctest.testmod()
|
|
|
|
main()
|