CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary...

23
Outline CSI33 Data Structures Department of Mathematics and Computer Science Bronx Community College October 26, 2016 CSI33 Data Structures

Transcript of CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary...

Page 1: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Outline

CSI33 Data Structures

Department of Mathematics and Computer ScienceBronx Community College

October 26, 2016

CSI33 Data Structures

Page 2: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Outline

Outline

1 Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

CSI33 Data Structures

Page 3: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

Outline

1 Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

CSI33 Data Structures

Page 4: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

Improving The Worst-Case Performance forBSTs

The Worst Case Scenario

In the worst case, a binary search tree looks like a linked list,with all the links going the same way.

The performance of the important methods (find, insert,

delete) is Θ(n).

1

2

3

4

CSI33 Data Structures

Page 5: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

Improving The Worst-Case Performance forBSTs

Goal: Keeping Any BST “Balanced”

Ideally, to prevent a BST from becoming too unbalanced, itwould be filled so that as many nodes as possible have leftand right subtrees. This would be equivalent to being acomplete binary tree.

This is impractical, since it would take too long to rearrangethe nodes for the tree to keep this shape every time a newnode gets added or deleted.

CSI33 Data Structures

Page 6: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

Improving The Worst-Case Performance forBSTs

A Workable Compromise

We will only insist that, for a BST to be “balanced”, anynode will have the property that the depths of its left andright subtrees will differ by one level at most.

This can be efficiently enforced each time a node is inserted ordeleted.

The worst case height is about 1.44 log(n).

The performance of the insert, delete, and find operations isΘ(log n).

CSI33 Data Structures

Page 7: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

Basic Facts

The AVL Tree property

An AVL tree is a binary search tree (so it has the Binary SearchProperty), which has the additional AVL Tree Property that forevery node, the depths of its left and right subtrees will differ by atmost one level.

CSI33 Data Structures

Page 8: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

Basic Facts

The AVL Tree property

An AVL tree is a binary search tree (so it has the Binary SearchProperty), which has the additional AVL Tree Property that forevery node, the depths of its left and right subtrees will differ by atmost one level.

Inventors

Such a tree is called an AVL Tree after its two co-inventors, G. M.Adelson-Velskii and E. M. Landis.

CSI33 Data Structures

Page 9: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

Normal BST Insertion

A value gets inserted into a BST by comparing its value withthe current node (starting with the root).

If the value is less, it changes the current node to the leftsubtree if it exists.

If the value is greater, it changes the current node to the rightsubtree if it exists.

If the value is equal, an error has occurred: value is already inthe tree.

The new node is made a leaf when the subtree on that sidedoesn’t exist.

CSI33 Data Structures

Page 10: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Insertion: Overview

The height of each subtree is saved as a new attribute ofevery TreeNode object.

Perform the insertion to the proper subtree (say, the leftsubtree).

If the left subtree height is now 2 more than the right subtree,rebalance the tree at the current node.

Similarly for the right subtree.

Height of the current node = max(height left subtree, heightright subtree)+1.

CSI33 Data Structures

Page 11: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Insertion: Overview

def insert(self, value):

self.root = self. insert help(self.root, value)

def insert help(self, t, value):

if t is None:

t = TreeNode(value)

elif value < t.item:

t.left = self. insert help(t.left, value)

if get height(t.left) - get height(t.right) == 2:

if value < t.left.item:

t = self. left single rotate(t)

else:

t = self. right left rotate(t)

else:

# exercise for reader

t.height = max(get height(t.left), get height(t.right)) + 1

return t

CSI33 Data Structures

Page 12: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Rebalancing# determine which subtree the new value was inserted

if value < t.left.item:

# insertion into left subtree of left child

t = self. left single rotate(t)

else:

# insertion into right subtree of left child

t = self. right left rotate(t)

CSI33 Data Structures

Page 13: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Rebalancing: Double Rotation

def right left rotate(self, t):

t.left =

self. right single rotate(t.left)

t = self. left single rotate(t)

return t3

5

4

2

6

8

t

CSI33 Data Structures

Page 14: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Right Subtree Insertion: Rebalancing at node t

def right single rotate(self,

t):

grandparent = t

parent = t.right

grandparent.right =

parent.left

parent.left = grandparent

t = parent

# adjust heights of

grandparent, parent

return t

3

5

4

2

6

8t

CSI33 Data Structures

Page 15: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Right Subtree Insertion: Rebalancing at node t

def right single rotate(self,

t):

grandparent = t

parent = t.right

grandparent.right =

parent.left

parent.left = grandparent

t = parent

# adjust heights of

grandparent, parent

return t

5

NONE

4

2

3t, grandparent

parent

CSI33 Data Structures

Page 16: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Right Subtree Insertion: Rebalancing at node t

def right single rotate(self,

t):

grandparent = t

parent = t.right

grandparent.right =

parent.left

parent.left = grandparent

t = parent

# adjust heights of

grandparent, parent

return t

42

35

NONE

t, grandparent parent

CSI33 Data Structures

Page 17: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Right Subtree Insertion: Rebalancing at node t

def right single rotate(self,

t):

grandparent = t

parent = t.right

grandparent.right =

parent.left

parent.left = grandparent

t = parent

# adjust heights of

grandparent, parent

return t

grandparent

NONE

4

5

3

2

t, parent

CSI33 Data Structures

Page 18: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Right Subtree Insertion: Rebalancing at node t

def right single rotate(self,

t):

grandparent = t

parent = t.right

grandparent.right =

parent.left

parent.left = grandparent

t = parent

# adjust heights of

grandparent, parent

return t2

6

8

4

5

3

parentt

grandparent

CSI33 Data Structures

Page 19: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Left Subtree Insertion: Rebalancing at node t

def left single rotate(self,

t):

grandparent = t

parent = t.left

grandparent.left =

parent.right

parent.right = grandparent

t = parent

# adjust heights of

grandparent, parent

return t2

6

8

4

5

3

t

CSI33 Data Structures

Page 20: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Left Subtree Insertion: Rebalancing at node t

def left single rotate(self,

t):

grandparent = t

parent = t.left

grandparent.left =

parent.right

parent.right = grandparent

t = parent

# adjust heights of

grandparent, parent

return t NONE

8

6

2

5

3

4

t, grandparent

parent

CSI33 Data Structures

Page 21: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Left Subtree Insertion: Rebalancing at node t

def left single rotate(self,

t):

grandparent = t

parent = t.left

grandparent.left =

parent.right

parent.right = grandparent

t = parent

# adjust heights of

grandparent, parent

return t

8

6

NONE

2

5

3

4

parent t, grandparent

CSI33 Data Structures

Page 22: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Left Subtree Insertion: Rebalancing at node t

def left single rotate(self,

t):

grandparent = t

parent = t.left

grandparent.left =

parent.right

parent.right = grandparent

t = parent

# adjust heights of

grandparent, parent

return t

grandparent

2

5

8

63

4

NONE

t, parent

CSI33 Data Structures

Page 23: CSI33 Data Structures - fsw01.bcc.cuny.edu 2016/CSI33/Day14.pdf · Section 13.3: Balanced Binary Search Trees Balanced Binary Search Trees AVL Trees AVL Trees: Insertion AVL Insertion:

Section 13.3: Balanced Binary Search TreesBalanced Binary Search TreesAVL Trees

AVL Trees: Insertion

AVL Left Subtree Insertion: Rebalancing at node t

def left single rotate(self,

t):

grandparent = t

parent = t.left

grandparent.left =

parent.right

parent.right = grandparent

t = parent

# adjust heights of

grandparent, parent

return t

2 4

3

8

6

5

t

CSI33 Data Structures