mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 10:31:29 +08:00
Pyupgrade to Python 3.9 (#4718)
* Pyupgrade to Python 3.9 * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -5,15 +5,16 @@ python3 -m doctest -v avl_tree.py
|
||||
For testing run:
|
||||
python avl_tree.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import random
|
||||
from typing import Any, List, Optional
|
||||
from typing import Any
|
||||
|
||||
|
||||
class my_queue:
|
||||
def __init__(self) -> None:
|
||||
self.data: List[Any] = []
|
||||
self.data: list[Any] = []
|
||||
self.head: int = 0
|
||||
self.tail: int = 0
|
||||
|
||||
@ -41,17 +42,17 @@ class my_queue:
|
||||
class my_node:
|
||||
def __init__(self, data: Any) -> None:
|
||||
self.data = data
|
||||
self.left: Optional[my_node] = None
|
||||
self.right: Optional[my_node] = None
|
||||
self.left: my_node | None = None
|
||||
self.right: my_node | None = None
|
||||
self.height: int = 1
|
||||
|
||||
def get_data(self) -> Any:
|
||||
return self.data
|
||||
|
||||
def get_left(self) -> Optional["my_node"]:
|
||||
def get_left(self) -> my_node | None:
|
||||
return self.left
|
||||
|
||||
def get_right(self) -> Optional["my_node"]:
|
||||
def get_right(self) -> my_node | None:
|
||||
return self.right
|
||||
|
||||
def get_height(self) -> int:
|
||||
@ -61,11 +62,11 @@ class my_node:
|
||||
self.data = data
|
||||
return
|
||||
|
||||
def set_left(self, node: Optional["my_node"]) -> None:
|
||||
def set_left(self, node: my_node | None) -> None:
|
||||
self.left = node
|
||||
return
|
||||
|
||||
def set_right(self, node: Optional["my_node"]) -> None:
|
||||
def set_right(self, node: my_node | None) -> None:
|
||||
self.right = node
|
||||
return
|
||||
|
||||
@ -74,7 +75,7 @@ class my_node:
|
||||
return
|
||||
|
||||
|
||||
def get_height(node: Optional["my_node"]) -> int:
|
||||
def get_height(node: my_node | None) -> int:
|
||||
if node is None:
|
||||
return 0
|
||||
return node.get_height()
|
||||
@ -149,7 +150,7 @@ def rl_rotation(node: my_node) -> my_node:
|
||||
return left_rotation(node)
|
||||
|
||||
|
||||
def insert_node(node: Optional["my_node"], data: Any) -> Optional["my_node"]:
|
||||
def insert_node(node: my_node | None, data: Any) -> my_node | None:
|
||||
if node is None:
|
||||
return my_node(data)
|
||||
if data < node.get_data():
|
||||
@ -197,7 +198,7 @@ def get_leftMost(root: my_node) -> Any:
|
||||
return root.get_data()
|
||||
|
||||
|
||||
def del_node(root: my_node, data: Any) -> Optional["my_node"]:
|
||||
def del_node(root: my_node, data: Any) -> my_node | None:
|
||||
left_child = root.get_left()
|
||||
right_child = root.get_right()
|
||||
if root.get_data() == data:
|
||||
@ -275,7 +276,7 @@ class AVLtree:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.root: Optional[my_node] = None
|
||||
self.root: my_node | None = None
|
||||
|
||||
def get_height(self) -> int:
|
||||
return get_height(self.root)
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class Node:
|
||||
@ -8,11 +8,11 @@ class Node:
|
||||
|
||||
def __init__(self, data: int) -> None:
|
||||
self.data = data
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
|
||||
def display(tree: Optional[Node]) -> None: # In Order traversal of the tree
|
||||
def display(tree: Node | None) -> None: # In Order traversal of the tree
|
||||
"""
|
||||
>>> root = Node(1)
|
||||
>>> root.left = Node(0)
|
||||
@ -30,7 +30,7 @@ def display(tree: Optional[Node]) -> None: # In Order traversal of the tree
|
||||
display(tree.right)
|
||||
|
||||
|
||||
def depth_of_tree(tree: Optional[Node]) -> int:
|
||||
def depth_of_tree(tree: Node | None) -> int:
|
||||
"""
|
||||
Recursive function that returns the depth of a binary tree.
|
||||
|
||||
|
@ -7,21 +7,23 @@ python -m unittest binary_search_tree_recursive.py
|
||||
To run an example:
|
||||
python binary_search_tree_recursive.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from typing import Iterator, Optional
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, label: int, parent: Optional["Node"]) -> None:
|
||||
def __init__(self, label: int, parent: Node | None) -> None:
|
||||
self.label = label
|
||||
self.parent = parent
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
|
||||
class BinarySearchTree:
|
||||
def __init__(self) -> None:
|
||||
self.root: Optional[Node] = None
|
||||
self.root: Node | None = None
|
||||
|
||||
def empty(self) -> None:
|
||||
"""
|
||||
@ -66,9 +68,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
self.root = self._put(self.root, label)
|
||||
|
||||
def _put(
|
||||
self, node: Optional[Node], label: int, parent: Optional[Node] = None
|
||||
) -> Node:
|
||||
def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node:
|
||||
if node is None:
|
||||
node = Node(label, parent)
|
||||
else:
|
||||
@ -98,7 +98,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
return self._search(self.root, label)
|
||||
|
||||
def _search(self, node: Optional[Node], label: int) -> Node:
|
||||
def _search(self, node: Node | None, label: int) -> Node:
|
||||
if node is None:
|
||||
raise Exception(f"Node with label {label} does not exist")
|
||||
else:
|
||||
@ -140,7 +140,7 @@ class BinarySearchTree:
|
||||
else:
|
||||
self._reassign_nodes(node, None)
|
||||
|
||||
def _reassign_nodes(self, node: Node, new_children: Optional[Node]) -> None:
|
||||
def _reassign_nodes(self, node: Node, new_children: Node | None) -> None:
|
||||
if new_children:
|
||||
new_children.parent = node.parent
|
||||
|
||||
@ -244,7 +244,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
return self._inorder_traversal(self.root)
|
||||
|
||||
def _inorder_traversal(self, node: Optional[Node]) -> Iterator[Node]:
|
||||
def _inorder_traversal(self, node: Node | None) -> Iterator[Node]:
|
||||
if node is not None:
|
||||
yield from self._inorder_traversal(node.left)
|
||||
yield node
|
||||
@ -266,7 +266,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
return self._preorder_traversal(self.root)
|
||||
|
||||
def _preorder_traversal(self, node: Optional[Node]) -> Iterator[Node]:
|
||||
def _preorder_traversal(self, node: Node | None) -> Iterator[Node]:
|
||||
if node is not None:
|
||||
yield node
|
||||
yield from self._preorder_traversal(node.left)
|
||||
|
@ -1,13 +1,14 @@
|
||||
# https://en.wikipedia.org/wiki/Tree_traversal
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class Node:
|
||||
data: int
|
||||
left: Optional["Node"] = None
|
||||
right: Optional["Node"] = None
|
||||
left: Node | None = None
|
||||
right: Node | None = None
|
||||
|
||||
|
||||
def make_tree() -> Node:
|
||||
|
@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import List, Union
|
||||
|
||||
|
||||
class SegmentTree:
|
||||
@ -38,7 +37,7 @@ class SegmentTree:
|
||||
return idx * 2 + 1
|
||||
|
||||
def build(
|
||||
self, idx: int, left_element: int, right_element: int, A: List[int]
|
||||
self, idx: int, left_element: int, right_element: int, A: list[int]
|
||||
) -> None:
|
||||
if left_element == right_element:
|
||||
self.segment_tree[idx] = A[left_element - 1]
|
||||
@ -89,7 +88,7 @@ class SegmentTree:
|
||||
# query with O(lg n)
|
||||
def query(
|
||||
self, idx: int, left_element: int, right_element: int, a: int, b: int
|
||||
) -> Union[int, float]:
|
||||
) -> int | float:
|
||||
"""
|
||||
query(1, 1, size, a, b) for query max of [a,b]
|
||||
>>> A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
|
||||
|
@ -5,7 +5,7 @@ The rule for merging is that if two nodes overlap, then put the value sum of
|
||||
both nodes to the new value of the merged node. Otherwise, the NOT null node
|
||||
will be used as the node of new tree.
|
||||
"""
|
||||
from typing import Optional
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class Node:
|
||||
@ -15,11 +15,11 @@ class Node:
|
||||
|
||||
def __init__(self, value: int = 0) -> None:
|
||||
self.value = value
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
|
||||
def merge_two_binary_trees(tree1: Optional[Node], tree2: Optional[Node]) -> Node:
|
||||
def merge_two_binary_trees(tree1: Node | None, tree2: Node | None) -> Node:
|
||||
"""
|
||||
Returns root node of the merged tree.
|
||||
|
||||
@ -52,7 +52,7 @@ def merge_two_binary_trees(tree1: Optional[Node], tree2: Optional[Node]) -> Node
|
||||
return tree1
|
||||
|
||||
|
||||
def print_preorder(root: Optional[Node]) -> None:
|
||||
def print_preorder(root: Node | None) -> None:
|
||||
"""
|
||||
Print pre-order traversal of the tree.
|
||||
|
||||
|
@ -2,7 +2,9 @@
|
||||
python/black : true
|
||||
flake8 : passed
|
||||
"""
|
||||
from typing import Iterator, Optional
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
class RedBlackTree:
|
||||
@ -21,11 +23,11 @@ class RedBlackTree:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
label: Optional[int] = None,
|
||||
label: int | None = None,
|
||||
color: int = 0,
|
||||
parent: Optional["RedBlackTree"] = None,
|
||||
left: Optional["RedBlackTree"] = None,
|
||||
right: Optional["RedBlackTree"] = None,
|
||||
parent: RedBlackTree | None = None,
|
||||
left: RedBlackTree | None = None,
|
||||
right: RedBlackTree | None = None,
|
||||
) -> None:
|
||||
"""Initialize a new Red-Black Tree node with the given values:
|
||||
label: The value associated with this node
|
||||
@ -42,7 +44,7 @@ class RedBlackTree:
|
||||
|
||||
# Here are functions which are specific to red-black trees
|
||||
|
||||
def rotate_left(self) -> "RedBlackTree":
|
||||
def rotate_left(self) -> RedBlackTree:
|
||||
"""Rotate the subtree rooted at this node to the left and
|
||||
returns the new root to this subtree.
|
||||
Performing one rotation can be done in O(1).
|
||||
@ -62,7 +64,7 @@ class RedBlackTree:
|
||||
right.parent = parent
|
||||
return right
|
||||
|
||||
def rotate_right(self) -> "RedBlackTree":
|
||||
def rotate_right(self) -> RedBlackTree:
|
||||
"""Rotate the subtree rooted at this node to the right and
|
||||
returns the new root to this subtree.
|
||||
Performing one rotation can be done in O(1).
|
||||
@ -82,7 +84,7 @@ class RedBlackTree:
|
||||
left.parent = parent
|
||||
return left
|
||||
|
||||
def insert(self, label: int) -> "RedBlackTree":
|
||||
def insert(self, label: int) -> RedBlackTree:
|
||||
"""Inserts label into the subtree rooted at self, performs any
|
||||
rotations necessary to maintain balance, and then returns the
|
||||
new root to this subtree (likely self).
|
||||
@ -139,7 +141,7 @@ class RedBlackTree:
|
||||
self.grandparent.color = 1
|
||||
self.grandparent._insert_repair()
|
||||
|
||||
def remove(self, label: int) -> "RedBlackTree":
|
||||
def remove(self, label: int) -> RedBlackTree:
|
||||
"""Remove label from this tree."""
|
||||
if self.label == label:
|
||||
if self.left and self.right:
|
||||
@ -337,7 +339,7 @@ class RedBlackTree:
|
||||
"""
|
||||
return self.search(label) is not None
|
||||
|
||||
def search(self, label: int) -> "RedBlackTree":
|
||||
def search(self, label: int) -> RedBlackTree:
|
||||
"""Search through the tree for label, returning its node if
|
||||
it's found, and None otherwise.
|
||||
This method is guaranteed to run in O(log(n)) time.
|
||||
@ -411,7 +413,7 @@ class RedBlackTree:
|
||||
return self.label
|
||||
|
||||
@property
|
||||
def grandparent(self) -> "RedBlackTree":
|
||||
def grandparent(self) -> RedBlackTree:
|
||||
"""Get the current node's grandparent, or None if it doesn't exist."""
|
||||
if self.parent is None:
|
||||
return None
|
||||
@ -419,7 +421,7 @@ class RedBlackTree:
|
||||
return self.parent.parent
|
||||
|
||||
@property
|
||||
def sibling(self) -> "RedBlackTree":
|
||||
def sibling(self) -> RedBlackTree:
|
||||
"""Get the current node's sibling, or None if it doesn't exist."""
|
||||
if self.parent is None:
|
||||
return None
|
||||
|
@ -1,9 +1,6 @@
|
||||
# flake8: noqa
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from random import random
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
class Node:
|
||||
@ -12,11 +9,11 @@ class Node:
|
||||
Treap is a binary tree by value and heap by priority
|
||||
"""
|
||||
|
||||
def __init__(self, value: Optional[int] = None):
|
||||
def __init__(self, value: int | None = None):
|
||||
self.value = value
|
||||
self.prior = random()
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
from pprint import pformat
|
||||
@ -35,7 +32,7 @@ class Node:
|
||||
return value + left + right
|
||||
|
||||
|
||||
def split(root: Optional[Node], value: int) -> Tuple[Optional[Node], Optional[Node]]:
|
||||
def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
|
||||
"""
|
||||
We split current tree into 2 trees with value:
|
||||
|
||||
@ -64,7 +61,7 @@ def split(root: Optional[Node], value: int) -> Tuple[Optional[Node], Optional[No
|
||||
return root, right
|
||||
|
||||
|
||||
def merge(left: Optional[Node], right: Optional[Node]) -> Optional[Node]:
|
||||
def merge(left: Node | None, right: Node | None) -> Node | None:
|
||||
"""
|
||||
We merge 2 trees into one.
|
||||
Note: all left tree's values must be less than all right tree's
|
||||
@ -86,7 +83,7 @@ def merge(left: Optional[Node], right: Optional[Node]) -> Optional[Node]:
|
||||
return right
|
||||
|
||||
|
||||
def insert(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
def insert(root: Node | None, value: int) -> Node | None:
|
||||
"""
|
||||
Insert element
|
||||
|
||||
@ -99,7 +96,7 @@ def insert(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
return merge(merge(left, node), right)
|
||||
|
||||
|
||||
def erase(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
def erase(root: Node | None, value: int) -> Node | None:
|
||||
"""
|
||||
Erase element
|
||||
|
||||
@ -112,7 +109,7 @@ def erase(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
return merge(left, right)
|
||||
|
||||
|
||||
def inorder(root: Optional[Node]) -> None:
|
||||
def inorder(root: Node | None) -> None:
|
||||
"""
|
||||
Just recursive print of a tree
|
||||
"""
|
||||
@ -124,7 +121,7 @@ def inorder(root: Optional[Node]) -> None:
|
||||
inorder(root.right)
|
||||
|
||||
|
||||
def interactTreap(root: Optional[Node], args: str) -> Optional[Node]:
|
||||
def interactTreap(root: Node | None, args: str) -> Node | None:
|
||||
"""
|
||||
Commands:
|
||||
+ value to add value into treap
|
||||
|
@ -7,8 +7,7 @@ such as the with segment trees or fenwick trees. You can read more about them he
|
||||
2. https://www.youtube.com/watch?v=4aSv9PcecDw&t=811s
|
||||
3. https://www.youtube.com/watch?v=CybAgVF-MMc&t=1178s
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from __future__ import annotations
|
||||
|
||||
test_array = [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]
|
||||
|
||||
@ -18,8 +17,8 @@ class Node:
|
||||
self.minn: int = -1
|
||||
self.maxx: int = -1
|
||||
self.map_left: list[int] = [-1] * length
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""
|
||||
|
Reference in New Issue
Block a user