avl trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · a binary tree...

17
AVL Trees (AVL Trees) Data Structures and Programming Spring 2017 1 / 17

Upload: others

Post on 03-Nov-2019

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

AVL Trees

(AVL Trees) Data Structures and Programming Spring 2017 1 / 17

Page 2: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Balanced Binary Tree

The disadvantage of a binary search tree is that its height can be aslarge as N-1This means that the time needed to perform insertion and deletionand many other operations can be O(N) in the worst caseWe want a tree with small heightA binary tree with N node has height at least Θ(log N)

Thus, our goal is to keep the height of a binary search tree O(logN)

Such trees are called balanced binary search trees. Examples are AVLtree, red-black tree.

(AVL Trees) Data Structures and Programming Spring 2017 2 / 17

Page 3: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

AVL Tree (Georgy Adelson-Velsky and EvgeniiLandis’ tree, 1962)

Height of a nodeI The height of a leaf is 1. The height of a null pointer is zero.I The height of an internal node is the maximum height of its

children plus 1

Note that this definition of height is different from the one we definedpreviously (we defined the height of a leaf as zero previously).

(AVL Trees) Data Structures and Programming Spring 2017 3 / 17

Page 4: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

AVL Tree (Cont’d)

An AVL tree is a binary search tree in which for every node in thetree, the height of the left and right subtrees differ by at most 1.

(AVL Trees) Data Structures and Programming Spring 2017 4 / 17

Page 5: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

AVL Tree (Cont’d)

Let x be the root of an AVL tree of height hLet Nh denote the minimum number of nodes in an AVL tree ofheight hClearly, Ni ≥ Ni−1 by definitionWe have

Nh ≥ Nh−1 + Nh−2 + 1 ≥ 2Nh−2 + 1 > 2Nh−2

By repeated substitution, we obtain the general form

Nh > 2iNh−2i

The boundary conditions are: N1 = 1 and N2 = 2. This impliesthat h = O(logNh).More precisely, the height of an n-node AVL tree is approx.1.44 log2 n.Thus, many operations (searching, insertion, deletion) on an AVLtree will take O(logN) time.

(AVL Trees) Data Structures and Programming Spring 2017 5 / 17

Page 6: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Smallest AVL Tree

(AVL Trees) Data Structures and Programming Spring 2017 6 / 17

Page 7: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Rotations

When the tree structure changes (e.g., insertion or deletion), weneed to transform the tree to restore the AVL tree property.This is done using single rotations or double rotations.Since an insertion/deletion involves adding/deleting a singlenode, this can only increase/decrease the height of some subtreeby 1Thus, if the AVL tree property is violated at a node x, it means thatthe heights of left(x) and right(x) differ by exactly 2.Rotations will be applied to x to restore the AVL tree property.

(AVL Trees) Data Structures and Programming Spring 2017 7 / 17

Page 8: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Insertion

First, insert the new key as a new leaf just as in ordinary binarysearch treeThen trace the path from the new leaf towards the root. For eachnode x encountered, check if heights of left(x) and right(x) differby at most 1.If yes, proceed to parent(x). If not, restructure by doing either asingle rotation or a double rotation [next slide].For insertion, once we perform a rotation at a node x, we won’tneed to perform any rotation at any ancestor of x.

(AVL Trees) Data Structures and Programming Spring 2017 8 / 17

Page 9: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Single rotation

The AVL-property is violated at xI height of left(x) is h+2I height of right(x) is h.

(AVL Trees) Data Structures and Programming Spring 2017 9 / 17

Page 10: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Double rotation

The new key is inserted in the subtree B1 or B2.The AVL-property is violated at x.x-y-z forms a zig-zag shape

(AVL Trees) Data Structures and Programming Spring 2017 10 / 17

Page 11: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Example - Rebalance after Insertion

(AVL Trees) Data Structures and Programming Spring 2017 11 / 17

Page 12: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Another Example

(AVL Trees) Data Structures and Programming Spring 2017 12 / 17

Page 13: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Another Example (Cont’d)

(AVL Trees) Data Structures and Programming Spring 2017 13 / 17

Page 14: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Deletion

The single rotations for deletion can be divided into 4 casesIn the following, a node is deleted in subtree C, causing the heightto drop to h. The height of y is h+2. When the height of subtree Ais h+1, the height of B can be h or h+1. Fortunately, the samesingle rotation can correct both cases.Imbalance may propagate upward so that many rotations may beneeded.

(AVL Trees) Data Structures and Programming Spring 2017 14 / 17

Page 15: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Example - Rebalance after Deletion (Single Rotation)

(AVL Trees) Data Structures and Programming Spring 2017 15 / 17

Page 16: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Example - Rebalance after Deletion (Double Rotation)

There are exactly two cases for double rotations (as in the case ofinsertion)Therefore, we can reuse exactly the same procedure for insertionto determine which rotation to perform

(AVL Trees) Data Structures and Programming Spring 2017 16 / 17

Page 17: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4b.pdf · A binary tree with N node has height at least ( logN) Thus, our goal is to keep the height of a

Pros and Cons of AVL Trees

Pros and Cons of AVL Trees1 Search is O(log N) since AVL trees are always balanced.2 Insertion and deletions are also O(log n)3 The height balancing adds no more than a constant factor to the

speed of insertion.Arguments against using AVL trees:

1 Difficult to program and debug; more space for balance factor.2 Asymptotically faster but rebalancing costs time.3 Most large searches are done in database systems on disk and use

other structures (e.g. B-trees).4 May be OK to have O(N) for a single operation if total run time for

many consecutive operations is fast (e.g. Splay trees).

(AVL Trees) Data Structures and Programming Spring 2017 17 / 17