Вы находитесь на странице: 1из 56

HEAP TREE

maxheap

HEAP

Suppose H is a complete binary tree,


1.Then H is called a heap, or a maxheap,
7

if each node N of H has the following property:


The value of N is greater than or equal to the value at
any descendants of N.

2.Minheap:

minheap

The value of N is less than or equal to


the value at any descendants of N.

1
2

Example: Maxheap
97
88

. 95
55

66

35

95

23

48

Inserting into heap

ADD ITEM at the end of H so that H is still a


complete binary tree, but not necessarily a heap.
Let ITEM rise to its 'appropriate place' in H so
that H is finally a heap.

Insert 90
97
88

. 95
55

66

35

95

23

48

Insert 90
97
88

95

. 95
55

66

35

23

90

48

Join ITEM at the end of H

Insert 90
97
88

95

. 95
55

66

35

23

90

48

Rise:
Compare 90 with its parent,
If parent is smaller, interchange.

Insert 90
97
88

95

. 95
90

66

35

23

55

48

Rise:
Compare 90 with its parent,
If parent is smaller, interchange.

Insert 90
97
88

95

. 95
90

66

35

23

55

48

Rise:
Compare 90 with its parent,
If parent is smaller, interchange.

Insert 90
97
90

95

. 95
88

66

35

23

55

48

Rise:
Compare 90 with its parent,
If parent is smaller, interchange.

Insert 90
97
90

95

. 95
88

66

35

23

55

48

Rise:
Compare 90 with its parent,
If parent is smaller, interchange.

Insert 90
97
90

95

. 95
88

66

35

23

55

48

Now, item 90 reached its correct


place.
Should verify that its a heap tree.

Build a heap with the following numbers:


44, 30, 50, 22, 60, 55, 77, 55

Build heap :

44,30,50,22,60,55,77,55
44

Build heap :

30,50,22,60,55,77,55
44
30
.

Build heap :

50,22,60,55,77,55
44
30

50
.

Build heap :

22,60,55,77,55
50
30

44
.

Build heap :

22,60,55,77,55
50
30

44
.

22

Build heap :

60,55,77,55
50
30

44
.

22

60

Build heap :

55,77,55
50
60

44
.

22

30

Build heap :55,77,55


50
60

44
.

22

30

Build heap :

55,77,55
60
50

44
.

22

30

Build heap :

55,77,55
60
50

44
.

22

30 55

Build heap :

77,55
60
50

55
.

22

30 44

Build heap :

77,55
60
50

55
.

22

30 44

77

Build heap :

55
60
50

77
.

22

30 44

55

Build heap :55


60
50

77
.

22

30 44

55

Build heap :

55
77
50

60
.

22

30 44

55

Build heap :

55
77
50

60
.

22
55

30 44

55

Build heap :
77
50

60
.

55
22

30 44

55

Build heap :
77
50

60
.

55
22

30 44

55

Build heap :
77
55

60
.

50
22

30 44

55

Heap: 44,30,50,22,60,55,77,55
77
55

60
.

50
22

30 44

55

Algorithm : insert_heap(item)
1. n=n+1
2. ptr=n-1
3. TREE[ptr]=item

//Add a new node to H


// initialise ptr to the new node added
// find parent of the new node

4. while(ptr>0)
1.
2.

parent =(ptr-1)/2
if(TREE[ptr]>TREE [parent])
1.
Swap( TREE[ptr] and TREE[parrent] )
// exchange
2.
ptr=parent
// current position of the inserted
node

Else
1. Break // no need to exchange,
4.
Endif
5. endwhile
6. stop
3.

Deleting the root of a heap

Assign the ROOT to some variable ITEM


2.
Replace the deleted node R by the last node L, so that its still
a complete tree but not necessarily a heap.
3.
Reheap ( Let L sink to its appropriate place so that its finally
a heap)
1.

Delete ROOT
ITEM = 77

77
55

60
.

50
22

30 44

55

Delete ROOT
ITEM = 77

77
55

60
.

50
22

30 44

55

Delete ROOT
ITEM = 77

22
55

60
.

50

30 44

55

Delete ROOT
ITEM = 77

22
55

60
.

50

30 44

55

Delete ROOT
ITEM = 77

60
55

22
.

50

30 44

55

Delete ROOT
ITEM = 77

60
55

22
.

50

30 44

55

Delete ROOT
ITEM = 77

60
55

55
.

50

30 44

22

ROOT deleted
ITEM = 77

60
55

55
.

50

30 44

22

Delete ROOT
Deleted 77
Now delete ITEM =60

60
55

55
.

50

30 44

22

Delete ROOT
Deleted 77
Now delete ITEM =60

60
55

55
.

50

30 44

22

Delete ROOT
Deleted 77
Now delete ITEM =60

22
55

55
.

50

30 44

Delete ROOT
Deleted 77
Now delete ITEM =60

22
55

55
.

50

30 44

Delete ROOT
Deleted 77
Now delete ITEM =60

55
22

55
.

50

30 44

Delete ROOT
Deleted 77
Now delete ITEM =60

55
22

55
.

50

30 44

Delete ROOT
Deleted 77
Now delete ITEM =60

55
50

55
.

22

30 44

ROOT deleted
Deleted 77
Deleted 60

55
50

55
.

22

30 44

Concept of Heapsort
Initially deleted 77
Then root was 60
Now root is 55

So while deleting root one by one ,


we get elements in sorted order ( descending order)

Algorithm : Delete _heap

Application of Heap Tree - HEAP SORT

Phase A : Build a heap out of the elements


Phase B: repeatedly delete the root element of heap

Algorithm : Heapsort
1. n=0
2. for j=1 to size

//Build a heap

Read ITEM
2.
Insert_heap(ITEM) //insert element into heap
3. end for
1.

4. while n>0

// sort by repeated deletion


1.
ITEM=Delete_heap() //delete root element from
heap
2.
TREE[n] = ITEM
//no element at position N
5. end while
6. stop

Algorithm : Delete _heap

Вам также может понравиться