#ifndef GRAPHTYPE_H
#define GRAPHTYPE_H
#include<vector>
#include<iostream>
using namespace std;
#define
#define
#define
#define
UNEXPLORER 0
VISITED 1
DISCOVERED 2
BACK 3
int refSecEdge;
vector<edge<E>*> *refSecIncidentDest;
vector<edge<E>*> *refSecIncidentOrig;
int label;
public:
edge(vertex<E>*,vertex<E>*,int);
friend class graph<E>;
};
template <class E> edge<E>::edge(vertex<E>* vertexOrig, vertex<E>*
vertexDest,int pos)
{
this->refVertexOrig=vertexOrig;
this->refVertexDest=vertexDest;
this->refSecEdge=pos;
this->refSecIncidentOrig=vertexOrig->secIncident;
this->refSecIncidentDest=vertexDest->secIncident;
this->label=UNEXPLORER;
}
template <class E> class graph
{
private:
vector<vertex<E>*> *secVertex;
vector<edge<E>*> *secEdge;
public:
graph();
//------------------Update Methods-----------------------void insertVertex(E);
void insertEdge(E,E);
vertex<E>* findVertex(E);
edge<E>* findEdge(vertex<E>* , vertex<E>* );
void DFS();
void BFS();
void pathDFS(E, E);
private:
void setLabelVertex();
void setLabelEdge();
void DFS(vertex<E>*);
void BFS(vertex<E>*);
void pathDFS(vertex<E>* , vertex<E>*);
vertex<E>* opposite(vertex<E>* , edge<E>*);
};
template <class E> graph<E>::graph()
{
this->secVertex=new vector<vertex<E>*>;
this->secEdge=new vector<edge<E>*>;}
if(w->label == UNEXPLORER)
{beginVertex->secIncident->at(i)->label=DISCOVERED;
//list->push_back(beginVertex->secIncident>at(i)); Para un vector que acepte nodos y aristas.
pathDFS(w,current);
//list->pop_back();
}
else
beginVertex->secIncident->at(i)->label=BACK;}}
list->pop_back();}
template <class E> void graph<E>::BFS()
{setLabelVertex();
setLabelEdge();
cout<<"Inicio: ";
for(int i=0; i<secVertex->size(); i++)
if(secVertex->at(i)->label == UNEXPLORER)
BFS(secVertex->at(i));}
template <class E> void graph<E>::BFS(vertex<E>* beginVertex)
{
vector<vertex<E>*>* list = new vector<vertex<E>*>;
vertex<E>* w;
beginVertex->label=VISITED;
list->push_back(beginVertex);
int i=0;
while (list->at(i) == NULL)
{for (int j = 0; j < list->size(); j++)
{for(int k = 0; k < beginVertex->secIncident->size(); k++)
{if (beginVertex->secIncident->at(i)->label ==
UNEXPLORER)
{w=opposite(beginVertex,beginVertex>secIncident->at(i));
if(w->label == UNEXPLORER)
{beginVertex->secIncident->at(i)>label=DISCOVERED;
w->label=VISITED;
list->push_back(w);
}
else
beginVertex->secIncident->at(i)>label=BACK;}}
i++;
}
for (int j = 0; j < list->size(); j++)
cout<<*(list->at(i));}
#endif*****grafo adyacente*****
#ifndef GRAPHMA_H
#define GRAPHMA_H
#include <iostream>
using namespace std;
#define N 10
//cantidad maxima de vertices
template<class E>class GraphMA{
private:
int **matrixAd;
//matriz de adyacencia
E *lstVertex;
//lista de vertices. La posicion ocupada determina
la fila y columna que
//representa a un vertice en la matriz
int nVertex;
//cantidad de v?rtices en un momento
determinado
public:
GraphMA();
void insertVertex(E);
int findVertex(E);
void insertEdge(E,E);
};
template<class E>GraphMA<E>::GraphMA(){
int i,j;
matrixAd = new int *[N];
for(i = 0; i<N; i++)
matrixAd[i] = new int[N];
for(i = 0; i<N; i++)
for(j = 0; j<N; j++)
matrixAd[i][j] = 0;
lstVertex = new E[N];
nVertex = 0;
}
template<class E>void GraphMA<E>::insertVertex(E item){
if (nVertex >= N){
cout<<"Espacio insuficiente para seguir insertando....\n";
return;
}
int pos = findVertex(item);
if (pos!=-1){
cout<<"Vertice duplicado...\n";
return;
}
lstVertex[nVertex++] = item;
}
template<class E>int GraphMA<E>::findVertex(E item){
//se deja para su implementacion
}
template<class E>void GraphMA<E>::insertEdge(E vOrig, E vDest){
int posOrig = findVertex(vOrig);
int posDest = findVertex(vDest);
if (posOrig == -1 or posDest == -1){
cout<<"Alguno de los vertices extremos no existe....\n";
return;
}
int posEdge = findEdge(posOrig, posDest);
if(posEdge != 0){
cout<<"Arista duplicada....\n";
return;
}
matrixAd[posOrig][posDest] = matrixAd[posDest][posOrig] = 1;
}
**** btreee
#ifndef BTREE_H
#define BTREE_H
#include "nodeb.h"
template<class E> class Btree{
private:
NodeB<E>* root;
int orden;
void push(NodeB<E>*, E, bool*, E*, NodeB<E>**);
void putNode(NodeB<E>*, E, NodeB<E>*, int);
void removeRegister(NodeB<E>*, E, bool*);
void removeKey(NodeB<E>*, int);
void restore(NodeB<E>*, int);
bool findNode(NodeB<E>*, E, int *);
void dividedNode(NodeB<E>*, E, NodeB<E>*, int, E*, NodeB<E>**);
friend ostream& operator << <>(ostream&, Btree<E>&);
void showTree(NodeB<E>*);
void sucesorFind(NodeB<E>*, int);
public:
Btree(int);
void insert(E);
void remove(E);
};
template<class E> Btree<E>::Btree(int orden){
this->orden = orden;
root = 0;
}
template<class E> void Btree<E>::insert(E ele){
bool up;
E clMediana;
NodeB<E> *nd;
NodeB<E> *pnew;
push(root, ele, &up, &clMediana, &nd);
if (up){
pnew = new NodeB<E>(orden);
pnew->getKey()[0] = clMediana;
pnew->getChilds()[0] = root;
pnew->getChilds()[1] = nd;
pnew->setCount(1);
root = pnew;
}
//cout << "Nodo: " << *root ;
}
template<class E> void Btree<E>::remove(E key){
bool found;
removeRegister(root, key, &found);
if (found){
if (root->getCount() == 0){
NodeB<E>*p = root;
root = root->getChilds()[0];
delete p;
}
}
else
cout << "El nodo no existe..." << endl;
}
template<class E> void Btree<E>::removeRegister(NodeB<E> *current, E
key, bool *found){
int pos;
if (current) {
(*found) = findNode(current, key, &pos);
if (*found) {
if (!current->getChilds()[0]) //hoja
removeKey(current, pos);
else {
sucesorFind(current, pos);
removeRegister(current->getChilds()[pos + 1],
current->getKey()[pos], found);
}
}
else
removeRegister(current->getChilds()[pos], key, found);
if (current->getChilds()[pos])
{
if (current->getChilds()[pos]->nodeEmpty(orden - 1))
restore(current, pos);
}
}
else
*found = false;
}
template<class E> void Btree<E>::removeKey(NodeB<E> *a, int elem){
int i;
for (i = elem + 1; i<a->getCount(); i++)
{
a->getKey()[i - 1] = a->getKey()[i];
a->getChilds()[i - 1] = a->getChilds()[i];
}
a->getChilds()[i - 1] = a->getChilds()[i];
a->count--;
}
b.showTree(b.root);
}
return os;
}
template <class E> void Btree<E>::showTree(NodeB<E> *act){
if (act){
cout << *act << endl;
for (int i = 0; i <= act->getCount(); i++){
showTree(act->getChilds()[i]);
}
}
}
template <class E> void Btree<E>::dividedNode(NodeB<E>* act, E cl,
NodeB<E>* hd, int pos, E* clMediana, NodeB<E>**nd){
int i, posMdna;
posMdna = (pos <= orden/2) ? orden / 2 : orden / 2 + 1;
(*nd) = new NodeB<E>(orden);
for (i = posMdna; i < orden - 1; i++) {
(*nd)->getKey()[i - posMdna] = act->getKey()[i];
(*nd)->getChilds()[i - posMdna + 1] = act->getChilds()[i + 1];
}
(*nd)->setCount(orden - 1 - posMdna);
act->setCount(posMdna);
if (pos <= orden / 2)
putNode(act, cl, hd, pos);
else
putNode(*nd, cl, hd, pos - posMdna);
(*clMediana) = act->getKey()[act->getCount() + 1];
(*nd)->getChilds()[0] = act->getChilds()[act->getCount()];
act->setCount(act->getCount() + 1);
}
template <class E> void Btree<E>::sucesorFind(NodeB<E> *a, int elem){
NodeB<E> *nodeKey = a;
a = a->getChilds()[elem + 1];
while (a->getChilds()[0])
a = a->getChilds()[0];
nodeKey->getKey()[elem] = a->getKey()[0];
}
#endif
template <class E>void insertar(NodeB<E>& nd, E ele, int max){
if (nd.getCount() >= max)
cout << "overflow..." << endl;
else
{
int i;
for (i = nd.getCount() - 1; i >= 0 && nd[i] > ele; i--)
nd[i + 1] = nd[i];
nd[i + 1] = ele;
nd.setCount(nd.getCount() + 1);
}
}
****bsqueda sucesor***
template<class E>
void Btree<E>::buscarSucesor() {
buscarSucesor(root, 0);
}
template<class E>
NodeB<E>* Btree<E>::buscarSucesor(NodeB<E>* act, int elem) {
cout << act->getCount() << endl;
cout << *act->getChilds()[0] << endl;
int cap = act->getCount();
if (act = NULL)
return NULL;
else {
for (int i = 0; i < cap; i++) {
cout << *act->getChilds()[i] << "**";}}}
*****Busqueda en profundidad****
template<class E>void CGrafo<E>::DFS(E vert)
{
cout<<"Recorrido DFS:"<<endl;
CVertice<E>*ptrVert=findVertice(vert);
if(ptrVert)
{
for(int i=0;i<SecVertice->size();i++)
SecVertice->at(i)->setLabel(UNEXPLORED);
DFS(ptrVert);
cout<<endl;
}else
cout<<"No existe este vrtice"<<endl;
}
//Mtodo soporte - recursivo para realizar la bsqueda
template<class E>void CGrafo<E>::DFS(CVertice<E>* ptrVert)
{cout<<ptrVert->getData()<<" ";
ptrVert->setLabel(VISITED);
if(!ptrVert->getSecIncident()->empty())
{for(int i=0;i<ptrVert->getSecIncident()->size();i++)
if(ptrVert->getSecIncident()->at(i)->getRefDestino()>getLabel()==UNEXPLORED)
DFS(ptrVert->getSecIncident()->at(i)->getRefDestino());
***lista enlazada
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
using namespace std;
class NodoLEG {
private:
int dato;
NodoLEG *siguiente;
public:
NodoLEG(int dato, NodoLEG *sig=NULL) {
this->dato = dato;
this->siguiente = sig;
}
};
class LEG {
private:
NodoLEG *list;
public:
LEG() {
list = NULL;
}
void insertar(int);
bool buscar(int);
};
void LEG::insertar(E x) {
list = new NodoLEG(x, list);
}
bool LEG::buscar(int x) {
NodoLEG *aux = list;
while (aux != NULL && aux->dato != x) {
aux = aux->siguiente;
}
if (aux == NULL) return false;
return true;
}
#endif