MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / Node

Class Node

data_structures/binary_tree/is_sorted.py:24–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22
23@dataclass
24class Node:
25 data: float
26 left: Node | None = None
27 right: Node | None = None
28
29 def __iter__(self) -> Iterator[float]:
30 """
31 >>> root = Node(data=2.1)
32 >>> list(root)
33 [2.1]
34 >>> root.left=Node(data=2.0)
35 >>> list(root)
36 [2.0, 2.1]
37 >>> root.right=Node(data=2.2)
38 >>> list(root)
39 [2.0, 2.1, 2.2]
40 """
41 if self.left:
42 yield from self.left
43 yield self.data
44 if self.right:
45 yield from self.right
46
47 @property
48 def is_sorted(self) -> bool:
49 """
50 >>> Node(data='abc').is_sorted
51 True
52 >>> Node(data=2,
53 ... left=Node(data=1.999),
54 ... right=Node(data=3)).is_sorted
55 True
56 >>> Node(data=0,
57 ... left=Node(data=0),
58 ... right=Node(data=0)).is_sorted
59 True
60 >>> Node(data=0,
61 ... left=Node(data=-11),
62 ... right=Node(data=3)).is_sorted
63 True
64 >>> Node(data=5,
65 ... left=Node(data=1),
66 ... right=Node(data=4, left=Node(data=3))).is_sorted
67 False
68 >>> Node(data='a',
69 ... left=Node(data=1),
70 ... right=Node(data=4, left=Node(data=3))).is_sorted
71 Traceback (most recent call last):
72 ...
73 TypeError: '<' not supported between instances of 'str' and 'int'
74 >>> Node(data=2,
75 ... left=Node([]),
76 ... right=Node(data=4, left=Node(data=3))).is_sorted
77 Traceback (most recent call last):
78 ...
79 TypeError: '<' not supported between instances of 'int' and 'list'
80 """
81 if self.left and (self.data < self.left.data or not self.left.is_sorted):

Callers 1

is_sorted.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected