avl trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19f/chapter-4b.pdfsuch trees are...

33
AVL Trees (AVL Trees) Data Structures Fall 2019 1 / 33

Upload: others

Post on 08-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

AVL Trees

(AVL Trees) Data Structures Fall 2019 1 / 33

Page 2: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

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 Fall 2019 2 / 33

Page 3: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

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 Fall 2019 3 / 33

Page 4: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

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.An AVL tree maintains a ”balance factor” (BF(T) = Height(T.right)- Height(T.left) ) in each node of 0, 1, or -1 i.e. no node’s two childsubtrees differ in height by more than 1.

(AVL Trees) Data Structures Fall 2019 4 / 33

Page 5: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

AVL Tree Examples

(a) an AVL tree(b) not an AVL tree (unbalanced nodes are darkened)

(AVL Trees) Data Structures Fall 2019 5 / 33

Page 6: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Tracking subtree height of an AVL Tree

Many of the AVL tree operations depend on height.I Height can be computed recursively by walking the tree; too slow.I Instead, each node can keep track of its subtree height as a field.I Practically, we keep the balance factor instead.

(AVL Trees) Data Structures Fall 2019 6 / 33

Page 7: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

AVL Tree Height

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 Fall 2019 7 / 33

Page 8: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Smallest AVL Tree

(AVL Trees) Data Structures Fall 2019 8 / 33

Page 9: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

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 Fall 2019 9 / 33

Page 10: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

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 Fall 2019 10 / 33

Page 11: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

AVL Insertion Cases

Consider the lowest node k2 that has now become unbalanced.The new offending node could be in one of the four followinggrandchild subtrees, relative to k2:

(AVL Trees) Data Structures Fall 2019 11 / 33

Page 12: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Right rotation

Basic operation used in AVL trees: A left child could legally have itsparent as its right child.

(AVL Trees) Data Structures Fall 2019 12 / 33

Page 13: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Right rotation

Right rotation (clockwise): (fixes Case 1 (LL))

left child k1 becomes parentoriginal parent k2 demoted to rightk1’s original right subtree B (if any) is attached to k2 as left subtree

(AVL Trees) Data Structures Fall 2019 13 / 33

Page 14: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Right rotation Example

What is the balance factor of k2 before and after rotating?

(AVL Trees) Data Structures Fall 2019 14 / 33

Page 15: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Right Rotation Steps

Detach left child (11)’s right subtree (27) (don’t lose it!)Consider left child (11) be the new parent.Attach old parent (43) onto right of new parent (11).Attach new parent (11)’s old right subtree (27) as left subtree ofold parent (43).

(AVL Trees) Data Structures Fall 2019 15 / 33

Page 16: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Left Rotation

Left rotation (clockwise): (fixes Case 4 (RR))

right child k2 becomes parentoriginal parent k1 demoted to leftk2’s original left subtree B (if any) is attached to k1 as right subtree

(AVL Trees) Data Structures Fall 2019 16 / 33

Page 17: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Left Rotation Steps

Detach right child (65)’s left subtree (51) (don’t lose it!)Consider right child (65) be the new parent.Attach old parent (43) onto left of new parent (65).Attach new parent (65)’s old left subtree (51) as right subtree ofold parent (43).

(AVL Trees) Data Structures Fall 2019 17 / 33

Page 18: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Problem Cases

A single right rotation does not fix Case 2 (LR).

(Similarly, a single left rotation does not fix Case 3 (RL).)

(AVL Trees) Data Structures Fall 2019 18 / 33

Page 19: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Left-right Double Rotation

Left-right double rotation: (fixes Case 2 (LR))left-rotate k3’s left child (i.e., k1) ... reduces Case 2 into Case 1right-rotate k3 to fix Case 1

(AVL Trees) Data Structures Fall 2019 19 / 33

Page 20: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Left-right Rotation Example

What is the balance factor of k1, k2, k3 before and after rotating?

(AVL Trees) Data Structures Fall 2019 20 / 33

Page 21: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Right-left Double Rotation

Right-left double rotation: (fixes Case 3 (RL))right-rotate k1’s right child (i.e., k3) ... reduces Case 3 into Case 4left-rotate k1 to fix Case 4

(AVL Trees) Data Structures Fall 2019 21 / 33

Page 22: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Properties of General Insert + Single Rotation

Restores balance to a lowest point in tree where imbalance occurs

After rotation, height of the subtree (in the example, h+1) is thesame as it was before the insert that imbalanced it

Thus, no further rotations are needed anywhere in the tree!

(AVL Trees) Data Structures Fall 2019 22 / 33

Page 23: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Deletion (Really Easy Case)

(AVL Trees) Data Structures Fall 2019 23 / 33

Page 24: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Deletion (Pretty Easy Case)

(AVL Trees) Data Structures Fall 2019 24 / 33

Page 25: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Deletion (Pretty Easy Case cont’d)

(AVL Trees) Data Structures Fall 2019 25 / 33

Page 26: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Deletion (Hard Case #1)

(AVL Trees) Data Structures Fall 2019 26 / 33

Page 27: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Single Rotation on Deletion

What is different about deletion than insertion?

(AVL Trees) Data Structures Fall 2019 27 / 33

Page 28: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Deletion (Hard Case)

(AVL Trees) Data Structures Fall 2019 28 / 33

Page 29: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Double Rotation on Deletion

(AVL Trees) Data Structures Fall 2019 29 / 33

Page 30: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Deletion with Propagation

(AVL Trees) Data Structures Fall 2019 30 / 33

Page 31: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Propagated Single Rotation

(AVL Trees) Data Structures Fall 2019 31 / 33

Page 32: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

Propagated Double Rotation

Imbalance may propagate upward so that many rotations may beneeded.

(AVL Trees) Data Structures Fall 2019 32 / 33

Page 33: AVL Trees - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds19F/chapter-4b.pdfSuch trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees)

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

Can we guarantee O(log N) performance with less overhead?

(AVL Trees) Data Structures Fall 2019 33 / 33