Balanced Binary Search Tree - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Balanced Binary Search Tree

Description:

height is O(log n), where n is the number of elements in the tree. AVL (Adelson-Velsky and Landis) trees. red-black trees. get, put, and remove take O(log n) time ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 36
Provided by: poste74
Category:

less

Transcript and Presenter's Notes

Title: Balanced Binary Search Tree


1
Balanced Binary Search Tree
  • Fall 2004
  • CSE, POSTECH

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

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

4
Balanced Search Trees
  • weight balanced binary search trees
  • 2-3 2-3-4 trees
  • AA trees
  • B-trees
  • BBST
  • etc.

5
AVL Tree
  • Definition
  • Binary 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 ? 1 where hL and hR are the heights of
    TL and TR, respectively

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

7
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).

8
AVL Search Tree
9
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
10
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
11
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.
12
AVL Rotations
  • RR
  • LL
  • RL
  • LR

13
Imbalance Types
  • After an insertion, when the balance factor of
    node A is 2 or 2, the node A is one of the
    following four imbalance types
  • LL new node is in the left subtree of the left
    subtree of A
  • LR new node is in the right subtree of the left
    subtree of A
  • RR new node is in the right subtree of the right
    subtree of A
  • RL new node is in the left subtree of the right
    subtree of A

14
Rotation
  • Definition
  • To switch children and parents among two or three
    adjacent nodes to restore balance of a tree.
  • A rotation may change the depth of some nodes,
    but does not change their relative ordering.

15
Left Rotation
  • Definition
  • In a binary search tree, pushing a node A down
    and to the left to balance the tree.
  • A's right child replaces A, and the right child's
    left child becomes A's right child.

16
Right Rotation
  • Definition
  • In a binary search tree, pushing a node A down
    and to the right to balance the tree.
  • A's left child replaces A, and the left child's
    right child becomes A's left child.
  • Animated rotation example http//www.cs.queensu.c
    a/home/jstewart/applets/bst/bst-rotation.html

17
AVL Rotations
  • To balance an unbalanced AVL tree (after an
    insertion), we may need to perform one of the
    following rotations LL, RR, LR, RL

18
An LL Rotation
19
An LR Rotation
20
Inserting into an AVL Search Tree
Insert(29)
  • Where is 29 going to be inserted into?
  • After the insertion, is the tree still an AVL
    search tree? (i.e., still balanced?)

21
Inserting into an AVL Search Tree
Insert(29)
  • What are the balance factors for 20, 25, 29?
  • RR imbalance ? new node is in the right subtree
    of right subtree of node 20 (node with bf -2)

22
After RR Rotation
  • What would the left subtree of 30 look like
    after an RR rotation?
  • After the RR rotation, is the resulting tree an
    AVL search tree now?

23
Single and Double Rotations
  • Single rotations the transformations done to
    correct LL and RR imbalances
  • Double rotations the transformations done to
    correct LR and RL imbalances
  • The transformation for an LR imbalance can be
    viewed as an RR rotation followed by an LL
    rotation
  • The transformation for an RL imbalance can be
    viewed as an LL rotation followed by an RR
    rotation (see Exercise 15)
  • See Figure 11.10 for the AVL-search-tree-insertion
    algorithm

24
Deletion from an AVL Search Tree
  • Deletion of a node may also produce an imbalance
  • Imbalance incurred by deletion is classified
    intothe types R0, R1, R-1, L0, L1, and L-1
  • Rotation is also needed for rebalancing
  • Read the observations after deleting a node from
    an AVL search tree
  • Read Section 11.2.6 for Deletion from an AVL
    search tree

25
An R0 Rotation
26
An R1 Rotation
27
An R-1 Rotation
28
Complexity of Dictionary OperationsSearch,
Insert, and Delete
Data Structure
Worst Case
Expected
Hash Table
O(n)
O(1)
Binary SearchTree
O(n)
O(log n)
BalancedBinary Search Tree
O(log n)
O(log n)
  • n is the number of elements in dictionary.

29
Complexity of Other OperationsAscend,
Search(rank), and Delete(rank)
Data Structure
ascend
get and remove
Hash Table
O(D n log n)
O(D n log n)
O(n)
Indexed BST
O(n)
IndexedBalanced BST
O(n)
O(log n)
  • D is the number of buckets.

30
Animated AVL Trees
  • http//www.seanet.com/users/arsen/avltree.html
  • http//www.cs.jhu.edu/goodrich/dsa/trees/avltree.
    html

31
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

32
Example Red Black Tree
33
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.

34
Example Red Black Tree
35
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