0% found this document useful (0 votes)
112 views4 pages

TAD Btree: Estructura de Datos II Página: 1

This document defines a B-tree data structure template in C++. It includes a BNode class to represent nodes in the B-tree and a BTree class that implements functions for operations on the B-tree like insertion, searching and printing the tree. The BNode class stores keys, child nodes and metadata. The BTree class implements functions for insertion that push new keys into nodes and split full nodes, as well as functions for searching keys and printing the structure of the tree.

Uploaded by

aqpalex87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views4 pages

TAD Btree: Estructura de Datos II Página: 1

This document defines a B-tree data structure template in C++. It includes a BNode class to represent nodes in the B-tree and a BTree class that implements functions for operations on the B-tree like insertion, searching and printing the tree. The BNode class stores keys, child nodes and metadata. The BTree class implements functions for insertion that push new keys into nodes and split full nodes, as well as functions for searching keys and printing the structure of the tree.

Uploaded by

aqpalex87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Estructura de Datos II Pgina:

M.Sc. Karim Guevara Puente de la Vega


1
TAD Btree


#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>;
};

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> ostream & operator <<(ostream &o, BNode<E>&node){
for(int i=0;i<node.getCount();i++)
o<<"\t"<<node[i]<<", ";
o<<"\n";
return o;
}







Estructura de Datos II Pgina:
M.Sc. Karim Guevara Puente de la Vega
2
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);
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);




Estructura de Datos II Pgina:
M.Sc. Karim Guevara Puente de la Vega
3
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,BNode<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,nuevo);
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];
}
Estructura de Datos II Pgina:
M.Sc. Karim Guevara Puente de la Vega
4
(*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]);
}

#endif

You might also like