0% found this document useful (0 votes)
17 views

Balanced+Trees REVIEW

Uploaded by

Jenesis Escobar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Balanced+Trees REVIEW

Uploaded by

Jenesis Escobar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Balanced Trees

REVIEW

So…remember binary search trees (BSTs)?


Quick recap:

The way a BST looks and performs depends on order of insertion of nodes:

Order 1: Order 2:

insert 4 insert 1
insert 2 insert 2
insert 6 insert 3
insert 1 insert 4
insert 3 insert 5
insert 5 insert 6

BEST CASE WORST CASE


BST BST

So, for regular BSTs


The “bushy” BST built with order 1 The BST built with insertion order 2
performs in the best case because it performs like a linked list. Common Best/Avg case Worst
has a height proportional to log(n) operations like search, insert, and case
instead of n. This means that common delete in a “spindly” tree like this Search O(log(n)) O(n)
operations like search, insert, and will be O(n)
Insert O(log(n)) O(n)
delete will all be O(log(n)).
Delete O(log(n)) O(n)

So now, the question is… Can we do better than this…?

AVL Trees root’s balance factor is 1 – 3 = 2

- AVL trees are BSTs that are balanced (“bushy”)


- Every node in an AVL tree has a balance factor = height (left subtree) – height (right subtree)
- If any node’s balance factor reaches a value outside [-1, 0, 1] → tree is not a valid AVL
and a rotation should be performed to fix imbalance.
AVL TREE ROTATIONS
Right-Right case
0
Right-Left case 0
-2
-2 -2

-1 -1
1

0 0 0 0
0 0 ALL ROTATIONS ARE
Left Rotate 0
Right Rotate Left Rotate O(1)
Left-Left case
0 Left Right case
0
2 2 2

1 -1 1

0 0 0 0
0 0 0
Right Rotate
Left Rotate Right Rotate
Insert (8) Insert (6) Insert (4) Stop here! There is an
imbalance, and we need
to rotate…

Right rotate
AVL TREE OPERATIONS

Insert/Delete – logic is same as for BST except Insert (10) Insert (9) Stop!
the AVL tree self-balances after every Right
left
insertion/deletion rotate
at 8
Notice: When inserting in an AVL tree,
we still must insert values LESS than node
to the LEFT of node, and values
GREATER than node to the RIGHT of
node, because valid AVL trees follow
BST properties (AVL trees are BSTs)
Continue…
Search – logic is same as for BST

Ex] 2-3 Tree (L = 2, N = 3)


B Trees
Each node has up to 3 children
Each node has up to 2 keys
- B Trees are N-ary trees that follow BST property
(Less than goes left, greater than goes right) Internal nodes can have
• Max of 2 keys
- Each node has up to N children • [2, 3] children
- Each node has up to L keys Leaf nodes can have
• [1, 2] keys
- Internal nodes can have: Root node can have Valid B- Tree
• Maximum of N -1 keys • [2,3] children
• [ceil(N/2), N] children
- Leaf nodes can have:
• [ceil(L/2), L] keys
- Root node is a leaf or can have:
• [2, N] children
B Tree Insertion: 2 – 3 Tree
Original Tree: Insert(1)
Break violation
node and bring
middle value = 3
up
5 goes up

Violation: L > 2 Violation: N > 3 L > 2 Perfectly Balanced ☺


B+ Trees
B+ trees are VERY similar to B trees, but there Example] The height of a B Tree is 3 (Level 0, Very Important:
are some important differences to know… 1, 2) and N = 3, L = 3.
What is the maximum number of unique keys For a B+ Tree, the
that this B Tree could contain? maximum number of
2 keys unique keys == maximum
number keys in leaves.

• All data is stored in the leaves – the 6 keys


inner nodes serve as a table of contents
for the data in the leaf nodes
• Leaves have pointers to each other Recall: The internal nodes of
forming a linked list 27 keys a B+ Tree simply act as a
• When inserting into a B+ tree, if you “table of contents” for the
“split a node” KEEP A COPY of the Remember, internal nodes can have a maximum of N -1 leaf nodes which actually
data moving up, in the leaves. keys. So, in this case they can have 3 – 1 = 2 keys. Leaves contain all the data.
can have L keys.

2 + 6 + 27 = 35 unique keys

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy