MCPcopy
hub / github.com/TheAlgorithms/Python / split

Function split

data_structures/binary_tree/treap.py:35–58  ·  view source on GitHub ↗

We split current tree into 2 trees with value: Left tree contains all values less than split value. Right tree contains all values greater or equal, than split value

(root: Node | None, value: int)

Source from the content-addressed store, hash-verified

33
34
35def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
36 """
37 We split current tree into 2 trees with value:
38
39 Left tree contains all values less than split value.
40 Right tree contains all values greater or equal, than split value
41 """
42 if root is None or root.value is None: # None tree is split into 2 Nones
43 return None, None
44 elif value < root.value:
45 """
46 Right tree&#x27;s root will be current node.
47 Now we split(with the same value) current node&#x27;s left son
48 Left tree: left part of that split
49 Right tree&#x27;s left son: right part of that split
50 """
51 left, root.left = split(root.left, value)
52 return left, root
53 else:
54 """
55 Just symmetric to previous case
56 """
57 root.right, right = split(root.right, value)
58 return root, right
59
60
61def merge(left: Node | None, right: Node | None) -> Node | None:

Callers 2

insertFunction · 0.70
eraseFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected