Balanced Binary Search Trees - PowerPoint PPT Presentation

1 / 72
About This Presentation
Title:

Balanced Binary Search Trees

Description:

AVL (Adelson-Velsky and Landis, 1962) trees (binary tree, internal memory applications) ... Applications of balanced trees same as for BST from last chapter. AVL Tree ... – PowerPoint PPT presentation

Number of Views:485
Avg rating:3.0/5.0
Slides: 73
Provided by: ith96
Category:
Tags: balanced | binary | bst | search | trees

less

Transcript and Presenter's Notes

Title: Balanced Binary Search Trees


1
Balanced Binary Search Trees
  • height is O(log n), where n is the number of
    elements in the tree
  • AVL (Adelson-Velsky and Landis, 1962) trees
    (binary tree, internal memory applications)
  • red-black trees (binary tree, internal memory)
  • get, put, and remove take O(log n) time

2
Balanced Binary Search Trees
  • Indexed AVL trees
  • Indexed red-black trees
  • Indexed operations also take O(log n) time

3
Balanced Search Trees
  • weight balanced binary search trees
  • B-tree degree gt 2, external memory applications
    (eg DB)
  • 2-3 trees
  • etc.

4
Dictionary Structures
5
Hashing
  • In practice hashing will outperform balanced
    search trees for search, insert, delete.
  • Balanced search trees used only when must
    guarantee a worst case time.
  • Balanced ST used when search and delete done by
    rank.
  • Balanced ST used when operations are not done by
    exact key match (eg, find smallest element with
    key larger than k).

6
Comparing Search Trees
  • Run-time performance of AVL and red-black trees
    similar.
  • Splay trees take less time to perform a sequence
    of u operations.
  • Splay trees easier to implement.

7
Comparing Search Trees
  • AVL and red-black trees use rotations to
    maintain balance.
  • AVL perform 1 rotation after an insert and O(log
    n) rotations after a delete.
  • red-black trees perform a single rotation
    following either an insert or delete.
  • In most applications a rotation takes Q(1) time
    so difference is not important.
  • In advanced applications rotations cannot be
    performed in constant time.
  • Example balanced priority search trees of
    McCreight. Represent 2D arrays. Each rotation
    costs O(log n).
  • RBT insert/delete remain at O(log n).
  • AVL insert O(log n) but delete is O(log2 n).

8
Comparing Search Trees
  • AVL, red-black, splay trees good when dictionary
    can fit into main memory.
  • B-tree good when dictionary is so large that it
    must be kept on disk.
  • In this case want high degree and smaller height.

9
Implementation
  • Some Java code available on web site (as solution
    to exercises).
  • The classes java.util.TreeMap and
    java.util.TreeSet use red-black trees.
  • Applications of balanced trees same as for BST
    from last chapter.

10
AVL Tree
  • binary tree, created in 1962 by Adelson-Velskii
    and Landis
  • Definition
  • An empty binary tree is an AVL tree
  • If T is a nonempty binary tree with TL and TR as
    its left and right subtrees, then T is an AVL
    tree iff
  • TL and TR are AVL trees and
  • hL - hR lt 1
  • where hL and hR are the heights of TL and TR
    respectively.

11
AVL Tree
  • An AVL search Tree is a binary search tree that
    is also an AVL tree.
  • An indexed AVL search tree is an indexed binary
    search tree that is also an AVL tree.
  • do not cover in this chapter
  • same techniques carry over to iAVL trees

12
AVL Tree
a and b are AVL trees. c is not (height
wrong). a is NOT an AVL search tree (not a search
tree) b is an AVL search tree.
13
AVL Tree
Both trees are AVL search trees.
14
AVL Tree
Both trees are indexed AVL search trees.
15
AVL TreeProperties, part 1
  • The height of an AVL tree with n elements is
    O(log n)
  • For every value of n, ngt0, there exists an AVL
    tree (otherwise some insertions cannot create an
    AVL tree).
  • An n-element AVL search tree can be searched in
    O(height) O(log n)

16
AVL TreeProperties, part 2
  • A new element can be inserted into an n-element
    AVL search tree so that the result is an n 1
    element AVL tree and can be done in O(log n)
    time.
  • An element can be deleted from an n-element AVL
    search tree, n gt 0, so that the result is an n -
    1 element AVL tree and can be done in O(log n)
    time.

17
Height
  • The height of an AVL tree that has n nodes is at
    most 1.44 log2 (n2).
  • The height of every n node binary tree is at
    least log2 (n1).

18
Height
  • The height of an AVL tree that has n nodes is at
    most 1.44log2 (n2).
  • Proof Let Nh be the minimum number of nodes in
    an AVL tree of height h.
  • worst case height of on of the subtrees is h-1
    and height of the other is h-2. Both are AVL
    trees. Hence
  • Nh Nh-1Nh-2 1, N00, N11.
  • Similar to the definitoin of the Fibonacci
    numbers
  • FnFn-1Fn-2, F00, F11

19
Height
  • The height of an AVL tree that has n nodes is at
    most 1.44 log2 (n2).
  • Proof (continued) can show
  • Nh Fh2-1 for h gt0.
  • From Fibonacci number theory, know that Fh
  • so
  • if there are n nodes in the tree then h is at
    most

20
AVL Tree
  • for every node x, define its balance factor
  • balance factor of x height of left subtree of x
  • - height of
    right subtree of x
  • balance factor of every node x is -1, 0, or 1

21
Balance Factors
-1
1
1
-1
0
1
0
0
-1
0
0
0
0
  • This is an AVL tree.

22
AVL Search Tree
23
AVL Search Tree
  • Searching. Use same code as for binary search
    tree.
  • Since height of an AVL tree with n elements is
    O(log n), search time is O(log n)
  • Inserting if use same code as for binary
    search tree, resulting tree may not be an AVL
    tree.

24
AVL Search Tree
  • Example insert 32 into figure

Resulting tree is unbalanced (left figure). Can
restore balance by shifting subtrees (right
figure).
25
AVL serach treeinserting observations
  • O1 In the unbalanced tree the balance factors
    are limited to -2, -1, 0, 1, and 2
  • O2 A node with balance factor 2 had a balance
    factor 1 before the insertion. A node with
    balance factor -2 had a balance factor -1 before
    the insertion.

26
AVL serach treeinserting observations
  • O3 The balance factors of only those nodes on
    the path from the root to the newly inserted node
    can change as a result of the insertion.
  • O4 Let A denote the nearest ancestor of the
    newly inserted node whose balance factor is
    either -2 or 2 (in previous example, the node A
    would be node 40). The balance factor of all
    nodes on the path from A to the newly inserted
    node was 0 prior to the insertion.

27
AVL serach treeinserting observations on node A
  • Node A may be identified while we are moving down
    from the root searching for the place to insert
    the new element.
  • From O2, bf(A) was either 1 or -1 prior to the
    insertion.
  • Let X denote the last node encountered that has
    bf 1 or -1.

28
AVL serach treeinserting observations on node X
  • Examples
  • if insert 32 into below AVL tree, X is 40

If insert 22, 28, 50 into below tree, X is node 25
29
AVL serach treeinserting observations on node X
  • When node X does not exist, all nodes on path
    from root to new node have balance factor 0 prior
    to insertion.
  • Thus tree cannot be unbalanced since insertion
    changes balance factors by -1, 0, or 1 and only
    bf on path to new node can be changed.

30
AVL serach treeinserting observations on node X
  • If bf (X) 0 after insertion, then the height of
    the subtree with X as root is the same before and
    after the insertion.
  • Example
  • if subtree rooted at X had height h before
    insertion and if bf(X) was 1,
  • then the height of its left subtree XL was h-1
  • and the height of its right subtree XR was
  • h-2

31
AVL serach treeinserting observations on node X
  • Example continued
  • For balance factor to become 0, insertion must be
    made in XR resulting in XR of height h-1
  • height of XR must increase to h-1 as all balance
    factors on path from X to new node were 0 prior
    to insertion.
  • height of X remains h, and balance factors of the
    ancestors of X are same before and after
    insertion, so tree is balanced.

32
AVL serach treeinserting observations on node X
  • Example continued
  • Only way tree can become unbalanced is if
    insertion causes bf(X) to change from -1 to -2 or
    1 to 2.
  • For latter to occur, insertion must be made in
    left subtree XL of X.
  • Now height of XL must become h because all
    balance factors
  • on path from X to new node were 0 prior to
    insertion.
  • Thus X is the node A referred to in O4.

33
AVL serach treeinserting classifying imbalances
  • Two main classes
  • L new node is in left subtree of A
  • R new node is in right subtree of A
  • Can refine
  • LL the left grandchild of A is on the path to
    new node.
  • Such a grandchild exists as the height of subtree
    of A that contains the new node must be at least
    2 for the bf(A) to be -2 or 2.
  • LR the right grandchild of A is on path to new
    node.
  • RR symmetrical situation to LR
  • RL symmetrical situation to LL

34
LL imbalance
  • Rotation
  • bf(B) 0, otherwise B would have been designated
    as the node A.
  • Node B becomes the root the subtree that A was
    previously root of.
  • BL remains the left subtree of B.
  • A becomes the root of Bs right subtree
  • BR becomes left subtree of A
  • right subtree of A is unchanged.

35
LL imbalance continued
  • Balance result
  • Balance factors of nodes in BL that are on the
    path from B to new node change
  • Balance factor of A changes
  • Remaining balance factors are same as before the
    rotation.
  • Heights of figures a and c below are same so
    balance factors of their ancestors are the same.
  • Thus a single LL rotation at A rebalances the
    entire tree.

36
LR imbalance
  • Rotation
  • Node C becomes the root of the subtree that A was
    previously root of.
  • BL remains the left subtree of B.
  • A becomes the root of Cs right subtree
  • CR becomes left subtree of A (if it exists)
  • CL becomes right subtree of B (if it exists)
  • right subtree AR of A is unchanged.

37
LR imbalance continued
  • Balance result
  • Balance factors of nodes B and A following the
    rearrangement depend on the value b of bf( C)
    just after the insertion but before the
    rearrangement.
  • Heights of figures a and c below are same so
    balance factors of their ancestors are the same.
  • Thus a single LR rotation at A rebalances the
    entire tree.

38
AVL rotations
  • RR and RL rotations are symmetric to LL and LR.
  • LL and RR imbalances called single rotations.
  • LR and RL imbalances are called double rotations.
  • A LR transformation can be viewed as an RR
    rotations followed by an LL rotation.
  • A RL transformation can be viewed as a LL
    rotation followed by an RR rotation.

39
AVL rotations
40
put(9)
-1
10
0
1
1
7
40
-1
0
1
-1
0
45
8
3
30
0
-1
0
0
0
0
60
35
9
1
20
5
0
25
41
put(29)
-1
10
1
1
7
40
-1
0
1
0
45
8
3
30
0
0
-1
0
0
-2
60
35
1
20
5
0
-1
RR imbalance gt new node is in right subtree of
right subtree of blue node (node with bf -2)
25
0
29
42
RR rotation
1
2
0
A
A
B
0
-1
0
AL
0
B
AL
B
BR
A
h
h
h1
h1
BL
BR
BL
BR
AL
BL
h
h
h
h1
h
h
before insertion
after inserting into BR
after RR rotation
RR imbalance gt new node is in right subtree of
right subtree of node (node with bf -2)
43
RR rotation
See web site.
44
put(29)
-1
10
1
1
7
40
-1
0
1
0
45
8
3
30
0
0
0
0
0
60
35
1
25
5
0
0
20
29
RR rotation.
45
put(27)
LR imbalance gt new node is in right subtree of
left subtree of blue node (node with bf 2)
-1
10
1
1
7
40
-1
0
2
0
45
8
3
30
0
-1
0
0
60
0
35
1
25
5
0
1
20
29
0
Node C from next slide is 29 so bf( C) 1
27
46
LR rotation
after inserting into BR
2
1
0
A
A
C
-1
0
AR
B
AR
B
A
B
h
h
C
BL
BR
BL
b
BL
CL
h
h
h
CR
AR
h
h
before insertion
after RR rotation
CL
CR
b 0 gt bf(B)bf(A)0 after rotation b 1 gt
bf(B)0 bf(A) -1 after rotation b -1 gt
bf(B)1 bf(A)0 after rotation
Node C from previous slide is 29 so bf( C) 1 gt
bf(B)0 bf(A) -1
47
put(27)
Node C was node 29 Node B was node 25 Node A was
node 30
-1
10
1
1
7
40
-1
0
0
0
45
29
8
3
0
-1
0
0
0
60
1
30
25
5
0
0
0
20
27
35
bf(B)0 bf(A) -1 as predicted on previous slide
48
AVL trees deletion
  • Delete as from a BST
  • Let q be the parent of the node that was
    physically deleted.

divert 20s right pointer to 30 and then q is 20
delete 25
so q is old node 15 (new node 12)
delete 15 its node is used by 12
node 12 is physically deleted
49
AVL trees deletion
  • Delete as from a BST
  • Let q be the parent of the node that was
    physically deleted.

delete 15 its node is used by 14
so q is node 13 (parent of old node 14)
13
10
node 14 is physically deleted
14
50
AVL trees deletion
  • Balance factors of some or all of the nodes on
    the path from the root to q change as result of
    deletion.
  • Must retrace this path backward from q toward
    root.
  • If deletion took place from the left subtree of q
    then bf(q) decreases by 1.
  • If deletion took place from right subtree of q
    then bf(q) increases by 1.

51
AVL trees deletion
  • D1 If the new balance factor of q is 0, its
    height has decreased by 1 we need to change the
    balance factor of its parent and possibly of its
    ancestors.
  • Recall that q is the parent of the physically
    deleted node.
  • so either qs left or right child has been
    deleted and the height of that subtree has
    decreased by 1.
  • Thus qs original height must have been 1 or -1.

0
1
0
-1
3
3
3
3
h
h
h1
h
h
h
h
h1
1
1
5
5
1
1
5
5
52
AVL trees deletion
  • D2 If the new balance factor of q is either -1
    or 1, its height is the same as before the
    deletion and the balance factors of its ancestors
    are unchanged.
  • Since we are deleting a node the height of some
    subtree has decreased. q is the parent of the
    deleted node.
  • But the height of the other subtree has not
    decreased, so the height of q remains the same.
  • To get bf(q) 1 or -1, the bf(q) before the
    deletion must have been 0.

1
-1
0
0
3
3
3
3
h-1
h
h
h-1
h
h
h
h
1
1
1
5
1
5
5
5
53
AVL trees deletion
  • D3 If the new balance factor of q is either -2
    or 2, the tree is unbalanced at q.

54
AVL trees deletion
  • From D1, the bf changes may propogate up the tree
    along the path from q to the root.
  • Thus some nodes on this path may have balance
    factor 2 or -2
  • Let A be the first such node on the path from q
    to the root.

55
AVL trees deletion
  • Classify the type of imbalance
  • If the deletion took place from As left subtree,
    it is an L imbalance.
  • Otherwise is an R imbalance.
  • If bf(A) 2 after deletion, must have been 1
    before deletion. So A must have a left subtree
    with root B
  • Subclassify R imbalances based on the bf of B
  • R0 if bf(B) 0
  • R1 if bf(B) 1
  • R-1 if bf(B) -1

56
AVL trees deletion
  • Subclassify R based on the bf of B
  • if bf(B) -1 and deletion took place from right
    subtree of A and this is classified as a R-1

1
2
A
A
h2
h2
-1
-1
h1
h1
B
AR
B
AR
h
h-1
BL
BL
C
C
h
h
h-1
h-1
57
AVL trees deletion
  • Subclassify R based on the bf of B
  • if bf(B) 1 and deletion took place from right
    subtree of A and this is classified as a R1

1
2
A
A
h2
h2
1
1
h1
h1
B
AR
B
AR
h
h-1
BL
BL
C
C
h-1
h-1
h
h
58
AVL trees deletion
  • Subclassify R based on the bf of B
  • if bf(B) 0 and deletion took place from right
    subtree of A and this is classified as a R0

1
2
A
A
h2
h2
0
0
h1
h1
B
AR
B
AR
h
h-1
BL
BL
C
C
h
h
h
h
59
AVL trees deletion
  • Subclassifications of L0, L1, L-1 are similar.
  • If bf(A) -2 after deletion, must have been -1
    before deletion. So A must have a right subtree
    with root B
  • Subclassify L imbalances based on the bf of B
  • L0 if bf(B) 0
  • L1 if bf(B) 1
  • L-1 if bf(B) -1

60
AVL trees deletion
  • Subclassify L based on the bf of B
  • if bf(B) -1 and deletion took place from left
    subtree of A and this is classified as a L-1

-1
-2
A
A
h3
h2
-1
-1
h
h-1
B
AL
B
AL
h1
h1
BL
BL
C
C
h-1
h
h-1
h
61
AVL trees deletion
  • Subclassify L based on the bf of B
  • if bf(B) 1 and deletion took place from left
    subtree of A and this is classified as a L1

-1
2
A
A
h2
h2
1
h1
h1
h-1
h
1
B
AL
B
AL
BL
BL
C
C
h-1
h-1
h
h
62
AVL trees deletion
  • Subclassify L based on the bf of B
  • if bf(B) 0 and deletion took place from left
    subtree of A and this is classified as a L0

-1
-2
A
A
h2
h2
0
0
h1
h1
AL
AL
B
B
h-1
h
BL
BL
C
C
h
h
h
h
63
AVL trees deletionR0 rotation (bf(B)0)
same as a LL rotation
1
2
-1
A
A
h2
h2
B
0
0
1
AR
B
AR
B
A
BL
h
h-1
h1
h
BL
BR
BL
BR
BR
AR
h
h
h
h
h
h-1
before deletion
after deleting from AR
after R0 rotation
Height of root after rotation is same as height
of root before deletion. So rest of tree must
be already balanced.
64
AVL trees deletion
  • Fixing an R0 imbalance.
  • Note that the height of the shown subtree was h2
    before rotation and is h2 after the rotation.
  • So balance factors of remaining nodes on path to
    root are unchanged. Entire tree has been
    rebalanced.

65
AVL trees deletionR1 rotation (bf(B) 1)
changes same as for R0
1
2
0
A
A
h2
h1
B
1
1
0
AR
B
AR
B
A
BL
h
h-1
h
h
BL
BR
BL
BR
BR
AR
h
h-1
h
h-1
h-1
h-1
before deleting
after deleting from AR
after R1 rotation
Height of root after rotation is different from
height of root before deletion. So must
traverse path to root to verify that tree is
balanced. Takes O(log n) rotations worst case.
66
R-1 rotation (bf(B) 1)
Same rotation as LR
2
1
0
A
A
C
-1
-1
AR
B
AR
B
A
B
h-1
h
C
h-1
BL
BR
BL
b
BL
CL
h-1
h
h-1
CR
AR
h-1
before deletion
after R-1 rotation
CL
CR
b 0 gt bf(B)bf(A)0 after rotation b 1 gt
bf(B)0 bf(A) -1 after rotation b -1 gt
bf(B)1 bf(A)0 after rotation
67
AVL Deletion Rotations
  • R0 rotation is same as LL rotation (including
    balance factors)
  • R1 rotation is same as LL rotation except that
    the final balance factors of A and B are
    different
  • R-1 and LR rotations are identical

68
Red Black Trees
  • Colored Nodes Definition
  • Binary search tree.
  • Each node is colored red or black.
  • Root and all external nodes are black.
  • No root-to-external-node path has two consecutive
    red nodes.
  • All root-to-external-node paths have the same
    number of black nodes

69
Example Red Black Tree
70
Red Black Trees
  • Colored Edges Definition
  • Binary search tree.
  • Child pointers are colored red or black.
  • Pointer to an external node is black.
  • No root to external node path has two consecutive
    red pointers.
  • Every root to external node path has the same
    number of black pointers.

71
Example Red Black Tree
72
Red Black Tree
  • The height of a red black tree that has n
    (internal) nodes is between log2(n1) and
    2log2(n1).
  • java.util.TreeMap gt red black tree
Write a Comment
User Comments (0)
About PowerShow.com