Balanced Trees - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Balanced Trees

Description:

NILl. NILl. NILl. NILl. NILl. NILl. NILl. NILl ... NILl. NILl. NILl. NILl. Red nodes may be seen as 'fill-ins' to a complete binary search tree. ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 12
Provided by: toshi155
Category:
Tags: balanced | nill | trees

less

Transcript and Presenter's Notes

Title: Balanced Trees


1
Balanced Trees
Balanced trees have height O(lg n).
At each node, height of left and right subtrees
are close.
e.g. AVL trees, B-trees, red-black trees, splay
trees
At each node, number of nodes in left and right
subtrees are close.
O(lg n) time.
2
Rebalancing Heuristic -- Rotation
y
x
B
B
To be used by insertion and deletion on a
red-black tree.
3
An Example of Rotation
11
18
4
Red-Black Trees
A balanced binary search tree with height ?(lg
n).
Basic dynamic-set operations Search,
Predecessor, Successor, Minimum
Maximum, Insert, Delete all take O(lg n) time.
5
Node of a Red-Black Tree
color
parent
right
left
key
NIL as pointers to external nodes (leaves) of the
tree.
Key-bearing nodes as internal nodes of the tree.
6
Red-Black Properties
12
internal node
8
14
NILl
15
9
4
NILl
NILl
NILl
NILl
NILl
external node (requiring no extra storage)
5
NILl
NILl
1. Every node is either red or black.
No path is more than twice as long as any other.
2. The root is black.
3. Every leaf (NIL) is black.
4. If a node is red, then both of its children
are black.
5. Every simple path from the root to a
descendant leaf contains the same number of
black nodes.
7
Sentinel (Save Storage)
parent
12
8
14
15
4
9
5
nil(T)
8
All Nodes Are Black
A complete binary tree!
12
8
14
20
4
9
13
NILl
NILl
NILl
NILl
NILl
NILl
NILl
NILl
Red nodes may be seen as fill-ins to a complete
binary search tree.
9
Black-Height
The black-height of a node x, denoted bh(x), is
the number of black nodes on any path from x
(excluded) to a leaf.
bh 3
17
bh 2
21
14
10
23
19
16
7
NILl
NILl
20
15
NILl
NILl
12
3
NILl
NILl
NILl
NILl
NILl
NILl
NILl
A node at height h has black-height h/2.
NILl
NILl
10
Internal Nodes vs Black Height
Proof By induction on the height h of x.
h 0
h gt 0
11
Internal Nodes vs Height
Lemma
A red-black tree with n internal nodes has height
at most 2lg (n1).
Proof The root of a red-black tree of height h
has black-height ? h/2.
This is because on any path down from the root a
red node is always followed by a black node (but
not necessarily vice versa).
Write a Comment
User Comments (0)
About PowerShow.com