Skip to content
Snippets Groups Projects
Commit 13d12b13 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

complete the class with the "balanced trees" part (mae-ac/trees!2)

parent 23a1264c
Branches
No related tags found
No related merge requests found
......@@ -35,8 +35,10 @@ Finally, a few examples of trees will be showcased:
- [_Question 13._](avl.md#question-13-toc)
- [_Question 14._](avl.md#question-14-toc)
- [_Question 15._](avl.md#question-15-toc)
- [_Tree rotations_](avl.md#tree-rotations-toc)
- [_Question 16._](avl.md#question-16-toc)
- [_Question 17._](avl.md#question-17-toc)
- [_Balancing our BSTs_](avl.md#balancing-our-bsts-toc)
- [_Question 18._](avl.md#question-18-toc)
- [_Maze generation and solving_](maze.md#maze-generation-and-solving-toc)
- [_Generating perfect mazes_](maze.md#generating-perfect-mazes-toc)
......
......@@ -102,18 +102,64 @@ balanced or not.
:gear: You can run `make test-avl`. If the "is_balanced" test is green, good job
:clap: :clap:
<-- TODO: tree rotations and _balanced_ BST.insert and BST.delete -->
## Tree rotations [[toc](README.md#table-of-content)]
If a tree is _unbalanced_, it is possible to fix the balance by applying a
_rotation_ on the root of the tree.
There are two types of rotations, left and right, depending on the side where
the balance is wrong.
As illustrated in the animation below, a right rotation consists of
- bubble up the left child as the new root
- move the previous root to the right child of the new root
- correct the BST property
![](assets/rotate.mp4)
## Question 16. [[toc](README.md#table-of-content)]
:pencil: rotations
### Question 16. [[toc](README.md#table-of-content)]
:pencil: Implement the two tree rotations.
:gear: You can run `make test-avl`. If the "rotate right" and "rotate left"
tests are green, good job :clap: :clap:
In order to help keeping track of the height of each node in the tree without
recomputing it all the time. We will add the height to our BST class and
recompute it after each operation on the tree.
:pencil: Add a field `height` to the BST class.
:pencil: Recompute the `height` after `insert`, `delete` and the rotations.
Below is the pseudo-algorithm of the tree "rebalancing" operation:
```js
1 rebalance(t)
2 if t.balance() < -1 and t.left.balance() == -1 then t.rotate_right()
3 else if t.balance() > 1 and t.right.balance() == 1 then t.rotate_left()
4 else if t.balance() < -1 and t.left.balance() == 1 then t.rotate_left_right()
5 else if t.balance() > 1 and t.right.balance() == -1 then t.rotate_right_left()
```
Where the "right-left" rotation consists of
- rotating the right subtree to the right
- rotating the root to the left
and the "left-right" rotation consists of
- rotating the left subtree to the left
- rotating the root to the right
### Question 17. [[toc](README.md#table-of-content)]
:pencil: Implement "left-right" and "right-left" rotations.
:pencil: Implement the `rebalance` method for the BST class.
## Balancing our BSTs [[toc](README.md#table-of-content)]
## Question 17. [[toc](README.md#table-of-content)]
:pencil: _balanced_ insert
### Question 18. [[toc](README.md#table-of-content)]
:pencil: Call `rebalance` at the end of `insert` and `delete`.
## Question 18. [[toc](README.md#table-of-content)]
:pencil: _balanced_ delete
:question: Can you run the same example as in the beginning, i.e. inserting
integers in ascending order and make sure that the tree is balanced at the end?
---
---
......
......@@ -28,3 +28,59 @@ def test_balance():
),
BinaryTree(2),
).balance() == -1
def test_rotate_right():
from tree import BinaryTree
assert BinaryTree().rotate_right() == BinaryTree()
assert BinaryTree(0).rotate_right() == BinaryTree(0)
t = {
"before": BinaryTree(
10,
BinaryTree(
5,
BinaryTree(1),
BinaryTree(8),
),
BinaryTree(20),
),
"after": BinaryTree(
5,
BinaryTree(1),
BinaryTree(
10,
BinaryTree(8),
BinaryTree(20),
),
)
}
assert t["before"].rotate_right() == t["after"]
def test_rotate_left():
from tree import BinaryTree
assert BinaryTree().rotate_left() == BinaryTree()
assert BinaryTree(0).rotate_left() == BinaryTree(0)
t = {
"before": BinaryTree(
10,
BinaryTree(5),
BinaryTree(
20,
BinaryTree(15),
BinaryTree(25),
),
),
"after": BinaryTree(
20,
BinaryTree(
10,
BinaryTree(5),
BinaryTree(15),
),
BinaryTree(25),
)
}
assert t["before"].rotate_left() == t["after"]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment