-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinHeap.H
More file actions
executable file
·127 lines (119 loc) · 2.42 KB
/
BinHeap.H
File metadata and controls
executable file
·127 lines (119 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
@file BinHeap.H
@brief Clase Monticulo Binario
Monticulo Binario o Heap
Cualquier duda o mejora informar al correo: erikvelasquez.25@gmail.com
@author Erik Velasquez
@date 8/2014
*/
#ifndef BINHEAP_H
#define BINHEAP_H
#include <iostream>
#include "BinNodeUtils.H"
/**
@class BinHeap
@brief Monticulo Binario
*/
template<class T> class BinHeap
{
private:
BinNode<T> *root; //cabecera o raiz del nodo
unsigned int type; //Heap de Minimos o Maximos (0=Minimo,1=Maximo)
public:
/**
@brief Constructor por omision de la clase
*/
BinHeap(unsigned int type = 0) //inicializamos el nodo en NULL
{
if(type != 0 and type != 1)
{
throw NotTypeHeap();
}
root=NULL;
this->type = type;
}
/**
@brief Constructor por copia de la clase
*/
BinHeap(Vector<T> &heap,unsigned int type = 0) //inicializamos el nodo en NULL
{
if(type != 0 and type != 1)
{
throw NotTypeHeap();
}
root = buildABHeap(heap,1,heap.getSize());
this->type = type;
}
/**
@brief Obtenemos la raiz del arbol
@return puntero de la raiz BinNode<T>*
*/
BinNode<T>*& getRoot() //retornamos la raiz del arbol
{
return root;
}
/**
@brief inserto un nodo en el monticulo
@param Node es el nodo a insertar
@return retorna NULL en caso de insercion fallida
*/
BinNode<T>* insert(BinNode<T>* Node)
{
if(Node == NULL)
{
throw NullNode();
}
if(type == 0)
{
return insertHeapMin(getRoot(),Node);
}
else
{
return insertHeapMax(getRoot(),Node);
}
}
/**
@brief inserto un valor en el monticulo
@param data es el valor a insertar
@return retorna NULL en caso de insercion fallida
*/
BinNode<T>* insert(T data)
{
BinNode<T> * node;
try
{
node = new BinNode<T>(data);
}
catch(bad_alloc &e)
{
throw NoMemory();
}
return this->insert(node);
}
/**
@brief remueve un nodo en el monticulo
@param key dato a ser eliminado
@return retorna NULL en caso de no remover
*/
BinNode<T>* remove(T key)
{
return removeFromHeap(getRoot(),key);//remove
}
/**
@brief busca un valor en el monticulo
@param key es el dato a buscar
@return retorna NULL en caso de no encontrar el valor
*/
BinNode<T>* search(T key)
{
return searchHeap(getRoot(),key);
}
/**
@brief destructor por omision de la clase
*/
~BinHeap()
{
deleteBinNode(root);
}
};//fin clase BinHeap
#endif