Created
March 27, 2019 18:30
-
-
Save jaraco/c165de9a87aaa239426dfdb31dc5d74c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
import itertools | |
def pairwise(iterable): | |
orig, copy = itertools.tee(iterable) | |
next(copy) | |
return zip(orig, copy) | |
class Node: | |
def __init__(self, value, left=None, right=None): | |
assert value is not None | |
self.value = value | |
self.left = left | |
self.right = right | |
def __lt__(self, other): | |
return self.value < other.value | |
def traverse_inorder(self): | |
if self.left: | |
yield from self.left.traverse_inorder() | |
yield self.value | |
if self.right: | |
yield from self.right.traverse_inorder() | |
def isBst(tree): | |
values = tree.traverse_inorder() | |
return all(itertools.starmap(operator.lt, pairwise(values))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment