mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 10:28:39 +00:00
Compare commits
3 Commits
8b831cb600
...
8cce9cf066
Author | SHA1 | Date | |
---|---|---|---|
|
8cce9cf066 | ||
|
4710e51deb | ||
|
d4f2873e39 |
@ -13,7 +13,7 @@
|
|||||||
<img src="https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square" height="20" alt="Contributions Welcome">
|
<img src="https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square" height="20" alt="Contributions Welcome">
|
||||||
</a>
|
</a>
|
||||||
<img src="https://img.shields.io/github/repo-size/TheAlgorithms/Python.svg?label=Repo%20size&style=flat-square" height="20">
|
<img src="https://img.shields.io/github/repo-size/TheAlgorithms/Python.svg?label=Repo%20size&style=flat-square" height="20">
|
||||||
<a href="https://discord.gg/c7MnfGFGa6">
|
<a href="https://the-algorithms.com/discord">
|
||||||
<img src="https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square" height="20" alt="Discord chat">
|
<img src="https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square" height="20" alt="Discord chat">
|
||||||
</a>
|
</a>
|
||||||
<a href="https://gitter.im/TheAlgorithms/community">
|
<a href="https://gitter.im/TheAlgorithms/community">
|
||||||
@ -42,7 +42,7 @@ Read through our [Contribution Guidelines](CONTRIBUTING.md) before you contribut
|
|||||||
|
|
||||||
## Community Channels
|
## Community Channels
|
||||||
|
|
||||||
We are on [Discord](https://discord.gg/c7MnfGFGa6) and [Gitter](https://gitter.im/TheAlgorithms/community)! Community channels are a great way for you to ask questions and get help. Please join us!
|
We are on [Discord](https://the-algorithms.com/discord) and [Gitter](https://gitter.im/TheAlgorithms/community)! Community channels are a great way for you to ask questions and get help. Please join us!
|
||||||
|
|
||||||
## List of Algorithms
|
## List of Algorithms
|
||||||
|
|
||||||
|
@ -58,6 +58,19 @@ def inorder(root: Node | None) -> list[int]:
|
|||||||
return [*inorder(root.left), root.data, *inorder(root.right)] if root else []
|
return [*inorder(root.left), root.data, *inorder(root.right)] if root else []
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_inorder(root: Node | None) -> list[int]:
|
||||||
|
"""
|
||||||
|
Reverse in-order traversal visits right subtree, root node, left subtree.
|
||||||
|
>>> reverse_inorder(make_tree())
|
||||||
|
[3, 1, 5, 2, 4]
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
[*reverse_inorder(root.right), root.data, *reverse_inorder(root.left)]
|
||||||
|
if root
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def height(root: Node | None) -> int:
|
def height(root: Node | None) -> int:
|
||||||
"""
|
"""
|
||||||
Recursive function for calculating the height of the binary tree.
|
Recursive function for calculating the height of the binary tree.
|
||||||
@ -161,15 +174,12 @@ def zigzag(root: Node | None) -> Sequence[Node | None] | list[Any]:
|
|||||||
|
|
||||||
|
|
||||||
def main() -> None: # Main function for testing.
|
def main() -> None: # Main function for testing.
|
||||||
"""
|
# Create binary tree.
|
||||||
Create binary tree.
|
|
||||||
"""
|
|
||||||
root = make_tree()
|
root = make_tree()
|
||||||
"""
|
|
||||||
All Traversals of the binary are as follows:
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
# All Traversals of the binary are as follows:
|
||||||
print(f"In-order Traversal: {inorder(root)}")
|
print(f"In-order Traversal: {inorder(root)}")
|
||||||
|
print(f"Reverse In-order Traversal: {reverse_inorder(root)}")
|
||||||
print(f"Pre-order Traversal: {preorder(root)}")
|
print(f"Pre-order Traversal: {preorder(root)}")
|
||||||
print(f"Post-order Traversal: {postorder(root)}", "\n")
|
print(f"Post-order Traversal: {postorder(root)}", "\n")
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ def linear_search(sequence: list, target: int) -> int:
|
|||||||
:param sequence: a collection with comparable items (as sorted items not required
|
:param sequence: a collection with comparable items (as sorted items not required
|
||||||
in Linear Search)
|
in Linear Search)
|
||||||
:param target: item value to search
|
:param target: item value to search
|
||||||
:return: index of found item or None if item is not found
|
:return: index of found item or -1 if item is not found
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> linear_search([0, 5, 7, 10, 15], 0)
|
>>> linear_search([0, 5, 7, 10, 15], 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user