mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update README.md
This commit is contained in:
parent
d1123f8c75
commit
92a74485ec
@ -66,32 +66,6 @@ def is_leaf(node):
|
|||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### balanced trees
|
|
||||||
|
|
||||||
<br>
|
|
||||||
|
|
||||||
* a **balanced tree** is a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
|
|
||||||
|
|
||||||
<br>
|
|
||||||
|
|
||||||
```python
|
|
||||||
def height(root):
|
|
||||||
if root is None:
|
|
||||||
return -1
|
|
||||||
|
|
||||||
return 1 + max(height(root.left), height(root.right))
|
|
||||||
|
|
||||||
def is_balanced(root):
|
|
||||||
if root is None:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return abs(height(root.left) - height(root.right)) < 2 and \
|
|
||||||
is_balanced(root.left) and is_balanced(root.right)
|
|
||||||
```
|
|
||||||
|
|
||||||
<br>
|
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
@ -135,6 +109,35 @@ def height(root):
|
|||||||
return 1 + max(height(root.left), height(root.right))
|
return 1 + max(height(root.left), height(root.right))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### balanced trees
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
* a **balanced tree** is a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
```python
|
||||||
|
def height(root):
|
||||||
|
if root is None:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
return 1 + max(height(root.left), height(root.right))
|
||||||
|
|
||||||
|
def is_balanced(root):
|
||||||
|
if root is None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return abs(height(root.left) - height(root.right)) < 2 and \
|
||||||
|
is_balanced(root.left) and is_balanced(root.right)
|
||||||
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### tree traversal: breath-first search (level-order)
|
### tree traversal: breath-first search (level-order)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user