Dynamically Balanced Binary Search Trees - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Dynamically Balanced Binary Search Trees

Description:

DS - Dynamically Balanced BST. 2. Balanced Trees ... DS - Dynamically Balanced BST. 3. AVL Trees. A binary search tree with balance condition ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 17
Provided by: yon5
Category:

less

Transcript and Presenter's Notes

Title: Dynamically Balanced Binary Search Trees


1
Dynamically Balanced Binary Search Trees
2
Balanced Trees
  • Many algorithm exists to implement balanced
    trees.
  • Most are more complex than simple binary search
    trees
  • All take longer on average, for insertions and
    deletions.
  • However, they provide protection against the
    simple, extreme cases.
  • The AVL tree is one of the oldest forms of
    balanced search trees.
  • One of the alternatives to AVL is the red/black
    tree.

3
AVL Trees
  • A binary search tree with balance condition
  • For each node in the tree,
  • height of the left subtree height of the right
    subtree (,-) 1
  • Thus, all operations in O(lg n).

AvlNode AvlNode left AvlNode right
integer height
5
7
2
2
8
1
4
1
7
4
8
5
3
3
not an AVL tree (root)
an AVL tree
4
AVL Trees
  • Maintain height of each node.
  • During insertion, verify (and possibly correct)
    height of nodes on the path from inserted node
    to the root.
  • Insertion can violate the balance condition, for
    some node on the corrected path.
  • Violation is corrected after each insertion, if
    needed.
  • Corrections are made by rotation.
  • Go up the tree, correct height information, and
    correct detected violation, on the first node
    (closest to leaves) where violation has been
    detected.

5
AVL Trees
Node a is violating AVL balancing condition,
after insertion. Four cases 1. Insertion
into left subtree of left child 2.
Insertion into right subtree of left child 3.
Insertion into left subtree of right child 4.
Insertion into right subtree of right child
a
a
a
a
2
1
3
4
1 and 4 are symmetrical, 2 and 3 are
symmetrical (thus, theoretically two cases).
6
AVL Trees Correcting Balance ViolationI -
Single Rotation
  • Violation at external subtrees (right-right and
    left-left),

Single (right) rotation
insertion
(down one)
Corrected subtree has exactly the same height it
had prior to the insertion
(up one)
7
I - Single (right) Rotation Example
5
5
2
8
2
7
1
4
7
1
4
3
8
6
6
3
insert violation ( left-left for )
8
AVL Trees Correcting Balance ViolationI -
Single (left) Rotation
  • Violation at external subtrees (right-right),

Single (left) rotation
9
Longer Example
node with AVL-balance violation
2
3
2
2
1
4
3
3
1
1
5
2
2
4
4
4
2
5
1
1
3
6
5
3
5
3
6
1
10
Longer Example cont.
4
4
2
5
2
6
6
7
3
1
3
1
5
7
11
Correcting Balance Violationcases 2 and 3
  • In cases 2,3 (left-right, right-left) a single
    rotation does not solve the problem

Single right rotation
y
y
2
2
1
1
12
Correcting Balance ViolationII - Double
(left-right) Rotation
Decompose y Exactly one subtree in y is
2-levels deeper.
Left-right double rotation
a
b lt A, B lt a
b
b
a
lt
lt
B
A
B
A
2
(either or )
A,B to the right of b, to the left of a
y
13
II - Double (right-left) RotationExample
Right-left double rotation
4
4
2
2
7
6
1
6
15
3
1
15
3
5
16
5
16
7
14
4
14
2
6
1
7
3
14 is between 6 and 15 thus case 2, 3 (double
rot.)
5
15
16
14
14
Example (cont.)
13 is not between 4 and 7, thus case 1,4 (single
rot.)
4
4
2
7
2
7
6
15
6
15
3
1
3
5
16
5
16
14
14
13
7
7
4
4
15
15
2
6
13
2
6
16
16
14
1
3
1
5
3
5
12
13
14
12
13
15
Example (cont.)
9 is between 8,10. Double rot.
7
4
4
13
13
15
15
2
6
2
6
11
11
9
1
1
3
3
5
5
12
12
10
7
8
4
13
10
8
15
2
6
11
9
1
3
5
12
10
8
9
16
AVL Insert
Private AvlNode insert(Comparable x, AvlNode t)
x is the new node, t is a reference to the
tree if (t null) empty tree t new
AvlNode(x, null, null) nulls to the left and
right subtrees of x else if ( x.compareTo(
t.element) lt 0 ) t.left insert (x,
t.left) if (height(t.left) - height(t.right)
2) if( x.compareTo(t.left.element) lt 0
) t rotateWithLeftChild ( t
) else t doubleWithLeft( t ) else
if ( x.compareTo( t.element) gt 0 ) t.right
insert (x, t.right) if (height(t.right) -
height(t.left) 2) if( x.compareTo(t.
right.element) gt0 ) t rotateWithRightChild
( t ) else t doubleWithRight( t
) t.height max ( height( t.left ), height(
t.right)) 1 return t
Write a Comment
User Comments (0)
About PowerShow.com