mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
64
ebook_src/trees/check_if_bst.py
Executable file
64
ebook_src/trees/check_if_bst.py
Executable file
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from binary_search_tree import BST, Node
|
||||
from binary_tree import BT, Node
|
||||
|
||||
|
||||
def isBST(node, min_node=float("-infinity"), maxVal=float("infinity")):
|
||||
if not node:
|
||||
return True
|
||||
|
||||
if not min_node <= node.item <= maxVal:
|
||||
return False
|
||||
|
||||
return isBST(node.left, min_node, node.item) and \
|
||||
isBST(node.right, node.item, maxVal)
|
||||
|
||||
|
||||
|
||||
def isBST_other_method(node, max_node=None, min_node=None):
|
||||
|
||||
if not node:
|
||||
return True
|
||||
|
||||
left, right = True, True
|
||||
min_node = min_node or float('inf')
|
||||
max_node = max_node or -float('inf')
|
||||
|
||||
if node.left:
|
||||
if node.left.item > node.item or node.left.item > max_node:
|
||||
left = False
|
||||
else:
|
||||
max_node = node.item
|
||||
left = isBST(node.left, max_node, min_node)
|
||||
|
||||
if node.right:
|
||||
if node.right.item < node.item or node.right.item < min_node:
|
||||
rihjt = False
|
||||
else:
|
||||
min_node = node.item
|
||||
right = isBST(node.right, max_node, min_node)
|
||||
|
||||
return left and right
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
bt = BST()
|
||||
for i in range(1, 10):
|
||||
bt.add(i)
|
||||
|
||||
assert(isBST(bt.root) == True)
|
||||
|
||||
bt = BT()
|
||||
for i in range(1, 10):
|
||||
bt.add(i)
|
||||
|
||||
assert(isBST(bt.root) == False)
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue