Skip to content

Commit b511b14

Browse files
committed
Added comments to treap
1 parent b8cd089 commit b511b14

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

data_structures/binary tree/treap.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44

55
class Node:
6+
"""
7+
Treap's node
8+
Treap is a binary tree by key and heap by priority
9+
"""
610
def __init__(self, key: int):
711
self.key = key
812
self.prior = random()
@@ -11,28 +15,64 @@ def __init__(self, key: int):
1115

1216

1317
def split(root: Node, key: int) -> Tuple[Node, Node]:
14-
if root is None:
18+
"""
19+
We split current tree into 2 trees with key:
20+
21+
Left tree contains all keys less than split key.
22+
Right tree contains all keys greater or equal, than split key
23+
"""
24+
if root is None: # None tree is split into 2 Nones
1525
return (None, None)
1626
if root.key >= key:
27+
"""
28+
Right tree's root will be current node.
29+
Now we split(with the same key) current node's left son
30+
Left tree: left part of that split
31+
Right tree's left son: right part of that split
32+
"""
1733
l, root.l = split(root.l, key)
1834
return (l, root)
1935
else:
36+
"""
37+
Just symmetric to previous case
38+
"""
2039
root.r, r = split(root.r, key)
2140
return (root, r)
2241

2342

2443
def merge(left: Node, right: Node) -> Node:
44+
"""
45+
We merge 2 trees into one.
46+
Note: all left tree's keys must be less than all right tree's
47+
"""
2548
if (not left) or (not right):
49+
"""
50+
If one node is None, return the other
51+
"""
2652
return left or right
2753
if left.key > right.key:
54+
"""
55+
Left will be root because it has more priority
56+
Now we need to merge left's right son and right tree
57+
"""
2858
left.r = merge(left.r, right)
2959
return left
3060
else:
61+
"""
62+
Symmetric as well
63+
"""
3164
right.l = merge(left, right.l)
3265
return right
3366

3467

3568
def insert(root: Node, key: int) -> Node:
69+
"""
70+
Insert element
71+
72+
Split current tree with a key into l, r,
73+
Insert new node into the middle
74+
Merge l, node, r into root
75+
"""
3676
node = Node(key)
3777
l, r = split(root, key)
3878
root = merge(l, node)
@@ -41,12 +81,22 @@ def insert(root: Node, key: int) -> Node:
4181

4282

4383
def erase(root: Node, key: int) -> Node:
84+
"""
85+
Erase element
86+
87+
Split all nodes with keys less into l,
88+
Split all nodes with keys greater into r.
89+
Merge l, r
90+
"""
4491
l, r = split(root, key)
4592
_, r = split(r, key + 1)
4693
return merge(l, r)
4794

4895

4996
def node_print(root: Node):
97+
"""
98+
Just recursive print of a tree
99+
"""
50100
if not root:
51101
return
52102
node_print(root.l)
@@ -55,6 +105,13 @@ def node_print(root: Node):
55105

56106

57107
def interactTreap():
108+
"""
109+
Commands:
110+
+ key to add key into treap
111+
- key to erase all nodes with key
112+
113+
After each command, program prints treap
114+
"""
58115
root = None
59116
while True:
60117
cmd = input().split()

0 commit comments

Comments
 (0)