Anda di halaman 1dari 23

test.

cpp
#include"btreeType.h"
#include"btreeplus.h"
#include"datos.h"
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<iostream>
using namespace std;
using std::ifstream;
using std::ofstream;
BTree<nb> A(3);
BTree<cb> B(3);
BTree<sb> C(3);
int c=0,ct=0;
void rellenar(string q){
string name;
char nfile[10];
int cod=0,ss=0, temp=0,cont=0;
std::fstream fs;
fs.open (q+".txt");
while (fs.good()){
char w = fs.get();
if (fs.good()){
std::cout<<w;
if(w !=' '){
nfile[temp]=w;
temp++;
}
else{
if(cont==0){
string str(nfile);
cod = atoi (nfile);
temp=0;
cont++;
}
if(cont==1){
string name(nfile);
temp=0;
cont++;
}
if(cont==2){
string str(nfile);
ss = atoi (nfile);
temp=0;
cont=0;
}
}
}
}
A.insert(*(new nb(cod,name,ss,1)));
B.insert(*(new cb(cod,name,ss,1)));
C.insert(*(new sb(cod,name,ss,1)));
}
void main(){
string nombre;
string nm;
int nseguro;
char nfile[10];
int codigo,temp=0;
char var;
if(c==0){
cout<<"Desea cargar archivos existentes(S/N): "<<endl;
cin>>var;
if(var=='s'||var=='S'){
void open (const string& test, ios_base::openmode mode =
ios_base::in);
std::fstream fs;
fs.open ("test.txt");
if (fs.is_open()){
while (fs.good()){
char w = fs.get();
if (fs.good()){
std::cout << w;
if(w !=' '){
nfile[temp]=w;
temp++;
}
else{
string nm (nfile);
temp=0;
rellenar(nm);
}
}
}
std::cout << "Operation successfully performed\n
";
fs.close();
}
else{
std::cout << "Error opening file";
}
}
c++;
}
cout<<"\t\tMENU ARBOL B"<<endl;
cout<<"\t\t===="<<endl;
cout<<"1.Registrar Usuario:"<<endl;
cout<<"2.Mostrar Arbol Deacuerdo al Nombre:"<<endl;
cout<<"3.Mostrar Arbol Deacuerdo al Codigo"<<endl;
cout<<"4.Mostrar Arbol Deacuerdo al Numero de Seguro Social:"<<endl;
cout<<"5.Salir:"<<endl;
cout<<"Ingrese una opcion..."<<endl;
int resp;
char res='n';
cin>>resp;
switch(resp){
case 1:
{
system("cls");
do{
cout<<"nombre:";
cin>>nombre;
std::fstream fs;
fs.open ("test.txt", std::fstream::in |
std::fstream::out | std::fstream::app);
fs <<nombre<<endl;
fs.close();
cout<<"codigo:";
cin>>codigo;
cout<<"numero de seguro social:";
cin>>nseguro;
A.insert(*(new nb(codigo,nombre,nseguro)
));
B.insert(*(new cb(codigo,nombre,nseguro)
));
C.insert(*(new sb(codigo,nombre,nseguro)
));
cout<<"Desea seguir ingresando?(S/N)"<<endl;
cin>>res;
}while(res=='S'|| res == 's');
system("cls");
main();
break;
}
case 2:
{
system("cls");
cout<<endl;
cout<<endl;
cout<<endl;
cout<<A;
cout<<endl;
cout<<endl;
cout<<endl;
cin.get();
main();
break;
}
case 3:
{
system("cls");
cout<<endl;
cout<<endl;
cout<<endl;
cout<<B;
cout<<endl;
cout<<endl;
cout<<endl;
cin.get();
main();
break;
}
case 4:{
system("cls");
cout<<endl;
cout<<endl;
cout<<endl;
cout<<C;
cout<<endl;
cout<<endl;
cout<<endl;
cin.get();
main();
break;
}
case 5:
break;
default:{
cout << "Ingreso Incorrecto!!... "<<endl
;
break;
}
}
system("pause");
//nb &name = new &nb;
cout<<"bienvenido a arbol B+"<<endl;
BTREEPLUS<char> a;
a.Set('A');
a.Set('B');
a.Set('C');
a.Set('D');
a.Set('E');
a.Set('F');
a.Set('G');
a.Set('H');
a.Set('I');
a.listar_VSAM();
a.listar_b();
}
btreeplus.h
#ifndef BTREEPLUS_H
#define BTREEPLUS_H
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include<iostream>
using namespace std;
#define N 2
#define M 4
#define M1 5
#define MAXIMO 10
#define localizar (pagina *) malloc (sizeof (pagina))
template <class E>class BTREEPLUS;
template<class E> class pagina
{
public:
int cont;
E info[M];
pagina<E> *apunt[M1];
};
template<class E> class LIFO
{
public:
int t;
pagina<E> *a[MAXIMO];
};
template<class E> class componente
{
public:
pagina<E> *s;
E v;
};
template<class E> class LIFO1
{
public:
int t;
componente<E> a[MAXIMO];
};
template<class E> class BTREEPLUS
{
public:
pagina<E> *raiz,*VSAM;
BTREEPLUS();
void Set(E);
void init_pila(LIFO<E> *);
int pila_vacia(LIFO<E> *);
void ins_pila(LIFO<E> *,pagina<E> *);
void retirar_pila(LIFO<E> *,pagina<E>**);
void init1_pila(LIFO1<E> *);
int pila1_vacia(LIFO1<E> *);
void ins1_pila(LIFO1<E> *,pagina<E> *,E);
void retirar1_pila(LIFO1<E> *,pagina<E> **,int *);
void inicializar (pagina<E> *);
void crear_pagina (pagina<E> **,E );
void buscar (pagina<E> *,E ,int *,LIFO<E> *);
void insertar (pagina<E> *,E ,int *);
int donde (pagina<E> *, E );
void romper (pagina<E> *,pagina<E> *,pagina<E> **,E ,E *,int );
void cderecha_apunt (pagina<E> *,int );
void listar_b ();
void listar1_b (pagina<E> *,int );
void listar_VSAM ();
void ins_b (pagina<E> **,pagina<E> **,E ,int *);
int lea();
//ELIMINAR
void retirar (pagina<E> *,int );
void cambio (pagina<E> *,pagina<E> *,pagina<E> *,int ,E );
int hoja (pagina<E> *);
void cizquierda_apunt (pagina<E> *,int ,int );
void esta (pagina<E> *, E , int *, LIFO1<E> *);
void unir (pagina<E> **, pagina<E> *, pagina<E> *, pagina<E> *,
int , LIFO1<E> , E , int );
int retira_b (pagina<E> **, pagina<E> **, E , int *);
friend class pagina<E>;
friend class LIFO<E>;
friend class LIFO1<E>;
};
/////////////////////////LIFO////////////////////////////
template<class E> void BTREEPLUS<E>::init_pila(LIFO<E> *P)
{
P->t=0;
}
template<class E> int BTREEPLUS<E>::pila_vacia(LIFO<E> *P)
{
return(!P->t);
}
template<class E> void BTREEPLUS<E>::ins_pila(LIFO<E> *P,pagina<E> *S)
{
if(P->t==MAXIMO)
{
printf ("la pila no soporta mas elementos\n");
exit (1);
}
else
{
P->t++;
P->a[P->t-1]=S;
}
}
template<class E> void BTREEPLUS<E>::retirar_pila(LIFO<E> *p,pagina<E> **s)
{
if(this->pila_vacia(p))
{
printf ("la pila esta vacia\n");
exit (1);
}
else
{
*s=p->a[p->t-1];
p->t--;
}
}
/////////////////////LIFO1////////////////////////////////
template<class E> void BTREEPLUS<E>::init1_pila(LIFO1<E> *p)
{
p->t=0;
}
template<class E> int BTREEPLUS<E>::pila1_vacia(LIFO1<E> *p)
{
return (!p->t);
}
template<class E> void BTREEPLUS<E>::ins1_pila(LIFO1<E> *p,pagina<E> *s,E i)
{
if(p->t==MAXIMO)
{
printf ("la pila no soporta mas elementos\n");
exit (1);
}
else
{
p->t++;
p->a[p->t-1].s=s;
p->a[p->t - 1].v = i;
}
}
template<class E> void BTREEPLUS<E>::retirar1_pila(LIFO1<E> *p,pagina<E> **s,int
*i)
{
if(this->pila1_vacia(p))
{
printf ("la pila esta vacia\n");
exit (1);
}
else
{
*s = p->a [p->t -1].s;
*i = p->a [p->t - 1].v;
p->t--;
}
}
////////////////////PAGINA///////////////////////////
template<class E> void BTREEPLUS<E>::inicializar(pagina<E> *p)
{
int i=0;
p->cont=0;
while(i<M1)
p->apunt[i++]=NULL;
}
////////////////////////revisar para corregir inconveniente///////////////////
template<class E> void BTREEPLUS<E>::crear_pagina(pagina<E> **p,E x)
{
//pnew=new BNode<E>(orden);
// pnew->count=1;
// pnew->keys[0]=median;
//*p = localizar;
*p= (pagina<E> *) malloc (sizeof (pagina<E>));
this->inicializar(*p);
(*p)->cont=1;
cout<<x<<endl;
cout<<"tratando de asignar..."<<endl;
(*p)->info[0]=x;
cout<<"se inserto correctamente..."<<endl;
cout<<(*p)->info[0]<<endl;
cout<<"ingreso a insertar..."<<endl;
}
template<class E> void BTREEPLUS<E>::buscar(pagina<E> *p,E x,int *posicion,LIFO<
E> *pila)
{
int i,encontro=0;
*posicion=-1;
while(p && !encontro)
{
this->ins_pila (pila, p);
i = 0;
while (x > p->info [i] && i < p->cont - 1)
i++;
if (x < p->info [i])
p = p->apunt [i];
else if (x > p->info [i])
if (p->apunt [0] != NULL)
p = p->apunt [i+1];
else
p = NULL;
else if (p->apunt [0] != NULL)
p = p->apunt [i+1];
else
encontro = 1;
}
if (!encontro)
*posicion = i;
}
template<class E> void BTREEPLUS<E>::insertar(pagina<E> *p,E x,int *i)
{
int j;
if(p->cont)
if(x > p->info[*i])
(*i)++;
else
{
j = p->cont - 1;
while (j >= *i)
{
p->info [j+1] = p->info [j];
j--;
}
}
p->cont++;
p->info [*i] = x;
}
template<class E> int BTREEPLUS<E>::donde(pagina<E> *p,E x)
{
int i;
i= 0;
while (x > p->info [i] && i < p->cont - 1)
i++;
return (i);
}
template<class E> void BTREEPLUS<E>::romper(pagina<E> *p,pagina<E> *t,pagina<E>
**q,E x,E *subir,int separar)
{
int i=0 ,s = 0;
E a[M1];
pagina<E> *b[M1+1], *temp;
if (separar == 0)
{
temp = p->apunt [M];
p->apunt[M] = NULL;
}
while (i < M && !s)
if (p->info[i] < x)
{
a[i] =p->info [i];
b[i] = p->apunt [i];
p->apunt [i++] = NULL;
}
else
s = 1;
a[i] = x;
b[i] = p->apunt [i];
p->apunt [i] = NULL;
b[++i] = t;
while (i <= M)
{
a[i] =p->info [i-1];
b[i+1] = p->apunt [i];
p->apunt [i++] = NULL;
}
//*q = localizar;
*q=(pagina<E> *) malloc (sizeof (pagina<E>));
this->inicializar (*q);
i = 0;
if (separar == 0)
{
p->cont = N;
(*q)->cont = N + 1;
(*q)->info [0] = a[N];
while (i < N)
{
p->info [i] = a[i];
p->apunt [i] = b [i];
(*q)->info [i+1] = a[i+N+1];
(*q)->apunt [i] = b[i+N+1];
i++;
}
(*q)->apunt [M] = temp;
p->apunt [M] = *q;
}
else
{
p->cont = (*q)->cont = N;
while (i < N)
{
p->info [i] = a[i];
p->apunt [i] = b [i];
(*q)->info [i] = a[i+N+1];
(*q)->apunt [i] = b[i+N+1];
i++;
}
}
p->apunt [N] = b [N];
(*q)->apunt [N] = b [M1];
*subir = a [N];
}
template<class E> void BTREEPLUS<E>::cderecha_apunt(pagina<E> *p,int i)
{
int j;
j = p->cont;
while (j > i)
{
p->apunt [j] = p->apunt [j - 1];
j--;
}
}
template<class E> void BTREEPLUS<E>::listar_b()
{
int i;
LIFO1<E> pila;
this->init1_pila (&pila);
while (raiz)
{
this->ins1_pila (&pila,raiz,0);
raiz = raiz->apunt [0];
}
while (!pila1_vacia (&pila) )
{
this->retirar1_pila (&pila,&raiz,&i);
if (i < raiz->cont)
{
cout<<raiz->info [i];
//printf ("%d ",p->info [i]);
this->ins1_pila (&pila,raiz,i+1);
if (raiz->apunt [0] != NULL)
{
raiz = raiz->apunt [i+1];
}
else
{
raiz = NULL;
}
while (raiz)
{
this->ins1_pila (&pila,raiz,0);
raiz = raiz->apunt [0];
}
}
}
}
template<class E> void BTREEPLUS<E>::listar1_b(pagina<E> *p,int l)
{
int i;
if (p)
{
for (i = 0; i < l; i++)
printf (" ");
for (i = 0; i < p->cont; i++)
printf ("%4d",p->info [i]);
printf ("\n");
this->listar1_b (p->apunt [0],l+1);
for (i=1; i <= p->cont; i++)
this->listar1_b (p->apunt [i], l+1);
}
}
template<class E> void BTREEPLUS<E>::listar_VSAM()
{
int i;
while (VSAM != NULL )
{
i = 0;
while (i < VSAM->cont)
cout<<VSAM->info[i++];
//printf ("%d ",VSAM->info[i++]);
printf ("/");
VSAM = VSAM->apunt[M];
}
printf ("\n");
}
//////////CONSTRUCTOR///////////////////
template<class E>BTREEPLUS<E>::BTREEPLUS()
{
this->raiz=NULL;
this->VSAM=NULL;
}
template<class E> void BTREEPLUS<E>::Set(E x)
{
int s;
//(&raiz, &VSAM, clave, &s);
this->ins_b(&raiz,&VSAM, x, &s);
if (s == 1)
cout<<"La llave ya existe en el arbol B+\n"<<endl;
else
cout<<"clave insertada correctamente..."<<endl;
}
template<class E> void BTREEPLUS<E>::ins_b(pagina<E> **raiz,pagina<E> **VSAM,E x
,int *s)
{
int posicion,i,separar,terminar;//subir,subir1,separar,terminar;
E subir,subir1;
pagina<E> *p= new pagina<E>,*nuevo= new pagina<E>,*nuevo1= new pagina<E>
;
LIFO<E> pila;
this->init_pila (&pila);
*s = 0;
if (*raiz == NULL)
{

this->crear_pagina (raiz, x);
cout<<"TERMINO DE CREAR PAGINA..."<<endl;
*VSAM = *raiz;
}
else
{
this->buscar (*raiz, x, &posicion, &pila);
if (posicion == -1)
*s = 1; /* La llave esta en el arbol */
else
{
terminar = separar = 0;
while (!this->pila_vacia (&pila) && terminar == 0)
{
this->retirar_pila (&pila, &p);
if (p->cont == M)
{
if (separar == 0)
{
this->romper (p, NULL, &nuevo, x, &subir
,separar);
separar = 1;
}
else
{
this->romper (p,nuevo,&nuevo1,subir,&sub
ir1,separar);
subir = subir1;
nuevo = nuevo1;
}
}
else
{
if (separar == 1)
{
separar = 0;
i = donde (p, subir);
this->insertar (p, subir, &i);
this->cderecha_apunt (p, i+1);
p->apunt [i+1] = nuevo;
}
else
insertar (p, x,&posicion);
terminar = 1;
}
}
if (separar == 1 && terminar == 0)
{
this->crear_pagina (raiz,subir);
(*raiz)->apunt [0] = p;
(*raiz)->apunt [1] = nuevo;
}
}
}
}
template<class E> int BTREEPLUS<E>::lea()
{
char a[10];
gets (a);
return (atoi(a));
}
template<class E> void BTREEPLUS<E>::retirar(pagina<E> *p,int i)
{
while (i < p->cont - 1)
{
p->info [i] = p->info [i+1];
i++;
}
p->cont--;
}
template<class E> void BTREEPLUS<E>::cambio (pagina<E> *p,pagina<E> *q,pagina<E>
*r,int i,E x)
{
int k,t;
if (x > r->info [r->cont - 1])
{
t = q->info [i];
this->retirar (q,i);
k = 0;
this->insertar (p,t,&k);
t = r->info [r->cont - 1];
this->retirar (r, r->cont - 1);
k = i;
if (k == -1)
k = 0;
this->insertar (q,t,&k);
}
else
{
t = q->info [i];
this->retirar (q, i);
k = p->cont - 1;
if (k == -1)
k = 0;
this->insertar (p,t,&k);
t = r->info [0];
this->retirar (r, 0);
k = i;
if (q->cont != 0)
if (k > q->cont - 1)
k = q->cont -1;
this->insertar (q,t,&k);
}
}
template<class E> int BTREEPLUS<E>::hoja(pagina<E> *p)
{
int j = 0;
while (p->apunt [j] == NULL && j < p->cont - 1)
j++;
return (p->apunt [j] == NULL);
}
template<class E> void BTREEPLUS<E>::cizquierda_apunt(pagina<E> *p,int i,int j)
{
while (i < j)
{
p->apunt [i] = p->apunt [i+1];
i++;
}
p->apunt [i] = NULL;
}
template<class E> void BTREEPLUS<E>::esta(pagina<E> *p, E x, int *posicion, LIFO
1<E> *pila)
{
int i = 0,encontro = 0;
*posicion = -1;
while (p != NULL && !encontro)
{
i = 0;
while (x > p->info [i] && i < p->cont - 1)
i++;
if (x < p->info [i])
{
this->ins1_pila (pila, p, i);
p = p->apunt [i];
}
else if (x > p->info [i])
{
this->ins1_pila (pila, p, i+1);
if (p->apunt [0] != NULL)
p = p->apunt [i+1];
else
p = NULL;
}
else
{
if (p->apunt [0] != NULL)
{
this->ins1_pila (pila, p, i+1);
p = p->apunt [i+1];
}
else
{
this->ins1_pila (pila, p, i);
encontro = 1;
}
}
}
if (encontro == 1)
*posicion = i;
}
template<class E> void BTREEPLUS<E>::unir (pagina<E> **raiz, pagina<E> *q, pagi
na<E> *r, pagina<E> *p, int i, LIFO1<E> pila, E x, int posicion)
{
int terminar = 0,j, k;
pagina<E> *t;
this->retirar (p, posicion);
if (x < r->info [0])
{
t = p;
p = r;
r = t;
}
while (terminar == 0)
{
if (r->cont < N && p->cont > N)
{
this->cambio (r, q, p, i, x);
r->apunt [r->cont] = p->apunt [0];
this->cizquierda_apunt (p, 0, p->cont + 1);
terminar = 1;
}
else if (p->cont < N && r->cont > N)
{
this->cambio (p, q, r, i, x);
this->cderecha_apunt (p, 0);
p->apunt [0] = r->apunt [r->cont + 1];
r->apunt [r->cont + 1] = NULL;
terminar = 1;
}
else
{
j = r->cont;
if (r->apunt [0] == NULL) // Si es ultimo nivel
r->apunt [M] = p->apunt [M];
else
r->info [j++] = q->info [i];
k = 0;
while (k <= p->cont - 1)
r->info [j++] = p->info [k++];
r->cont = j;
this->retirar (q, i);
k = 0;
j = M - p->cont;
while (p->apunt [k] != NULL)
r->apunt [j++] = p->apunt [k++];
free (p);
if (q->cont == 0)
{
q->apunt [i+1] = NULL;
if (this->pila1_vacia (&pila) )
{
free (q);
q = NULL;
}
}
else
this->cizquierda_apunt (q, i+1, q->cont+1);
if (q != NULL)
if (q->cont >= N)
terminar = 1;
else
{
t = q;
if (!this->pila1_vacia (&pila) )
{
this->retira1_pila (&pila, &q, &i);
if (x >= q->info [0])
{
p = t;
r = q->apunt [i-1];
i--;
}
else
{
r = t;
p = q->apunt [i+1];
}
}
else
terminar = 1;
}
else
{
terminar = 1;
*raiz = r;
}
}
}
}
template<class E> int BTREEPLUS<E>::retira_b(pagina<E> **raiz, pagina<E> **VSAM,
E x, int *s)
{
int posicion, i, k, temp;
pagina<E> *p, *q, *r;
LIFO1<E> pila;
*s = 1;
this->init1_pila (&pila);
this->esta (*raiz, x, &posicion, &pila);
if (posicion == -1)
{
*s = 0; // La llave no existe en el arbol
return (0);
}
this->retira1_pila (&pila, &p, &i);
if (p->cont > N)
{
this->retirar (p, posicion);
return (1);
}
if (this->pila1_vacia (&pila))
{
this->retirar (p, posicion);
if (p->cont == 0)
{
free (*raiz);
*raiz = *VSAM = NULL;
}
return (1);
}
this->retira1_pila (&pila, &q, &i);
if (i < q->cont)
{
r = q->apunt [i+1];
if (r->cont > N)
{
this->retirar (p, posicion);
temp = r->info[0];
this->retirar(r,0);
this->retirar (q,i);
k = this->donde (p, temp);
this->insertar (p, temp, &k);
k = this->donde (q, r->info [0]);
this->insertar (q, r->info [0], &k);
return (1);
}
}
if (i > 0)
{
r = q->apunt [i-1];
if (r->cont > N)
{
this->retirar (p, posicion);
temp = r->info [r->cont - 1];
this->retirar(r,r->cont - 1);
this->retirar (q,i-1);
k = this->donde (p, temp);
this->insertar (p, temp, &k);
k = this->donde (q, temp);
this->insertar (q, temp, &k);
return (1);
}
}
if (i > 0)
i--;
this->unir (raiz,q,r,p,i,pila,x,posicion);
return (1);
}
#endif
datos.h
#include<string>
#include<fstream>
using namespace std;
using std::ifstream;
using std::ofstream;
class nb{
private:
int codigo;
string nombre;
int codDep;
public:
nb(int, string, int);
nb(int, string, int,int);
nb(){}
bool operator<(nb &obj);
bool operator==(nb &obj);
int getCodDep();
int getCodigo();
string getNombre();
friend ostream& operator << (ostream &o, nb & prov){
o<<prov.nombre<<" "<<prov.codigo<<" "<<prov.codDep<<", ";
return o;
}
};
nb::nb(int codigo, string nombre, int codDep){
this->codigo=codigo;
this->nombre=nombre;
this->codDep=codDep;
ofstream fs(nombre+".txt");
fs<<codigo<<endl;
fs<<nombre<<endl;
fs<<codDep<<endl;
fs.close();
}
nb::nb(int codigo, string nombre, int codDep, int flat){
this->codigo=codigo;
this->nombre=nombre;
this->codDep=codDep;
}
bool nb::operator <(nb &obj){
return(this->nombre < obj.nombre);
}
bool nb::operator ==(nb &obj){
return(this->nombre == obj.nombre);
}
int nb::getCodDep(){
return codDep;
}
int nb::getCodigo(){
return codigo;
}
string nb::getNombre(){
return nombre;
}
class cb{
private:
int codigo;
string nombre;
int codDep;
public:
cb(int, string, int);
cb(int, string, int, int);
cb(){}
bool operator<(cb &obj);
bool operator==(cb &obj);
int getCodDep();
int getCodigo();
string getNombre();
friend ostream& operator << (ostream &o, cb & prov){
o<<prov.codigo<<" "<<prov.nombre<<" "<<prov.codDep<<", ";
return o;
}
};
cb::cb(int codigo, string nombre, int codDep){
this->codigo=codigo;
this->nombre=nombre;
this->codDep=codDep;
}
cb::cb(int codigo, string nombre, int codDep, int flat){
this->codigo=codigo;
this->nombre=nombre;
this->codDep=codDep;
}
bool cb::operator <(cb &obj){
return(this->codigo < obj.codigo);
}
bool cb::operator ==(cb &obj){
return(this->codigo == obj.codigo);
}
int cb::getCodDep(){
return codDep;
}
int cb::getCodigo(){
return codigo;
}
string cb::getNombre(){
return nombre;
}
class sb{
private:
int codigo;
string nombre;
int codDep;
public:
sb(int, string, int);
sb(int, string, int, int);
sb(){}
bool operator<(sb &obj);
bool operator==(sb &obj);
int getCodDep();
int getCodigo();
string getNombre();
friend ostream& operator << (ostream &o, sb & prov){
o<<prov.codDep<<" "<<prov.nombre<<" "<<prov.codigo<<", ";
return o;
}
};
sb::sb(int codigo, string nombre, int codDep){
this->codigo=codigo;
this->nombre=nombre;
this->codDep=codDep;
}
sb::sb(int codigo, string nombre, int codDep, int flat){
this->codigo=codigo;
this->nombre=nombre;
this->codDep=codDep;
}
bool sb::operator <(sb &obj){
return(this->codDep < obj.codDep);
}
bool sb::operator ==(sb &obj){
return(this->codDep == obj.codDep);
}
int sb::getCodDep(){
return codDep;
}
int sb::getCodigo(){
return codigo;
}
string sb::getNombre(){
return nombre;
}
///////////////////////////////////////////////////////////////////////////
class Texto{
private:
int codigo;
string nombre;
int telefono;
public:
Texto(int, string,int);
Texto(){}
bool operator<(Texto &obj);
bool operator>(Texto &obj);
bool operator==(Texto &obj);
int getCodigo();
string getNombre();
int getTelefono();
friend ostream& operator << (ostream &o, Texto & prov)
{
o<<prov.codigo<<" "<<prov.nombre<<" "<<prov.telefono<<", ";
return o;
}
};
Texto::Texto(int codigo, string nombre,int telefono)
{
ofstream fs(nombre+".txt");
fs << codigo << endl;
fs << nombre << endl;
fs << telefono << endl;
fs.close();
this->codigo=codigo;
this->nombre=nombre;
this->telefono=telefono;
}
bool Texto::operator <(Texto &obj){
return(this->codigo < obj.codigo);
}
bool Texto::operator >(Texto &obj){
return(this->codigo > obj.codigo);
}
bool Texto::operator ==(Texto &obj){
return(this->codigo == obj.codigo);
}
int Texto::getCodigo(){
return codigo;
}
int Texto::getTelefono(){
return telefono;
}
string Texto::getNombre(){
return nombre;
}
btreeType.h
#ifndef BTREE_H
#define BTREE_H
#include <iostream>
using namespace std;
template <class E>class BTree;
template <class E>class BNode{
private:
E *keys;
BNode<E>* *childs;
int count;
public:
BNode(int);
bool NodeFull(int);
bool NodeEmpty(int);
int getCount();
E operator[](int);
friend ostream& operator<<<>(ostream&,BNode<E>&);
friend class BTree<E>;
E getKey(int);
};
template <class E>BNode<E>::BNode(int nCount){
keys= new E[nCount];
childs= new BNode<E>*[nCount];
this->count=0;
for(int i=0;i<=nCount;i++)
childs[i]=0;
}
template <class E> bool BNode<E>::NodeFull(int nEle){
return (count==nEle);
}
template<class E>bool BNode<E>::NodeEmpty(int nEle){
return (count<nEle/2);
}
template<class E>int BNode<E>::getCount(){
return count;
}
template<class E> E BNode<E>::operator[](int index){
return (keys[index]);
}
template<class E> E BNode<E>::getKey(int index){
return(keys[index]);
}
template<class E> ostream & operator<<<>(ostream&o, BNode<E>&node){
for(int i=0;i<node.getCount();i++)
o<<"\t"<<node[i]<<", ";
o<<"\n";
return o;
}
template<class E>class BTree{
private:
BNode<E> *root;
int orden;
bool FindNode(BNode<E> *,E,int *);
public:
BTree(int);
BNode<E>* Find(E,int *, BNode<E>* =0,bool=true);
BNode<E>* borar(BNode<E> *, E, int);
void insert(E);
void push(BNode<E>*,E,int *,E*,BNode<E>**);
void putLeaf(BNode<E> *,E,BNode<E>*,int);
void dividedNode(BNode<E> *,E ,BNode<E> *,int,E *,BNode<E> **);
bool isEmpty();
void showTree(BNode<E> *);
BNode<E>* getRoot();
friend ostream& operator<<<>(ostream &o, BTree<E>&b);
};
template<class E>BTree<E>::BTree(int orden){
this->orden=orden;
root=0;
}
template<class E>BNode<E>* BTree<E>::getRoot(){
return root;
}
template<class E>bool BTree<E>::isEmpty(){
return (root==0);
}
template<class E>bool BTree<E>::FindNode(BNode<E> *current,E cl,int *pos){
(*pos)=0;
while((*pos)<current->count && current->keys[(*pos)]<cl)
(*pos)++;
return(cl==current->keys[(*pos)]);
}
template<class E>BNode<E>* BTree<E>::Find(E cl,int *pos,BNode<E> *Nodo,bool r){
if(r)
Nodo=root;
if(!Nodo)
return 0;
else{
bool fl;
fl=FindNode(Nodo,cl,pos);
if(fl)
return Nodo;
else
return Find(cl,pos,Nodo->childs[*pos],false);
}
}
template<class E>void BTree<E>::insert(E cl){
int up;
E median;
BNode<E> *pnew,*nd;
push(root,cl,&up,&median,&nd);
if(up){
pnew=new BNode<E>(orden);
pnew->count=1;
pnew->keys[0]=median;
pnew->childs[0]=root;
pnew->childs[1]=nd;
root=pnew;
}
}
template<class E>void BTree<E>::push(BNode<E> *current,E cl,int* up,E *median,BN
ode<E> **nuevo){
int k;
if(!current){
(*up)=1;
(*median)=cl;
(*nuevo)=0;
}
else{
bool fl;
fl=FindNode(current,cl,&k);
if(fl){
cout<<"Item duplicado\n";
(*up)=0;
return;
}
push(current->childs[k],cl,up,median,nuevo);
if(*up){
if(current->NodeFull(orden-1))
dividedNode(current,*median,*nuevo,k,median,nuev
o);
else{
(*up)=0;
putLeaf(current,*median,*nuevo,k);
}
}
}
}
template<class E>void BTree<E>::putLeaf(BNode<E> *current,E cl,BNode<E>* rd,int
k){
int i;
for(i=current->count-1;i>=k;i--){
current->keys[i+1]=current->keys[i];
current->childs[i+2]=current->childs[i+1];
}
current->keys[k]=cl;
current->childs[i+2]=rd;
current->count++;
}
template<class E>void BTree<E>::dividedNode(BNode<E> *current,E cl,BNode<E> *rd,
int k,E *median,BNode<E> **nuevo){
int i,posMdna;
posMdna=(k<=orden/2)?orden/2:orden/2+1;
(*nuevo)=new BNode<E>(orden);
for(i=posMdna;i<orden-1;i++){
(*nuevo)->keys[i-posMdna]=current->keys[i];
(*nuevo)->childs[i-posMdna+1]=current->childs[i+1];
}
(*nuevo)->count=(orden-1)-posMdna;
current->count=posMdna;
if(k<=orden/2)
putLeaf(current,cl,rd,k);
else
putLeaf(*nuevo,cl,rd,k-posMdna);
(*median)=current->keys[current->count-1];
(*nuevo)->childs[0]=current->childs[current->count];
current->count--;
}
template<class E>ostream& operator<<<>(ostream&o, BTree<E>&b){
if(b.isEmpty())
o<<"BTree is empty...";
else{
o<<"\tKeys Nodo\n";
b.showTree(b.getRoot());
}
return o;
}
template<class E>void BTree<E>::showTree(BNode<E> *current){
int i;
if(!current)
return;
cout<<*(current);
for(i=0;i<=current->count;i++)
showTree(current->childs[i]);
}

Anda mungkin juga menyukai