mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 08:51:31 -04:00
Change the dir structure slightly
This commit is contained in:
parent
6b6fe21db3
commit
2f4a9638c0
184 changed files with 0 additions and 21 deletions
31
source_code/trees/check_if_balanced.py
Executable file
31
source_code/trees/check_if_balanced.py
Executable file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from binary_search_tree import BST, Node
|
||||
from binary_tree import BT, Node
|
||||
|
||||
|
||||
|
||||
def isBalanced(node, left=0, right=0):
|
||||
if not node:
|
||||
return (left - right) < 2
|
||||
|
||||
return isBalanced(node.left, left+1, right) and \
|
||||
isBalanced(node.right, left, right+1)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
bt = BST()
|
||||
for i in range(1, 10):
|
||||
bt.add(i)
|
||||
|
||||
assert(isBalanced(bt.root) == True)
|
||||
|
||||
bt = BT()
|
||||
for i in range(1, 10):
|
||||
bt.add(i)
|
||||
|
||||
assert(isBalanced(bt.root) == False)
|
Loading…
Add table
Add a link
Reference in a new issue