Commands: + value to add value into treap - value to erase all nodes with value >>> root = interact_treap(None, "+1") >>> inorder(root) 1, >>> root = interact_treap(root, "+3 +5 +17 +19 +2 +16 +4 +0") >>> inorder(root) 0,1,2,3,4,5,16,17,1
(root: Node | None, args: str)
| 119 | |
| 120 | |
| 121 | def interact_treap(root: Node | None, args: str) -> Node | None: |
| 122 | """ |
| 123 | Commands: |
| 124 | + value to add value into treap |
| 125 | - value to erase all nodes with value |
| 126 | |
| 127 | >>> root = interact_treap(None, "+1") |
| 128 | >>> inorder(root) |
| 129 | 1, |
| 130 | >>> root = interact_treap(root, "+3 +5 +17 +19 +2 +16 +4 +0") |
| 131 | >>> inorder(root) |
| 132 | 0,1,2,3,4,5,16,17,19, |
| 133 | >>> root = interact_treap(root, "+4 +4 +4") |
| 134 | >>> inorder(root) |
| 135 | 0,1,2,3,4,4,4,4,5,16,17,19, |
| 136 | >>> root = interact_treap(root, "-0") |
| 137 | >>> inorder(root) |
| 138 | 1,2,3,4,4,4,4,5,16,17,19, |
| 139 | >>> root = interact_treap(root, "-4") |
| 140 | >>> inorder(root) |
| 141 | 1,2,3,5,16,17,19, |
| 142 | >>> root = interact_treap(root, "=0") |
| 143 | Unknown command |
| 144 | """ |
| 145 | for arg in args.split(): |
| 146 | if arg[0] == "+": |
| 147 | root = insert(root, int(arg[1:])) |
| 148 | |
| 149 | elif arg[0] == "-": |
| 150 | root = erase(root, int(arg[1:])) |
| 151 | |
| 152 | else: |
| 153 | print("Unknown command") |
| 154 | |
| 155 | return root |
| 156 | |
| 157 | |
| 158 | def main() -> None: |