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

Binary Search Trees: Balance: Daniel Kane

This document discusses binary search trees and the importance of balance. It notes that basic binary tree operations like find take time proportional to the depth of the tree, so trees can become unbalanced over time with insertions and deletions. To remedy this, the document introduces the idea of balancing trees so subtrees have approximately equal size to maintain logarithmic runtime. It also briefly outlines rotations as a way to rearrange nodes and balance the tree while preserving order.

Uploaded by

ljklefkj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Binary Search Trees: Balance: Daniel Kane

This document discusses binary search trees and the importance of balance. It notes that basic binary tree operations like find take time proportional to the depth of the tree, so trees can become unbalanced over time with insertions and deletions. To remedy this, the document introduces the idea of balancing trees so subtrees have approximately equal size to maintain logarithmic runtime. It also briefly outlines rotations as a way to rearrange nodes and balance the tree while preserving order.

Uploaded by

ljklefkj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Binary Search Trees:

Balance
Daniel Kane
Department of Computer Science and Engineering
University of California, San Diego

Data Structures
Data Structures and Algorithms
Learning Objectives
Think about the runtime of basic binary
tree operations.
Understand the motivation behind
binary search tree balance.
Implement a rotation.
Outline

1 Runtime

2 Balanced Trees

3 Rotations
Runtime

How long do Binary Search Tree operations


take?
Find
Find(5)

Number of operations = O(Depth)


Problem
Which nodes will be faster to search for in
the following tree?
Example I

Depth can be as bad as n.


Outline

1 Runtime

2 Balanced Trees

3 Rotations
Example II

Depth can be much smaller.


Balance

Want left and right subtrees to have


approximately the same size.
Balance

Want left and right subtrees to have


approximately the same size.
Suppose perfectly balanced:
Balance

Want left and right subtrees to have


approximately the same size.
Suppose perfectly balanced:
Each subtree half the size of its parent.
After log2 (n) levels, subtree of size 1.
Operations run in O(log(n)) time.
Problem
Insertions and deletions can destroy balance!
Problem
Insertions and deletions can destroy balance!
Problem
Insertions and deletions can destroy balance!
Problem
Insertions and deletions can destroy balance!
Problem
Insertions and deletions can destroy balance!
Outline

1 Runtime

2 Balanced Trees

3 Rotations
Rebalancing

Idea: Rearrange tree to maintain balance.


Rebalancing

Idea: Rearrange tree to maintain balance.


Problem: How do we rearrange tree while
maintaining order?
Rotations

A<Y <B <X <C


Implementation
RotateRight(X )
P ← X .Parent
Y ← X .Left
B ← Y .Right
Y .Parent ← P
P.AppropriateChild ← Y
X .Parent ← Y , Y .Right ← X
B.Parent ← X , X .Left ← B
Next Time

How to keep a tree balanced. AVL trees.

You might also like