mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
🏣 Clean up for arxiv
This commit is contained in:
parent
1b969e7db3
commit
41756cb10c
280 changed files with 2 additions and 11 deletions
31
book/ebook_src/trees/check_if_balanced.py
Executable file
31
book/ebook_src/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