Single Rotation k2 k1 k1 k2 h Z X X Y Y Z New item - - PowerPoint PPT Presentation

single rotation
SMART_READER_LITE
LIVE PREVIEW

Single Rotation k2 k1 k1 k2 h Z X X Y Y Z New item - - PowerPoint PPT Presentation

Single Rotation k2 k1 k1 k2 h Z X X Y Y Z New item Suppose an item is added at the bottom of subtree X, thus causing an imbalance at k2. Then pull k1 up. Note that after the rotation, the height of the tree is the same as it was


slide-1
SLIDE 1

Single Rotation

X Y Z

k2 k1 h

X Y Z

k2 k1

Suppose an item is added at the bottom of subtree X, thus causing an imbalance at k2. Then pull k1 up. Note that after the rotation, the height of the tree is the same as it was before the insertion.

New item

slide-2
SLIDE 2

Example

3 1 2 4 6 3 1 2 4 6

Imbalance at node 4 solved with single rotation.

slide-3
SLIDE 3

Another Single Rotation

X Y Z

k2 k1 h

X Z Y

k2 k1

Suppose an item is added at the bottom of subtree X, thus causing an imbalance at k2. Then pull k1 up. Note that after the rotation, the height of the tree is the same as it was before the insertion.

slide-4
SLIDE 4

Another Example

8 5 2 4 6 10 8 5 6 10 2 4

Imbalance at node 4 solved with single rotation.

slide-5
SLIDE 5

Single Rotations

 After single rotations, the new height of the

entire subtree is exactly the same as the height

  • f the original subtree prior to the insertion of

the new data item that caused X to grow.

 Thus no further updating of heights on the path

to the root is needed, and consequently no further rotations are needed.

slide-6
SLIDE 6

Double Rotation

k 3 h

D

Suppose an item is added below k2. This causes an imbalance at

  • k3. Then pull k2 up. Note that after the rotation, the height of the

tree is the same as it was before the insertion.

k 2 k 1

C B A

k 2 k 1 k 3

C D B A

  • r
  • r
slide-7
SLIDE 7

Another Double Rotation

k 3 h

D

Suppose an item is added below k2. This causes an imbalance at

  • k3. Then pull k2 up. Note that after the rotation, the height of the

tree is the same as it was before the insertion.

k 2 k 1

C B A

k 2 k 3 k 1

C D B A

  • r
  • r
slide-8
SLIDE 8

An Example

8 5 2 4 6 10 1 9 9 5 2 4 6 10 1 8

Imbalance at node 8 solved with double rotation.

slide-9
SLIDE 9

Which Rotation Do I Use?

 Recognizing which rotation you have to use is

the hardest part.

 Find the imbalanced node.  Walk down two steps.  If the path is straight, use single rotation.  If the path zig-zags, use double rotation.

slide-10
SLIDE 10

Double Rotation= 2 Single Rotations

k 3 k 2 k 1

C B A D

First do a single rotation of k2 and k1.

slide-11
SLIDE 11

k 3 k 1 k 2

C B A D

But k3 still imbalanced, so do a single rotation of k2 and k3.

Double Rotation= 2 Single Rotations

slide-12
SLIDE 12

Double Rotation= 2 Single Rotations

k 2 k 1 k 3

C B A D

Now we are done.

slide-13
SLIDE 13

Double Rotations

 As with the single rotations, double rotations

restore the height of the subtree to what it was before the insertion.

 This guarantees that all rebalancing and

height updating is complete.

slide-14
SLIDE 14

Programming Tip

 You need to maintain a value for height of subtrees

 Say variables LHeight and RHeight.  Store height = LHeight - RHeight

 Whenever a node is inserted, walk up from that node and update

the parent Lheight (or Rheight) and height variables.

 In doing so, if a node becomes imbalanced, stop and do rotations  If some ancestor’s height does not change, work is done.

slide-15
SLIDE 15

Caution

 Maintain height in each node to find which

node has imbalance => Extra storage

 Deletion is more complicated. May require a

series of rotations that propagate upwards.

 Difficult to program and debug

 Nicer in theory but have to take care of corner

cases in deletion and insertion.

slide-16
SLIDE 16

Conclusions: AVL Tree

 AVL trees maintain balance of binary search

trees while they are being created via insertions of data.

 An alternative approach is to have trees that

readjust themselves when data is accessed, making often accessed data items move to the top of the tree. We won’t be covering these (splay trees).

slide-17
SLIDE 17

Thank You

slide-18
SLIDE 18

EE717: Advanced Computing foR ELECTRICAL Engineers Lecture 7

Priority Queues

Shankar Balachandran, IIT Bombay (shankarb@ee.iitb.ac.in)

21 Aug 2014

slide-19
SLIDE 19

Priority Queues

 T

wo kinds of priority queues:

  • Min priority queue.
  • Max priority queue.
slide-20
SLIDE 20

Min Priority Queue

  • Collection of elements.
  • Each element has a priority or key.
  • Supports following operations:
  • isEmpty
  • size
  • add/put an element into the priority queue
  • get element with min priority
  • remove element with min priority
slide-21
SLIDE 21

Max Priority Queue

  • Collection of elements.
  • Each element has a priority or key.
  • Supports following operations:
  • isEmpty
  • size
  • add/put an element into the priority queue
  • get element with max priority
  • remove element with max priority

 

slide-22
SLIDE 22

Complexity Of Operations

 T

wo good implementations are heaps and leftist trees.

 isEmpty, size, and get => O(1) time  put and remove => O(log n) time where n is

the size of the priority queue

slide-23
SLIDE 23

Applications

 Sorting

  • use element key as priority
  • put elements to be sorted into a priority queue
  • extract elements in priority order
  • if a min priority queue is used, elements are extracted

in ascending order of priority (or key)

  • if a max priority queue is used, elements are

extracted in descending order of priority (or key)

slide-24
SLIDE 24

Sorting Example

 Sort five elements whose keys are 6, 8, 2, 4, 1

using a max priority queue.

  • Put the five elements into a max priority queue.
  • Do five remove max operations placing removed

elements into the sorted array from right to left.

slide-25
SLIDE 25

After Putting Into Max Priority Queue

 Sorted Array

6 8 2 4 1

Max Priority Queue

slide-26
SLIDE 26

After First Remove Max Operation

 Sorted Array

6 2 4 1

8

Max Priority Queue

slide-27
SLIDE 27

After Second Remove Max Operation

 Sorted Array

2 4 1

8 6

Max Priority Queue

slide-28
SLIDE 28

After Third Remove Max Operation

 Sorted Array

2 1

8 6 4

Max Priority Queue

slide-29
SLIDE 29

After Fourth Remove Max Operation

 Sorted Array

1

8 6 4 2

Max Priority Queue

slide-30
SLIDE 30

After Fifth Remove Max Operation

 Sorted Array

8 6 4 2 1

Max Priority Queue

slide-31
SLIDE 31

Complexity Of Sorting

 Sort n elements.

  • n put operations => O(n log n) time.
  • n remove max operations => O(n log n) time.
  • total time is O(n log n).
  • compare with O(n2) for other sort methods
slide-32
SLIDE 32

Heap Sort

 Uses a max priority queue that is

implemented as a heap.

 Initial put operations are replaced by a heap

initialization step that takes O(n) time.

slide-33
SLIDE 33

Min Tree Definition

 Each tree node has a value.  Value in any node is the minimum value in the

subtree for which that node is the root.

 Equivalently, no descendent has a smaller

value.

slide-34
SLIDE 34

Min Tree Example

 Root has minimum element.

2 4 9 3 4 8 7 9 9

slide-35
SLIDE 35

Max Tree Example

 Root has maximum element.

9 4 9 8 4 2 7 3 1

slide-36
SLIDE 36

Min Heap Definition

  • Nearly-complete binary tree

and a

  • Min tree
slide-37
SLIDE 37

Min Heap With 9 Nodes

 Nearly-Complete binary tree with 9 nodes.

slide-38
SLIDE 38

Min Heap With 9 Nodes

 Nearly-Complete binary tree with 9 nodes that

is also a min tree.

2 4 6 7 9 3 8 6 3

slide-39
SLIDE 39

Max Heap With 9 Nodes

 Nearly-Complete binary tree with 9 nodes that

is also a max tree.

9 8 6 7 2 6 5 1 7

slide-40
SLIDE 40

Heap Height

 Since a heap is a complete binary tree, the

height of an n node heap is log2 (n+1).

slide-41
SLIDE 41

A Heap Is Efficiently Represented As An Array

9 8 7 6 7 2 6 5 1

1 2 3 4 5 6 7 8 9 10 9 8 6 7 2 6 5 1 7

slide-42
SLIDE 42

Moving Up And Down A Heap

9 8 6 7 2 6 5 1 7 1 2 3 4 5 6 7 8 9

slide-43
SLIDE 43

Putting An Element Into A Max Heap

 Complete binary tree with 10 nodes.

9 8 6 7 2 6 5 1 7 7

slide-44
SLIDE 44

Putting An Element Into A Max Heap

 New element is 5.

9 8 6 7 2 6 5 1 7 7 5

slide-45
SLIDE 45

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7

slide-46
SLIDE 46

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7

slide-47
SLIDE 47

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7

slide-48
SLIDE 48

Putting An Element Into A Max Heap

 New element is 20.

9 8 6 7 2 6 5 1 7 7 7 20

slide-49
SLIDE 49

Putting An Element Into A Max Heap

 Complete binary tree with 11 nodes.

9 8 6 7 2 6 5 1 7 7 7 20

slide-50
SLIDE 50

Putting An Element Into A Max Heap

 New element is 15.

9 8 6 7 2 6 5 1 7 7 7 20

slide-51
SLIDE 51

Putting An Element Into A Max Heap

 New element is 15.

9 8 6 7 2 6 5 1 7 7 7 20 8

slide-52
SLIDE 52

Putting An Element Into A Max Heap

 New element is 15.

8 6 7 2 6 5 1 7 7 7 20 8 9 15

slide-53
SLIDE 53

Complexity Of Put

 Complexity is O(log n), where n is heap size.

8 6 7 2 6 5 1 7 7 7 20 8 9 15