balanced binary search tree - kocwcontents.kocw.net/kocw/document/2014/hankukforeign/... · 2016....

16
Balanced Binary Search Tree (English slides) 신찬수

Upload: others

Post on 03-Feb-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

  • Balanced Binary Search Tree

    (English slides)

    신찬수

  • Complexities on Binary Search Tree • Search, insertion, and deletion on BST need O( h )

    – h = height of BST

    – h can be increased to n – 1

    – Thus all three operations take O( n ) time

    • Solution: maintain the height as less as possible under operations

    – BST is called balanced if its height is O( log n ) under operations.

    – AVL trees, red-black trees, 2-3 trees, splay trees

  • AVL trees

    • Guarantee the O(log n)-height by controlling the tree height directly.

    – maintain the height of left and right subtrees of every node differ by at most

    one. height rule

    – all leaves are on the last two levels

    15

    8

    3 11

    5

    18

    28

    21 33

    28

    21 33

    15

    8

    3 11

    5

    18

    28

    21 33

    28

    21 33

    16

  • When the height rule violates?

    • 25 is inserted:

    15

    8

    3 11

    5

    18

    21

    28

    33

    28

    33

    16

    25

  • When the height rule violates?

    • 25 is inserted:

    15

    8

    3 11

    5

    18

    28

    33

    33

    21

    28

    16

    25

    15

    8

    3 11

    5

    21

    28

    25 33

    28

    33

    18

    16

    right rotation

  • Rotations for balancing

    • Single rotations

    – Left rotation (figure) and right rotation

  • Rotations for balancing

    • Double rotations

    – Left-left, left-right, right-left (figure), right-right rotations

    x

    y

    z

    A

    B C

    D

    x

    z

    y

    A B

    C

    D

    x

    z

    y

    A B C D

  • Deletion 1. Delete the node of the key by copying with its predecessor.

    2. Balance the height if necessary by rotations while moving to root.

    15

    8

    3 11

    5

    18

    28

    21 33

    28

    21 33

    16

    15

    5

    3 11

    5

    18

    28

    21 33

    28

    21 33

    16

  • Animation

    http://www.seanet.com/users/arsen/avltree.html

    http://www.seanet.com/users/arsen/avltree.html

  • Red-black tree • A BST is called a red-black tree if the following

    four properties are satisfied:

    1. Every node is either red or black.

    2. Every null is assumed to be black.

    3. If a node is red, then both its children are black.

    4. Every simple path from a node to a descendant

    leaf contains the same number of black nodes.

    • Red-balck tree maintains O(log n)-height by

    controlling the path length to leaf.

  • Red-black tree • The height of a red-black tree of n nodes is at most (2log n + 1).

    – It means the longest path length is at most (2log n + 1).

    • This means that it takes O( log n ) time to search a node with a

    specific key.

    • Insertion and deletion will violate Red-Black property, so we need

    to restore the property (by rotations).

    – Insertion performs at most two rotations.

    – Deletion performs at most three rotations.

    http://www.seanet.com/users/arsen/avltree.html

    http://www.seanet.com/users/arsen/avltree.html

  • Splay tree • AVL tree and red-black tree enforces the height of the tree to be

    O( log n ) whenever insertion and deletion endanger the tree balance.

    • But it is not the only way to support efficient search, insert, delete operations.

    • Splay tree uses the locality of the access frequency:

    – The more frequently are key value accessed, the closer is it to the root.

    – Moving up the key to the root by rotation whenever it is accessed by operations.

    • With m operations, the amortized cost is O( m log n ) time, so the average time per operation is O( log n ). ( n is the maximum number of key values of the tree from start to end. )

  • Splaying to move a node to the root

    a a

  • Splaying to move a node to the root

    a

    a

  • Splaying to move a node to the root

    a

    a a

  • Search/insertion/deletion • Search

    1. Find the node containing the key value.

    2. Splay the node to the root.

    • Insertion

    1. Find the location to be inserted, and create a node there.

    2. Splay the node to the root.

    • Deletion

    1. Find the node to be deleted, and splay the node to the root.

    2. Delete the node (then you have left and right subtrees L and R)

    3. Splay the predecessor in L of the node to the root.

    4. Join L and R where R becomes right subtree of the root of L.