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

Method is_sorted

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

>>> Node(data='abc').is_sorted True >>> Node(data=2, ... left=Node(data=1.999), ... right=Node(data=3)).is_sorted True >>> Node(data=0, ... left=Node(data=0), ... right=Node(data=0)).is_sorted True

(self)

Source from the content-addressed store, hash-verified

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):
82 return False
83 return not (
84 self.right and (self.data > self.right.data or not self.right.is_sorted)
85 )
86
87
88if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected