Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Trees
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Model registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Paul Thevenon
Trees
Commits
13d12b13
Commit
13d12b13
authored
Jul 5, 2024
by
STEVAN Antoine
Browse files
Options
Downloads
Patches
Plain Diff
complete the class with the "balanced trees" part (mae-ac/trees!2)
parent
23a1264c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+5
-3
5 additions, 3 deletions
README.md
avl.md
+53
-7
53 additions, 7 deletions
avl.md
tests/test_balanced_tree.py
+56
-0
56 additions, 0 deletions
tests/test_balanced_tree.py
with
114 additions
and
10 deletions
README.md
+
5
−
3
View file @
13d12b13
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
avl.md
+
53
−
7
View file @
13d12b13
...
...
@@ -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

## 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 1
7
. [[toc](README.md#table-of-content)]
:pencil:
_balanced_ insert
##
#
Question 1
8
. [[toc](README.md#table-of-content)]
:pencil:
Call
`rebalance`
at the end of
`insert`
and
`delete`
.
## Q
uestion
18. [[toc](README.md#table-of-content)]
:pencil: _balanced_ delete
:q
uestion
: 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?
---
---
...
...
This diff is collapsed.
Click to expand it.
tests/test_balanced_tree.py
+
56
−
0
View file @
13d12b13
...
...
@@ -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
"
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment