72
AVL Trees Version of September 6, 2016 AVL Trees Version of September 6, 2016 1 / 22

AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL TreesVersion of September 6, 2016

AVL Trees Version of September 6, 2016 1 / 22

Page 2: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Binary Search Trees

8

4 14

2 7 9

31 12

< x > x

x

Binary-search-tree property

For every node x

All keys in its left subtree are smaller than the key value in x

All keys in its right subtree are larger than the key value in x

AVL Trees Version of September 6, 2016 2 / 22

Page 3: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Binary Search Trees

8

4 14

2 7 9

31 12

< x > x

x

Binary-search-tree property

For every node x

All keys in its left subtree are smaller than the key value in x

All keys in its right subtree are larger than the key value in x

AVL Trees Version of September 6, 2016 2 / 22

Page 4: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Binary Search Trees

8

4 14

2 7 9

31 12

< x > x

x

Binary-search-tree property

For every node x

All keys in its left subtree are smaller than the key value in x

All keys in its right subtree are larger than the key value in x

AVL Trees Version of September 6, 2016 2 / 22

Page 5: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 6: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 7: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 8: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 9: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 10: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 11: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

8

4 14

2 7 9

31 120 0 0

0

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 12: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

8

4 14

2 7 9

31 120 0 0

01 1

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 13: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

8

4 14

2 7 9

31 120 0 0

01 1

2 2

3

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 14: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height

The height of a node in a tree is the number of edges on thelongest downward path from the node to a leaf

Node height= max(children height) +1

Leaves: height = 0

Tree height = root height

Empty tree: height = −1

Tree operations typically takeO(height) time

8

4 14

2 7 9

31 120 0 0

01 1

2 2

3

Question

Let n be the size of a binary search tree. How can we keep itsheight O(log n) under insertion and deletion?

AVL Trees Version of September 6, 2016 3 / 22

Page 15: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Balanced Binary Search Tree: AVL Tree

An AVL [Adelson-Velskii & Landis, 1962] tree is a binary searchtree in which

for every node in the tree, heights of its left and right subtreesdiffer by at most 1.

8

4 14

2 7 9

31 120 0 0

01 1

2 2

3

010 0 0

0 0 1

−1

1

−2

08

4 14

2 7 9

31 120 0 0

01 1

2 2

3

010 0 0

0 0 1

−1

1

−1

0

160

0

AVL Treenon-AVL Tree

The balance factor of a node is the height of its right subtreeminus the height of its left subtree.

A node with balance factor 1, 0 or −1 is considered balanced.

AVL Trees Version of September 6, 2016 4 / 22

Page 16: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Balanced Binary Search Tree: AVL Tree

An AVL [Adelson-Velskii & Landis, 1962] tree is a binary searchtree in which

for every node in the tree, heights of its left and right subtreesdiffer by at most 1.

8

4 14

2 7 9

31 120 0 0

01 1

2 2

3

010 0 0

0 0 1

−1

1

−2

08

4 14

2 7 9

31 120 0 0

01 1

2 2

3

010 0 0

0 0 1

−1

1

−1

0

160

0

AVL Treenon-AVL Tree

The balance factor of a node is the height of its right subtreeminus the height of its left subtree.

A node with balance factor 1, 0 or −1 is considered balanced.

AVL Trees Version of September 6, 2016 4 / 22

Page 17: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Balanced Binary Search Tree: AVL Tree

An AVL [Adelson-Velskii & Landis, 1962] tree is a binary searchtree in which

for every node in the tree, heights of its left and right subtreesdiffer by at most 1.

8

4 14

2 7 9

31 120 0 0

01 1

2 2

3

010 0 0

0 0 1

−1

1

−2

08

4 14

2 7 9

31 120 0 0

01 1

2 2

3

010 0 0

0 0 1

−1

1

−1

0

160

0

AVL Treenon-AVL Tree

The balance factor of a node is the height of its right subtreeminus the height of its left subtree.

A node with balance factor 1, 0 or −1 is considered balanced.

AVL Trees Version of September 6, 2016 4 / 22

Page 18: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees with Minimum Number of Nodes

Let nh be the minimum number of nodes in an AVL tree of height h

n0 = 1

AVL Trees Version of September 6, 2016 5 / 22

Page 19: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees with Minimum Number of Nodes

Let nh be the minimum number of nodes in an AVL tree of height h

n1 = 2n0 = 1

AVL Trees Version of September 6, 2016 5 / 22

Page 20: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees with Minimum Number of Nodes

Let nh be the minimum number of nodes in an AVL tree of height h

n2 = n1 + n0 + 1 = 4n1 = 2n0 = 1

AVL Trees Version of September 6, 2016 5 / 22

Page 21: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees with Minimum Number of Nodes

Let nh be the minimum number of nodes in an AVL tree of height h

n3 = n2 + n1 + 1 = 7n2 = n1 + n0 + 1 = 4n1 = 2n0 = 1

AVL Trees Version of September 6, 2016 5 / 22

Page 22: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height of AVL Tree

Let nh denote the minimum number of nodes in an AVL treeof height h

n0 = 1, n1 = 2 (base)

nh = nh−1 + nh−2 + 1

For any AVL tree of height h and size n,

n ≥ nh = nh−1 + nh−2 + 1 > 2nh−2 > 4nh−4 > · · · > 2inh−2i

If h is even, let i = h/2. ⇒ n > 2h/2n0 = 2h/2 · 1 ⇒h = O(log n)

If h is odd, let i = (h − 1)/2. ⇒ n > 2(h−1)/2n1 = 2(h−1)/2 · 2⇒ h = O(log n)

Thus, many operations (e.g., insertion, deletion, and search)on an AVL tree will take O(logn) time

AVL Trees Version of September 6, 2016 6 / 22

Page 23: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height of AVL Tree

Let nh denote the minimum number of nodes in an AVL treeof height h

n0 = 1, n1 = 2 (base)

nh = nh−1 + nh−2 + 1

For any AVL tree of height h and size n,

n ≥ nh = nh−1 + nh−2 + 1 > 2nh−2 > 4nh−4 > · · · > 2inh−2i

If h is even, let i = h/2. ⇒ n > 2h/2n0 = 2h/2 · 1 ⇒h = O(log n)

If h is odd, let i = (h − 1)/2. ⇒ n > 2(h−1)/2n1 = 2(h−1)/2 · 2⇒ h = O(log n)

Thus, many operations (e.g., insertion, deletion, and search)on an AVL tree will take O(logn) time

AVL Trees Version of September 6, 2016 6 / 22

Page 24: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height of AVL Tree

Let nh denote the minimum number of nodes in an AVL treeof height h

n0 = 1, n1 = 2 (base)

nh = nh−1 + nh−2 + 1

For any AVL tree of height h and size n,

n ≥ nh = nh−1 + nh−2 + 1 > 2nh−2 > 4nh−4 > · · · > 2inh−2i

If h is even, let i = h/2. ⇒ n > 2h/2n0 = 2h/2 · 1 ⇒h = O(log n)

If h is odd, let i = (h − 1)/2. ⇒ n > 2(h−1)/2n1 = 2(h−1)/2 · 2⇒ h = O(log n)

Thus, many operations (e.g., insertion, deletion, and search)on an AVL tree will take O(logn) time

AVL Trees Version of September 6, 2016 6 / 22

Page 25: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height of AVL Tree

Let nh denote the minimum number of nodes in an AVL treeof height h

n0 = 1, n1 = 2 (base)

nh = nh−1 + nh−2 + 1

For any AVL tree of height h and size n,

n ≥ nh = nh−1 + nh−2 + 1 > 2nh−2 > 4nh−4 > · · · > 2inh−2i

If h is even, let i = h/2. ⇒ n > 2h/2n0 = 2h/2 · 1 ⇒h = O(log n)

If h is odd, let i = (h − 1)/2. ⇒ n > 2(h−1)/2n1 = 2(h−1)/2 · 2⇒ h = O(log n)

Thus, many operations (e.g., insertion, deletion, and search)on an AVL tree will take O(logn) time

AVL Trees Version of September 6, 2016 6 / 22

Page 26: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Height of AVL Tree

Let nh denote the minimum number of nodes in an AVL treeof height h

n0 = 1, n1 = 2 (base)

nh = nh−1 + nh−2 + 1

For any AVL tree of height h and size n,

n ≥ nh = nh−1 + nh−2 + 1 > 2nh−2 > 4nh−4 > · · · > 2inh−2i

If h is even, let i = h/2. ⇒ n > 2h/2n0 = 2h/2 · 1 ⇒h = O(log n)

If h is odd, let i = (h − 1)/2. ⇒ n > 2(h−1)/2n1 = 2(h−1)/2 · 2⇒ h = O(log n)

Thus, many operations (e.g., insertion, deletion, and search)on an AVL tree will take O(logn) time

AVL Trees Version of September 6, 2016 6 / 22

Page 27: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees and Fibonacci Numbers

We saw that nh = nh−1 + nh−2 + 1.Recall Fibonacci numbers satisfy fh = fh−1 + fh−2. Now compare

h 0 1 2 3 4 5 6 7 8

nh 1 2 4 7 12 20 33 54 88

fh 1 1 2 3 5 8 13 21 34

Lemma:

nh = fh+2 − 1

Proof: by induction

nh+1 = 1+nh+nh−1 = 1+fh+2−1+nh+1−1 = fh+3−1

Since fh ∼ cφh for golden ratio φ = 1+√5

2 , this also immediatelyprovides alternative derivation that h = O(log n).

AVL Trees Version of September 6, 2016 7 / 22

Page 28: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees and Fibonacci Numbers

We saw that nh = nh−1 + nh−2 + 1.Recall Fibonacci numbers satisfy fh = fh−1 + fh−2. Now compare

h 0 1 2 3 4 5 6 7 8

nh 1 2 4 7 12 20 33 54 88

fh 1 1 2 3 5 8 13 21 34

Lemma:

nh = fh+2 − 1

Proof: by induction

nh+1 = 1+nh+nh−1 = 1+fh+2−1+nh+1−1 = fh+3−1

Since fh ∼ cφh for golden ratio φ = 1+√5

2 , this also immediatelyprovides alternative derivation that h = O(log n).

AVL Trees Version of September 6, 2016 7 / 22

Page 29: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees and Fibonacci Numbers

We saw that nh = nh−1 + nh−2 + 1.Recall Fibonacci numbers satisfy fh = fh−1 + fh−2. Now compare

h 0 1 2 3 4 5 6 7 8

nh 1 2 4 7 12 20 33 54 88

fh 1 1 2 3 5 8 13 21 34

Lemma:

nh = fh+2 − 1

Proof: by induction

nh+1 = 1+nh+nh−1 = 1+fh+2−1+nh+1−1 = fh+3−1

Since fh ∼ cφh for golden ratio φ = 1+√5

2 , this also immediatelyprovides alternative derivation that h = O(log n).

AVL Trees Version of September 6, 2016 7 / 22

Page 30: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

AVL Trees and Fibonacci Numbers

We saw that nh = nh−1 + nh−2 + 1.Recall Fibonacci numbers satisfy fh = fh−1 + fh−2. Now compare

h 0 1 2 3 4 5 6 7 8

nh 1 2 4 7 12 20 33 54 88

fh 1 1 2 3 5 8 13 21 34

Lemma:

nh = fh+2 − 1

Proof: by induction

nh+1 = 1+nh+nh−1 = 1+fh+2−1+nh+1−1 = fh+3−1

Since fh ∼ cφh for golden ratio φ = 1+√5

2 , this also immediatelyprovides alternative derivation that h = O(log n).

AVL Trees Version of September 6, 2016 7 / 22

Page 31: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion

Basically follows insertion strategy of binary search tree

But may cause violation of AVL tree property

Restore the destroyed height balance if needed

8

4 14

2 9

8

4 14

2 9

1

8

2 14

1 94

Insert 1 Restore height balance

AVL Trees Version of September 6, 2016 8 / 22

Page 32: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion

Basically follows insertion strategy of binary search tree

But may cause violation of AVL tree property

Restore the destroyed height balance if needed

8

4 14

2 9

8

4 14

2 9

1

8

2 14

1 94

Insert 1 Restore height balance

AVL Trees Version of September 6, 2016 8 / 22

Page 33: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion

Basically follows insertion strategy of binary search tree

But may cause violation of AVL tree property

Restore the destroyed height balance if needed

8

4 14

2 9

8

4 14

2 9

1

8

2 14

1 94

Insert 1 Restore height balance

AVL Trees Version of September 6, 2016 8 / 22

Page 34: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion

Basically follows insertion strategy of binary search tree

But may cause violation of AVL tree property

Restore the destroyed height balance if needed

8

4 14

2 9

8

4 14

2 9

1

8

2 14

1 94

Insert 1 Restore height balance

AVL Trees Version of September 6, 2016 8 / 22

Page 35: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Observation

After an insertion, only nodes that are on the path from theinsertion node to the root might have their balance altered

Because only those nodes have their subtrees altered

8

4 14

2 9

8

4 14

2 9

1

0

−1

0

−1

0

0

−1

−2

−1

−1

0

Insert 1

AVL Trees Version of September 6, 2016 9 / 22

Page 36: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Observation

After an insertion, only nodes that are on the path from theinsertion node to the root might have their balance altered

Because only those nodes have their subtrees altered

8

4 14

2 9

8

4 14

2 9

1

0

−1

0

−1

0

0

−1

−2

−1

−1

0

Insert 1

AVL Trees Version of September 6, 2016 9 / 22

Page 37: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Observation

After an insertion, only nodes that are on the path from theinsertion node to the root might have their balance altered

Because only those nodes have their subtrees altered

8

4 14

2 9

8

4 14

2 9

1

0

−1

0

−1

0

0

−1

−2

−1

−1

0

Insert 1

AVL Trees Version of September 6, 2016 9 / 22

Page 38: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Four Cases

Let A denote the lowest node violating AVL tree property

Case 1 (Left-Left case)— insert into the left subtree of the left child of A

Case 2 (Left-Right case)— insert into the right subtree of the left child of A

Case 3 (Right-Left case)— insert into the left subtree of the right child of A

Case 4 (Right-Right case)— insert into the right subtree of the right child of A

Cases 1 and 4 are mirror image symmetries with respect to A,as are cases 2 and 3

AVL Trees Version of September 6, 2016 10 / 22

Page 39: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Four Cases

Let A denote the lowest node violating AVL tree property

Case 1 (Left-Left case)— insert into the left subtree of the left child of A

Case 2 (Left-Right case)— insert into the right subtree of the left child of A

Case 3 (Right-Left case)— insert into the left subtree of the right child of A

Case 4 (Right-Right case)— insert into the right subtree of the right child of A

Cases 1 and 4 are mirror image symmetries with respect to A,as are cases 2 and 3

AVL Trees Version of September 6, 2016 10 / 22

Page 40: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Four Cases

Let A denote the lowest node violating AVL tree property

Case 1 (Left-Left case)— insert into the left subtree of the left child of A

Case 2 (Left-Right case)— insert into the right subtree of the left child of A

Case 3 (Right-Left case)— insert into the left subtree of the right child of A

Case 4 (Right-Right case)— insert into the right subtree of the right child of A

Cases 1 and 4 are mirror image symmetries with respect to A,as are cases 2 and 3

AVL Trees Version of September 6, 2016 10 / 22

Page 41: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Four Cases

Let A denote the lowest node violating AVL tree property

Case 1 (Left-Left case)— insert into the left subtree of the left child of A

Case 2 (Left-Right case)— insert into the right subtree of the left child of A

Case 3 (Right-Left case)— insert into the left subtree of the right child of A

Case 4 (Right-Right case)— insert into the right subtree of the right child of A

Cases 1 and 4 are mirror image symmetries with respect to A,as are cases 2 and 3

AVL Trees Version of September 6, 2016 10 / 22

Page 42: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Four Cases

Let A denote the lowest node violating AVL tree property

Case 1 (Left-Left case)— insert into the left subtree of the left child of A

Case 2 (Left-Right case)— insert into the right subtree of the left child of A

Case 3 (Right-Left case)— insert into the left subtree of the right child of A

Case 4 (Right-Right case)— insert into the right subtree of the right child of A

Cases 1 and 4 are mirror image symmetries with respect to A,as are cases 2 and 3

AVL Trees Version of September 6, 2016 10 / 22

Page 43: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Four Cases

Let A denote the lowest node violating AVL tree property

Case 1 (Left-Left case)— insert into the left subtree of the left child of A

Case 2 (Left-Right case)— insert into the right subtree of the left child of A

Case 3 (Right-Left case)— insert into the left subtree of the right child of A

Case 4 (Right-Right case)— insert into the right subtree of the right child of A

Cases 1 and 4 are mirror image symmetries with respect to A,as are cases 2 and 3

AVL Trees Version of September 6, 2016 10 / 22

Page 44: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Left-Left Case

A

B

−1

0

α β

γ

k k

k

A

B

−2

−1

α β

γ

k + 1k

k

k + 2 k + 3

B

0

αk + 1

k + 2

A

0

β γk k

• Right rotation with B as thepivot

• The new subtree rooted at B hasheight k + 2, exactly the sameheight before the insertion

• The rest of the tree (if any) thatwas originally above node Aalways remains balanced

AVL Trees Version of September 6, 2016 11 / 22

Page 45: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Right-Right Case

A

B

1

0

β γ

α

k k

k

A

B

2

1

γβ

α

k + 1k

k

k + 2 k + 3

B

0

k + 2

A

0

α βk k

• Left rotation with B as the pivot

• The new subtree rooted at B hasheight k + 2, exactly the sameheight before the insertion

• The rest of the tree (if any) thatwas originally above node Aalways remains balanced

γk + 1

AVL Trees Version of September 6, 2016 12 / 22

Page 46: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Left-Right Case

A

B

−1

0

α β

γ

k k

k

A

B

−2

1

α β

γ

k k + 1

k

k + 2 k + 3

B

2

k + 3

A

-1

γk

• Single rotation fails to fix it

• Subtree β is too tall

αk

βk + 1

AVL Trees Version of September 6, 2016 13 / 22

Page 47: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Left-Right Case: Special Case

When subtree α, β and γ are empty, k = −1. Insert C :

A

B

−1

0

A

B

−21

2

0

1

C

B

0

A

C

−2

−1

2

C

B

0

0

1

0

A

Left rotation and then right rotation with C as the pivot.Done!

AVL Trees Version of September 6, 2016 14 / 22

Page 48: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Left-Right Case: Special Case

When subtree α, β and γ are empty, k = −1. Insert C :

A

B

−1

0

A

B

−21

2

0

1

C

B

0

A

C

−2

−1

2

C

B

0

0

1

0

A

Left rotation and then right rotation with C as the pivot.Done!

AVL Trees Version of September 6, 2016 14 / 22

Page 49: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Left-Right Case: General Case 1

A

B

−1

0

δ

k

k + 2

X

β γ

k − 1

k − 1

0

B

0

A

X

−2

−2

k + 3

Xk + 2

A

1

γ

k − 1

0

α

k

A

B

−2

1

δ

k

k + 3

X

β

γ k − 1

−1

α

k

k

δ

k

γk − 1

α

k

β k

B

0

α

k

β k δ

k

AVL Trees Version of September 6, 2016 15 / 22

Page 50: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Left-Right Case: General Case 2

A

B

−1

0

δ

k

k + 2

X

β γ

k − 1

k − 1

0

B

−1

A

X

−2

−1

k + 3

Xk + 2

β

k − 1

0

α

k

A

B

−2

1

δ

k

k + 3

X

γ

k

1

α

k

δ

k

α

kk

γ kα

k

βk − 1

β

k − 1

γk

A

0

B

−1

δ

AVL Trees Version of September 6, 2016 16 / 22

Page 51: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Right-Left Case: Special Case

When subtree α, β and γ are empty, k = −1. Insert C :

C

A

0

0

1

0

B

A

2

−1

A

1

1

B

0

C

0

B

A

2

2

C

1

B

0

2

Right rotation and then left rotation with C as the pivot.Done!

AVL Trees Version of September 6, 2016 17 / 22

Page 52: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Right-Left Case: Special Case

When subtree α, β and γ are empty, k = −1. Insert C :

C

A

0

0

1

0

B

A

2

−1

A

1

1

B

0

C

0

B

A

2

2

C

1

B

0

2

Right rotation and then left rotation with C as the pivot.Done!

AVL Trees Version of September 6, 2016 17 / 22

Page 53: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Summary

Left-Left case: single rotation

— Right rotation

Left-Right case: double rotation

— Left rotation and then right rotation

Right-Left case: double rotation

— Right rotation and then left rotation

Right-Right case: single rotation

— Left rotation

For each insertion, at most two rotations are needed to restore theheight balance of the entire tree.

Note that in all cases, height of rebalanced subtree is unchanged!This means no further tree modifications are needed.

AVL Trees Version of September 6, 2016 18 / 22

Page 54: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Summary

Left-Left case: single rotation

— Right rotation

Left-Right case: double rotation

— Left rotation and then right rotation

Right-Left case: double rotation

— Right rotation and then left rotation

Right-Right case: single rotation

— Left rotation

For each insertion, at most two rotations are needed to restore theheight balance of the entire tree.

Note that in all cases, height of rebalanced subtree is unchanged!This means no further tree modifications are needed.

AVL Trees Version of September 6, 2016 18 / 22

Page 55: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Summary

Left-Left case: single rotation

— Right rotation

Left-Right case: double rotation

— Left rotation and then right rotation

Right-Left case: double rotation

— Right rotation and then left rotation

Right-Right case: single rotation

— Left rotation

For each insertion, at most two rotations are needed to restore theheight balance of the entire tree.

Note that in all cases, height of rebalanced subtree is unchanged!This means no further tree modifications are needed.

AVL Trees Version of September 6, 2016 18 / 22

Page 56: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Summary

Left-Left case: single rotation

— Right rotation

Left-Right case: double rotation

— Left rotation and then right rotation

Right-Left case: double rotation

— Right rotation and then left rotation

Right-Right case: single rotation

— Left rotation

For each insertion, at most two rotations are needed to restore theheight balance of the entire tree.

Note that in all cases, height of rebalanced subtree is unchanged!This means no further tree modifications are needed.

AVL Trees Version of September 6, 2016 18 / 22

Page 57: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Summary

Left-Left case: single rotation

— Right rotation

Left-Right case: double rotation

— Left rotation and then right rotation

Right-Left case: double rotation

— Right rotation and then left rotation

Right-Right case: single rotation

— Left rotation

For each insertion, at most two rotations are needed to restore theheight balance of the entire tree.

Note that in all cases, height of rebalanced subtree is unchanged!This means no further tree modifications are needed.

AVL Trees Version of September 6, 2016 18 / 22

Page 58: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Insertion: Summary

Left-Left case: single rotation

— Right rotation

Left-Right case: double rotation

— Left rotation and then right rotation

Right-Left case: double rotation

— Right rotation and then left rotation

Right-Right case: single rotation

— Left rotation

For each insertion, at most two rotations are needed to restore theheight balance of the entire tree.

Note that in all cases, height of rebalanced subtree is unchanged!This means no further tree modifications are needed.

AVL Trees Version of September 6, 2016 18 / 22

Page 59: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion

Delete a node as in ordinary binary search tree

If the node is a leaf, remove it

If not, replace it with either the largest in its left subtree orthe smallest in its right subtree, and remove that nodeNote: The replacement node has at most one subtree

Trace the path from the parent of removed node to root

For each node along path, restore the height balance if neededby doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22

Page 60: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion

Delete a node as in ordinary binary search tree

If the node is a leaf, remove it

If not, replace it with either the largest in its left subtree orthe smallest in its right subtree, and remove that nodeNote: The replacement node has at most one subtree

Trace the path from the parent of removed node to root

For each node along path, restore the height balance if neededby doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22

Page 61: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion

Delete a node as in ordinary binary search tree

If the node is a leaf, remove it

If not, replace it with either the largest in its left subtree orthe smallest in its right subtree, and remove that node

Note: The replacement node has at most one subtree

Trace the path from the parent of removed node to root

For each node along path, restore the height balance if neededby doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22

Page 62: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion

Delete a node as in ordinary binary search tree

If the node is a leaf, remove it

If not, replace it with either the largest in its left subtree orthe smallest in its right subtree, and remove that nodeNote: The replacement node has at most one subtree

Trace the path from the parent of removed node to root

For each node along path, restore the height balance if neededby doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22

Page 63: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion

Delete a node as in ordinary binary search tree

If the node is a leaf, remove it

If not, replace it with either the largest in its left subtree orthe smallest in its right subtree, and remove that nodeNote: The replacement node has at most one subtree

Trace the path from the parent of removed node to root

For each node along path, restore the height balance if neededby doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22

Page 64: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion

Delete a node as in ordinary binary search tree

If the node is a leaf, remove it

If not, replace it with either the largest in its left subtree orthe smallest in its right subtree, and remove that nodeNote: The replacement node has at most one subtree

Trace the path from the parent of removed node to root

For each node along path, restore the height balance if neededby doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22

Page 65: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion

Delete a node as in ordinary binary search tree

If the node is a leaf, remove it

If not, replace it with either the largest in its left subtree orthe smallest in its right subtree, and remove that nodeNote: The replacement node has at most one subtree

Trace the path from the parent of removed node to root

For each node along path, restore the height balance if neededby doing single or double rotations

AVL Trees Version of September 6, 2016 19 / 22

Page 66: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion (continued)

For each node along the path, restore the height balance if neededby doing single or double rotations

Very similar to insertion with one major caveat

In insertion a (single/double) rotation restored balance andkept height of rebalanced subtree unchanged.⇒ Only one rotation needed.In deletion, rotation restores balance but final height of rotatedsubtree might decrease by one. If this occurs, need to continuewalking up path towards root, continuing to restoring balanceby rotations when necessary.Since path has length O(h) this might require O(h) =O(log n) rotations.

⇒ Deletion can also be done in O(log n) time.

AVL Trees Version of September 6, 2016 20 / 22

Page 67: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion (continued)

For each node along the path, restore the height balance if neededby doing single or double rotations

Very similar to insertion with one major caveat

In insertion a (single/double) rotation restored balance andkept height of rebalanced subtree unchanged.⇒ Only one rotation needed.In deletion, rotation restores balance but final height of rotatedsubtree might decrease by one. If this occurs, need to continuewalking up path towards root, continuing to restoring balanceby rotations when necessary.Since path has length O(h) this might require O(h) =O(log n) rotations.

⇒ Deletion can also be done in O(log n) time.

AVL Trees Version of September 6, 2016 20 / 22

Page 68: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion (continued)

For each node along the path, restore the height balance if neededby doing single or double rotations

Very similar to insertion with one major caveat

In insertion a (single/double) rotation restored balance andkept height of rebalanced subtree unchanged.⇒ Only one rotation needed.In deletion, rotation restores balance but final height of rotatedsubtree might decrease by one. If this occurs, need to continuewalking up path towards root, continuing to restoring balanceby rotations when necessary.

Since path has length O(h) this might require O(h) =O(log n) rotations.

⇒ Deletion can also be done in O(log n) time.

AVL Trees Version of September 6, 2016 20 / 22

Page 69: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion (continued)

For each node along the path, restore the height balance if neededby doing single or double rotations

Very similar to insertion with one major caveat

In insertion a (single/double) rotation restored balance andkept height of rebalanced subtree unchanged.⇒ Only one rotation needed.In deletion, rotation restores balance but final height of rotatedsubtree might decrease by one. If this occurs, need to continuewalking up path towards root, continuing to restoring balanceby rotations when necessary.Since path has length O(h) this might require O(h) =O(log n) rotations.

⇒ Deletion can also be done in O(log n) time.

AVL Trees Version of September 6, 2016 20 / 22

Page 70: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion (continued)

For each node along the path, restore the height balance if neededby doing single or double rotations

Very similar to insertion with one major caveat

In insertion a (single/double) rotation restored balance andkept height of rebalanced subtree unchanged.⇒ Only one rotation needed.In deletion, rotation restores balance but final height of rotatedsubtree might decrease by one. If this occurs, need to continuewalking up path towards root, continuing to restoring balanceby rotations when necessary.Since path has length O(h) this might require O(h) =O(log n) rotations.

⇒ Deletion can also be done in O(log n) time.

AVL Trees Version of September 6, 2016 20 / 22

Page 71: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Deletion Example

Diagram below illustrates example in which subtree rooted at Ahas height k + 3. An item is deleted from subtree γ, reducing itsheight from k + 1 to k , leading to an imbalance.

After a single rotation, the subtree is now rooted at B with noimbalance. But, B has height k + 2. This might cause animbalance further up the tree, so the algorithm might need tocontinue walking upwards, correcting that imbalance.

A

B

−1

-1

α β

γ

k + 1 k

k + 1

k + 3

A

B

0

0

α

β γk k

k + 1

k + 2

AVL Trees Version of September 6, 2016 21 / 22

Page 72: AVL Trees Version of September 6, 2016AVL Trees Version of September 6, 2016 2 / 22 Binary Search Trees 8 4 14 2 7 9 1 3 12 < x > x x Binary-search-tree property For every node

Going Further

AVL trees are one particular type of Balanced Search trees, yieldingO(log n) behavior for dictionary operations.

There are many other types of Balanced Search Trees, e.g.

red-black trees

B-trees

(a, b) trees(2, 3) and (2, 3, 4) trees are special cases

treaps (randomized BSTs)

splay Trees (only O(log n) in amortized sense)

AVL Trees Version of September 6, 2016 22 / 22