File tree 4 files changed +99
-0
lines changed
4 files changed +99
-0
lines changed Original file line number Diff line number Diff line change
1
+ "use strict" ;
2
+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3
+ var Heap = /** @class */ ( function ( ) {
4
+ function Heap ( capacity ) {
5
+ this . a = new Array ( capacity + 1 ) ;
6
+ this . n = capacity ;
7
+ this . count = 0 ;
8
+ }
9
+ Heap . prototype . insert = function ( data ) {
10
+ if ( this . count >= this . n )
11
+ return ;
12
+ ++ this . count ;
13
+ this . a [ this . count ] = data ;
14
+ var i = this . count ;
15
+ while ( i / 2 > 0 && this . a [ i ] > this . a [ i / 2 ] ) {
16
+ this . swap ( this . a , i , Math . floor ( i / 2 ) ) ;
17
+ }
18
+ } ;
19
+ Heap . prototype . swap = function ( a , i , max ) {
20
+ var tmp = this . a [ i ] ;
21
+ a [ i ] = a [ max ] ;
22
+ a [ max ] = tmp ;
23
+ } ;
24
+ return Heap ;
25
+ } ( ) ) ;
26
+ exports . Heap = Heap ;
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ var QuickSort_1 = require("./QuickSort");
12
12
var CountingSort_1 = require ( "./CountingSort" ) ;
13
13
var BinarySearch_1 = require ( "./BinarySearch" ) ;
14
14
var Nodes_1 = require ( "./Nodes" ) ;
15
+ var Heap_1 = require ( "./Heap" ) ;
15
16
exports . default = {
16
17
LinkedList : LinkedList_1 . LinkedList ,
17
18
ArrayStack : ArrayStack_1 . ArrayStack ,
@@ -25,4 +26,5 @@ exports.default = {
25
26
CountingSort : CountingSort_1 . CountingSort ,
26
27
BinarySearch : BinarySearch_1 . BinarySearch ,
27
28
Nodes : Nodes_1 . Nodes ,
29
+ Heap : Heap_1 . Heap ,
28
30
} ;
Original file line number Diff line number Diff line change
1
+ export class Heap {
2
+ private a : Array < number >
3
+ private n : number
4
+ private count : number
5
+
6
+ constructor ( capacity : number ) {
7
+ this . a = new Array ( capacity + 1 )
8
+ this . n = capacity
9
+ this . count = 0
10
+ }
11
+
12
+ public insert ( data : number ) : void {
13
+ if ( this . count >= this . n ) return
14
+ ++ this . count
15
+ this . a [ this . count ] = data
16
+ let i = this . count
17
+ while ( i / 2 > 0 && this . a [ i ] > this . a [ i / 2 ] ) {
18
+ this . swap ( this . a , i , Math . floor ( i / 2 ) )
19
+ }
20
+
21
+ }
22
+
23
+ public swap ( a : Array < number > , i : number , max : number ) : void {
24
+ let tmp = this . a [ i ]
25
+ a [ i ] = a [ max ]
26
+ a [ max ] = tmp
27
+ }
28
+
29
+ public removeMax ( ) : any {
30
+ if ( this . count == 0 ) {
31
+ return - 1
32
+ }
33
+ this . a [ 1 ] = this . a [ this . count ]
34
+ -- this . count
35
+ this . heapify ( this . a , this . count , 1 )
36
+ }
37
+
38
+ public heapify ( a : Array < number > , n : number , i : number ) {
39
+ while ( true ) {
40
+ let maxPos = i
41
+ if ( i * 2 < this . n && a [ i ] < a [ i * 2 ] ) {
42
+ maxPos = i * 2
43
+ }
44
+ if ( i * 2 + 1 <= n && a [ maxPos ] < a [ i * 2 + 1 ] ) {
45
+ maxPos = i * 2 + 1
46
+ }
47
+ if ( maxPos == i ) {
48
+ break
49
+ }
50
+ this . swap ( a , i , maxPos )
51
+ }
52
+ }
53
+
54
+ public buildHelp ( a : Array < number > , n : number ) : void {
55
+ for ( let i = Math . floor ( n / 2 ) ; i >= 1 ; -- i ) {
56
+ this . heapify ( a , n , i )
57
+ }
58
+ }
59
+
60
+ public sort ( a : Array < number > , n : number ) {
61
+ this . buildHelp ( a , n )
62
+ let k = n
63
+ while ( k > 1 ) {
64
+ this . swap ( a , 1 , k )
65
+ -- k ;
66
+ this . heapify ( a , k , 1 )
67
+ }
68
+ }
69
+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import {QuickSort} from './QuickSort'
10
10
import { CountingSort } from './CountingSort'
11
11
import { BinarySearch } from './BinarySearch'
12
12
import { Nodes } from './Nodes'
13
+ import { Heap } from './Heap'
13
14
14
15
export default {
15
16
LinkedList,
@@ -24,5 +25,6 @@ export default {
24
25
CountingSort,
25
26
BinarySearch,
26
27
Nodes,
28
+ Heap,
27
29
}
28
30
You can’t perform that action at this time.
0 commit comments