mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Update basic_binary_tree.py (#10388)
* Update basic_binary_tree.py * Update basic_binary_tree.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
3923e590d7
commit
c15dda405a
|
@ -1,101 +1,110 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Iterator
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class Node:
|
class Node:
|
||||||
"""
|
data: int
|
||||||
A Node has data variable and pointers to Nodes to its left and right.
|
left: Node | None = None
|
||||||
"""
|
right: Node | None = None
|
||||||
|
|
||||||
def __init__(self, data: int) -> None:
|
def __iter__(self) -> Iterator[int]:
|
||||||
self.data = data
|
if self.left:
|
||||||
self.left: Node | None = None
|
yield from self.left
|
||||||
self.right: Node | None = None
|
yield self.data
|
||||||
|
if self.right:
|
||||||
|
yield from self.right
|
||||||
|
|
||||||
|
def __len__(self) -> int:
|
||||||
|
return sum(1 for _ in self)
|
||||||
|
|
||||||
|
def is_full(self) -> bool:
|
||||||
|
if not self or (not self.left and not self.right):
|
||||||
|
return True
|
||||||
|
if self.left and self.right:
|
||||||
|
return self.left.is_full() and self.right.is_full()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def display(tree: Node | None) -> None: # In Order traversal of the tree
|
@dataclass
|
||||||
"""
|
class BinaryTree:
|
||||||
>>> root = Node(1)
|
root: Node
|
||||||
>>> root.left = Node(0)
|
|
||||||
>>> root.right = Node(2)
|
|
||||||
>>> display(root)
|
|
||||||
0
|
|
||||||
1
|
|
||||||
2
|
|
||||||
>>> display(root.right)
|
|
||||||
2
|
|
||||||
"""
|
|
||||||
if tree:
|
|
||||||
display(tree.left)
|
|
||||||
print(tree.data)
|
|
||||||
display(tree.right)
|
|
||||||
|
|
||||||
|
def __iter__(self) -> Iterator[int]:
|
||||||
|
return iter(self.root)
|
||||||
|
|
||||||
def depth_of_tree(tree: Node | None) -> int:
|
def __len__(self) -> int:
|
||||||
"""
|
return len(self.root)
|
||||||
Recursive function that returns the depth of a binary tree.
|
|
||||||
|
|
||||||
>>> root = Node(0)
|
@classmethod
|
||||||
>>> depth_of_tree(root)
|
def small_tree(cls) -> BinaryTree:
|
||||||
1
|
"""
|
||||||
>>> root.left = Node(0)
|
Return a small binary tree with 3 nodes.
|
||||||
>>> depth_of_tree(root)
|
>>> binary_tree = BinaryTree.small_tree()
|
||||||
2
|
>>> len(binary_tree)
|
||||||
>>> root.right = Node(0)
|
3
|
||||||
>>> depth_of_tree(root)
|
>>> list(binary_tree)
|
||||||
2
|
[1, 2, 3]
|
||||||
>>> root.left.right = Node(0)
|
"""
|
||||||
>>> depth_of_tree(root)
|
binary_tree = BinaryTree(Node(2))
|
||||||
3
|
binary_tree.root.left = Node(1)
|
||||||
>>> depth_of_tree(root.left)
|
binary_tree.root.right = Node(3)
|
||||||
2
|
return binary_tree
|
||||||
"""
|
|
||||||
return 1 + max(depth_of_tree(tree.left), depth_of_tree(tree.right)) if tree else 0
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def medium_tree(cls) -> BinaryTree:
|
||||||
|
"""
|
||||||
|
Return a medium binary tree with 3 nodes.
|
||||||
|
>>> binary_tree = BinaryTree.medium_tree()
|
||||||
|
>>> len(binary_tree)
|
||||||
|
7
|
||||||
|
>>> list(binary_tree)
|
||||||
|
[1, 2, 3, 4, 5, 6, 7]
|
||||||
|
"""
|
||||||
|
binary_tree = BinaryTree(Node(4))
|
||||||
|
binary_tree.root.left = two = Node(2)
|
||||||
|
two.left = Node(1)
|
||||||
|
two.right = Node(3)
|
||||||
|
binary_tree.root.right = five = Node(5)
|
||||||
|
five.right = six = Node(6)
|
||||||
|
six.right = Node(7)
|
||||||
|
return binary_tree
|
||||||
|
|
||||||
def is_full_binary_tree(tree: Node) -> bool:
|
def depth(self) -> int:
|
||||||
"""
|
"""
|
||||||
Returns True if this is a full binary tree
|
Returns the depth of the tree
|
||||||
|
|
||||||
>>> root = Node(0)
|
>>> BinaryTree(Node(1)).depth()
|
||||||
>>> is_full_binary_tree(root)
|
1
|
||||||
True
|
>>> BinaryTree.small_tree().depth()
|
||||||
>>> root.left = Node(0)
|
2
|
||||||
>>> is_full_binary_tree(root)
|
>>> BinaryTree.medium_tree().depth()
|
||||||
False
|
4
|
||||||
>>> root.right = Node(0)
|
"""
|
||||||
>>> is_full_binary_tree(root)
|
return self._depth(self.root)
|
||||||
True
|
|
||||||
>>> root.left.left = Node(0)
|
|
||||||
>>> is_full_binary_tree(root)
|
|
||||||
False
|
|
||||||
>>> root.right.right = Node(0)
|
|
||||||
>>> is_full_binary_tree(root)
|
|
||||||
False
|
|
||||||
"""
|
|
||||||
if not tree:
|
|
||||||
return True
|
|
||||||
if tree.left and tree.right:
|
|
||||||
return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)
|
|
||||||
else:
|
|
||||||
return not tree.left and not tree.right
|
|
||||||
|
|
||||||
|
def _depth(self, node: Node | None) -> int: # noqa: UP007
|
||||||
|
if not node:
|
||||||
|
return 0
|
||||||
|
return 1 + max(self._depth(node.left), self._depth(node.right))
|
||||||
|
|
||||||
def main() -> None: # Main function for testing.
|
def is_full(self) -> bool:
|
||||||
tree = Node(1)
|
"""
|
||||||
tree.left = Node(2)
|
Returns True if the tree is full
|
||||||
tree.right = Node(3)
|
|
||||||
tree.left.left = Node(4)
|
|
||||||
tree.left.right = Node(5)
|
|
||||||
tree.left.right.left = Node(6)
|
|
||||||
tree.right.left = Node(7)
|
|
||||||
tree.right.left.left = Node(8)
|
|
||||||
tree.right.left.left.right = Node(9)
|
|
||||||
|
|
||||||
print(is_full_binary_tree(tree))
|
>>> BinaryTree(Node(1)).is_full()
|
||||||
print(depth_of_tree(tree))
|
True
|
||||||
print("Tree is: ")
|
>>> BinaryTree.small_tree().is_full()
|
||||||
display(tree)
|
True
|
||||||
|
>>> BinaryTree.medium_tree().is_full()
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
return self.root.is_full()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user