Skip to content

Commit c15dda4

Browse files
Saswatsusmoycclausspre-commit-ci[bot]
authored
Update basic_binary_tree.py (TheAlgorithms#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 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3923e59 commit c15dda4

File tree

1 file changed

+103
-94
lines changed

1 file changed

+103
-94
lines changed
+103-94
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,110 @@
11
from __future__ import annotations
22

3+
from collections.abc import Iterator
4+
from dataclasses import dataclass
35

6+
7+
@dataclass
48
class Node:
5-
"""
6-
A Node has data variable and pointers to Nodes to its left and right.
7-
"""
8-
9-
def __init__(self, data: int) -> None:
10-
self.data = data
11-
self.left: Node | None = None
12-
self.right: Node | None = None
13-
14-
15-
def display(tree: Node | None) -> None: # In Order traversal of the tree
16-
"""
17-
>>> root = Node(1)
18-
>>> root.left = Node(0)
19-
>>> root.right = Node(2)
20-
>>> display(root)
21-
0
22-
1
23-
2
24-
>>> display(root.right)
25-
2
26-
"""
27-
if tree:
28-
display(tree.left)
29-
print(tree.data)
30-
display(tree.right)
31-
32-
33-
def depth_of_tree(tree: Node | None) -> int:
34-
"""
35-
Recursive function that returns the depth of a binary tree.
36-
37-
>>> root = Node(0)
38-
>>> depth_of_tree(root)
39-
1
40-
>>> root.left = Node(0)
41-
>>> depth_of_tree(root)
42-
2
43-
>>> root.right = Node(0)
44-
>>> depth_of_tree(root)
45-
2
46-
>>> root.left.right = Node(0)
47-
>>> depth_of_tree(root)
48-
3
49-
>>> depth_of_tree(root.left)
50-
2
51-
"""
52-
return 1 + max(depth_of_tree(tree.left), depth_of_tree(tree.right)) if tree else 0
53-
54-
55-
def is_full_binary_tree(tree: Node) -> bool:
56-
"""
57-
Returns True if this is a full binary tree
58-
59-
>>> root = Node(0)
60-
>>> is_full_binary_tree(root)
61-
True
62-
>>> root.left = Node(0)
63-
>>> is_full_binary_tree(root)
64-
False
65-
>>> root.right = Node(0)
66-
>>> is_full_binary_tree(root)
67-
True
68-
>>> root.left.left = Node(0)
69-
>>> is_full_binary_tree(root)
70-
False
71-
>>> root.right.right = Node(0)
72-
>>> is_full_binary_tree(root)
73-
False
74-
"""
75-
if not tree:
76-
return True
77-
if tree.left and tree.right:
78-
return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)
79-
else:
80-
return not tree.left and not tree.right
81-
82-
83-
def main() -> None: # Main function for testing.
84-
tree = Node(1)
85-
tree.left = Node(2)
86-
tree.right = Node(3)
87-
tree.left.left = Node(4)
88-
tree.left.right = Node(5)
89-
tree.left.right.left = Node(6)
90-
tree.right.left = Node(7)
91-
tree.right.left.left = Node(8)
92-
tree.right.left.left.right = Node(9)
93-
94-
print(is_full_binary_tree(tree))
95-
print(depth_of_tree(tree))
96-
print("Tree is: ")
97-
display(tree)
9+
data: int
10+
left: Node | None = None
11+
right: Node | None = None
12+
13+
def __iter__(self) -> Iterator[int]:
14+
if self.left:
15+
yield from self.left
16+
yield self.data
17+
if self.right:
18+
yield from self.right
19+
20+
def __len__(self) -> int:
21+
return sum(1 for _ in self)
22+
23+
def is_full(self) -> bool:
24+
if not self or (not self.left and not self.right):
25+
return True
26+
if self.left and self.right:
27+
return self.left.is_full() and self.right.is_full()
28+
return False
29+
30+
31+
@dataclass
32+
class BinaryTree:
33+
root: Node
34+
35+
def __iter__(self) -> Iterator[int]:
36+
return iter(self.root)
37+
38+
def __len__(self) -> int:
39+
return len(self.root)
40+
41+
@classmethod
42+
def small_tree(cls) -> BinaryTree:
43+
"""
44+
Return a small binary tree with 3 nodes.
45+
>>> binary_tree = BinaryTree.small_tree()
46+
>>> len(binary_tree)
47+
3
48+
>>> list(binary_tree)
49+
[1, 2, 3]
50+
"""
51+
binary_tree = BinaryTree(Node(2))
52+
binary_tree.root.left = Node(1)
53+
binary_tree.root.right = Node(3)
54+
return binary_tree
55+
56+
@classmethod
57+
def medium_tree(cls) -> BinaryTree:
58+
"""
59+
Return a medium binary tree with 3 nodes.
60+
>>> binary_tree = BinaryTree.medium_tree()
61+
>>> len(binary_tree)
62+
7
63+
>>> list(binary_tree)
64+
[1, 2, 3, 4, 5, 6, 7]
65+
"""
66+
binary_tree = BinaryTree(Node(4))
67+
binary_tree.root.left = two = Node(2)
68+
two.left = Node(1)
69+
two.right = Node(3)
70+
binary_tree.root.right = five = Node(5)
71+
five.right = six = Node(6)
72+
six.right = Node(7)
73+
return binary_tree
74+
75+
def depth(self) -> int:
76+
"""
77+
Returns the depth of the tree
78+
79+
>>> BinaryTree(Node(1)).depth()
80+
1
81+
>>> BinaryTree.small_tree().depth()
82+
2
83+
>>> BinaryTree.medium_tree().depth()
84+
4
85+
"""
86+
return self._depth(self.root)
87+
88+
def _depth(self, node: Node | None) -> int: # noqa: UP007
89+
if not node:
90+
return 0
91+
return 1 + max(self._depth(node.left), self._depth(node.right))
92+
93+
def is_full(self) -> bool:
94+
"""
95+
Returns True if the tree is full
96+
97+
>>> BinaryTree(Node(1)).is_full()
98+
True
99+
>>> BinaryTree.small_tree().is_full()
100+
True
101+
>>> BinaryTree.medium_tree().is_full()
102+
False
103+
"""
104+
return self.root.is_full()
98105

99106

100107
if __name__ == "__main__":
101-
main()
108+
import doctest
109+
110+
doctest.testmod()

0 commit comments

Comments
 (0)