2020-05-22 06:10:11 +00:00
|
|
|
# flake8: noqa
|
|
|
|
|
2019-05-25 22:20:37 +00:00
|
|
|
from random import random
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def __init__(self, value: int = None):
|
|
|
|
self.value = value
|
2019-05-25 22:20:37 +00:00
|
|
|
self.prior = random()
|
2019-10-18 07:39:37 +00:00
|
|
|
self.left = None
|
|
|
|
self.right = None
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def __repr__(self):
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
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
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def split(root: Node, value: int) -> Tuple[Node, Node]:
|
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
|
|
|
|
return (None, None)
|
2019-10-18 07:39:37 +00:00
|
|
|
elif root.value is None:
|
|
|
|
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)
|
|
|
|
return (left, root)
|
|
|
|
else:
|
|
|
|
"""
|
|
|
|
Just symmetric to previous case
|
|
|
|
"""
|
|
|
|
root.right, right = split(root.right, value)
|
|
|
|
return (root, right)
|
2019-05-25 22:20:37 +00:00
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-05-25 22:20:37 +00:00
|
|
|
def merge(left: Node, right: Node) -> Node:
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def insert(root: Node, value: int) -> Node:
|
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
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def erase(root: Node, value: int) -> Node:
|
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
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def inorder(root: Node):
|
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)
|
|
|
|
print(root.value, end=" ")
|
|
|
|
inorder(root.right)
|
2019-05-25 22:20:37 +00:00
|
|
|
|
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def interactTreap(root, args):
|
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
|
|
|
|
|
|
|
|
>>> root = interactTreap(None, "+1")
|
|
|
|
>>> inorder(root)
|
|
|
|
1
|
|
|
|
>>> root = interactTreap(root, "+3 +5 +17 +19 +2 +16 +4 +0")
|
|
|
|
>>> inorder(root)
|
|
|
|
0 1 2 3 4 5 16 17 19
|
|
|
|
>>> root = interactTreap(root, "+4 +4 +4")
|
|
|
|
>>> inorder(root)
|
|
|
|
0 1 2 3 4 4 4 4 5 16 17 19
|
|
|
|
>>> root = interactTreap(root, "-0")
|
|
|
|
>>> inorder(root)
|
|
|
|
1 2 3 4 4 4 4 5 16 17 19
|
|
|
|
>>> root = interactTreap(root, "-4")
|
|
|
|
>>> inorder(root)
|
|
|
|
1 2 3 5 16 17 19
|
|
|
|
>>> root = interactTreap(root, "=0")
|
|
|
|
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
|
|
|
|
2019-10-18 07:39:37 +00:00
|
|
|
def main():
|
|
|
|
"""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":
|
2019-10-18 07:39:37 +00:00
|
|
|
root = interactTreap(root, args)
|
|
|
|
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()
|