Anda di halaman 1dari 22

15).

Elimina solo nodos terminales

void preorder(tree &T,iteratort n,list<int> &L) {


L.insert(L.end(),T.retrieve(n));

iteratort c = n.lchild();
while (c!=T.end()) {
preorder(T,c,L);
c = c.right();
}
}
void preorder(tree &T,list<int> &L) {
if (T.begin()==T.end()) return;
preorder(T,T.begin(),L);
}

//---:---<*>---:---<*>---:---<*>---:---<*>
void postorder(tree &T,iteratort n,list<int> &L) {
iteratort c = n.lchild();
while (c!=T.end()) {
postorder(T,c,L);
c = c.right();
}
L.insert(L.end(),T.retrieve(n));
}
void postorder(tree &T,list<int> &L) {
if (T.begin()==T.end()) return;
postorder(T,T.begin(),L);
}

//---:---<*>---:---<*>---:---<*>---:---<*>
void lispprint(tree &T,iteratort n) {
iteratort c = n.lchild();
if (c==T.end()) cout << T.retrieve(n);
else {
cout << "(" << T.retrieve(n);
while (c!=T.end()) {
cout << " ";
lispprint(T,c);
c = c.right();
}
cout << ")";
}
}
void lispprint(tree &T) {
if (T.begin()!=T.end()) lispprint(T,T.begin());
}

//---:---<*>---:---<*>---:---<*>---:---<*>
iteratort treecopy(tree &T,iteratort nt,
tree &Q,iteratort nq) {
nq = Q.insert(nq,T.retrieve(nt));
iteratort
ct = nt.lchild(),
cq = nq.lchild();
while (ct!=T.end()) {
cq = treecopy(T,ct,Q,cq);
ct = ct.right();
cq = cq.right();
}
return nq;
}

void treecopy(tree &T,tree &Q) {


if (T.begin() != T.end())
treecopy(T,T.begin(),Q,Q.begin());
}

//---:---<*>---:---<*>---:---<*>---:---<*>8
iteratort mirrorcopy(tree &T,iteratort nt,
tree &Q,iteratort nq) {
nq = Q.insert(nq,T.retrieve(nt));
iteratort
ct = nt.lchild(),
cq = nq.lchild();
while (ct != T.end()) {
cq = mirrorcopy(T,ct,Q,cq);
ct = ct.right();
}
return nq;
}

void mirrorcopy(tree &T,tree &Q) {


if (T.begin() != T.end())
mirrorcopy(T,T.begin(),Q,Q.begin());
}

//---:---<*>---:---<*>---:---<*>---:---<*>
iteratort pruneodd(tree &T,iteratort n) {
if (T.retrieve(n) % 2) n = T.erase(n);
else {
iteratort c = n.lchild();
while (c != T.end()) c = pruneodd(T,c);
n = n.right();
}
return n;
}

void pruneodd(tree &T) {


if (T.begin() != T.end()) pruneodd(T,T.begin());
}
14)
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>

using namespace std;

struct Nodo{
int dato;
Nodo *der;
Nodo *izq;
Nodo *padre;
};

Nodo *crearNodo(int,Nodo *);


Nodo *minimo(Nodo *);
void insertarNodo(Nodo *&,int,Nodo *);
void mostrarArbol(Nodo *,int);
bool busqueda(Nodo *,int);
void recorridoPreorden(Nodo *);
void recorridoInorden(Nodo*);
void recorridoPostorden(Nodo*);
void eliminar(Nodo *,int);
void eliminarNodo(Nodo *);
void reemplazar(Nodo *,Nodo*);
void destruir(Nodo *);

Nodo *crearNodo(int n,Nodo *padre){


Nodo *nuevo_nodo=new Nodo();
nuevo_nodo->dato=n;
nuevo_nodo->der=NULL;
nuevo_nodo->izq=NULL;
nuevo_nodo->padre=padre;

return nuevo_nodo;
}

void insertarNodo(Nodo *&arbol,int n,Nodo *padre){


if(arbol==NULL){
Nodo *nuevo_nodo=crearNodo(n,padre);
arbol=nuevo_nodo;
}else{
int valorRaiz=arbol->dato;
if(n<valorRaiz){
insertarNodo(arbol->izq,n,arbol);
}else{
insertarNodo(arbol->der,n,arbol);
}
}
}

void mostrarArbol(Nodo *arbol,int contador){


if(arbol==NULL){
return;
}else{
mostrarArbol(arbol->der,contador+1);
for(int i=0;i<contador;i++){
cout<<" ";
}
cout<<arbol->dato<<endl;
mostrarArbol(arbol->izq,contador+1);
}
}

void recorridoPreorden(Nodo *arbol){


if(arbol==NULL){
return;
}else{
cout<<arbol->dato<<"-";
recorridoPreorden(arbol->izq);
recorridoPreorden(arbol->der);
}
}

void recorridoInorden(Nodo *arbol){


if(arbol==NULL){
return;
}else{
recorridoInorden(arbol->izq);
cout<<arbol->dato<<"-";
recorridoInorden(arbol->der);
}
}

void recorridoPostorden(Nodo *arbol){


if(arbol==NULL){
return;
}else{
recorridoPostorden(arbol->izq);
recorridoPostorden(arbol->der);
cout<<arbol->dato<<"-";
}
}

bool busqueda(Nodo *arbol,int n){


if(arbol==NULL){
return false;
}else if(arbol->dato==n){
return true;
}else if(n<arbol->dato){
return busqueda(arbol->izq,n);
}else{
return busqueda(arbol->der,n);
}
}

void eliminar(Nodo *arbol,int n){


if(arbol==NULL){
return;
}else if(n<arbol->dato){
eliminar(arbol->izq,n);
}else if(n>arbol->dato){
eliminar(arbol->der,n);
}else{
eliminarNodo(arbol);
}
}

Nodo *minimo(Nodo *arbol){


if(arbol==NULL){
return NULL;
}
if(arbol->izq){
return minimo(arbol->izq);
}else{
return arbol;
}
}

void eliminarNodo(Nodo *nodoelem){


if(nodoelem->izq && nodoelem->der){
Nodo *menor=minimo(nodoelem->der);
nodoelem->dato=menor->dato;
eliminarNodo(menor);
}else if(nodoelem->izq){
reemplazar(nodoelem,nodoelem->izq);
destruir(nodoelem);
}
else if(nodoelem->der){
reemplazar(nodoelem,nodoelem->der);
destruir(nodoelem);
}else{
reemplazar(nodoelem,NULL);
destruir(nodoelem);
}
}
void reemplazar(Nodo *arbol,Nodo *nuevo){
if(arbol->padre){
if(arbol->dato==arbol->padre->izq->dato){
arbol->padre->izq=nuevo;
}else if(arbol->dato==arbol->padre->der->dato){
arbol->padre->der=nuevo;
}
}
if(nuevo){
nuevo->padre=arbol->padre;
}
}

void destruir(Nodo *nodo){


nodo->izq=NULL;
nodo->der=NULL;
delete nodo;
}

int main(){
int dato,opcion;
int contador=0;
Nodo *arbol=NULL;

do{
printf("\tMENU\n");
printf("1/Insertar nuevo Nodo\n");
printf("2/Mostrar nuevo Nodo\n");
printf("3/Buscar nuevo Nodo\n");
printf("4/Recorrido en Pre Orden\n");
printf("5/Recorrido en In Orden\n");
printf("6/Recorrido en Post Orden\n");
printf("7/Eliminar Nodo\n");
printf("8/Salir\n");
printf("Ingrese opcion\n");
cin>>opcion;
switch(opcion){
case 1:
cout<<"\nDigite un numero"<<endl;
cin>>dato;
insertarNodo(arbol,dato,NULL);
system("pause");
break;
case 2:
cout<<"Mostrando los datos"<<endl;
mostrarArbol(arbol,contador);
system("pause");
break;
case 3:
cout<<"Digite el elemento a buscar"<<endl;
cin>>dato;
if(busqueda(arbol,dato)){
cout<<"el elmento :"<<dato<<"fue enncontrado"<<endl;
}else{
cout<<"el elmento :"<<dato<<" NO fue enncontrado"<<endl;
}
system("pause");
break;
case 4:
cout<<"Recorrido en Pre orden"<<endl;
recorridoPreorden(arbol);
system("pause");
break;
case 5:
cout<<"Recorrido en In orden"<<endl;
recorridoInorden(arbol);
system("pause");
break;
case 6:
cout<<"Recorrido en Post orden"<<endl;
recorridoPostorden(arbol);
system("pause");
break;
case 7:
cout<<"ingrese nodo a eliminar"<<endl;
cin>>dato;
eliminar(arbol,dato);
system("pause");
break;
}
system("cls");
}while(opcion!=8);

getch();
return 0;
}

16 y 17)

#include
<iostream>
#include <stdlib.h>

using namespace std;

/*--------- Estructura del arbol -------*/


typedef struct nodo{
int nro;
struct nodo *izq, *der;
}*ABB;

int numNodos = 0; // nummero de nodos del arbol ABB


int numK = 0, k; // nodos menores que un numero k ingresado
/* ---------- Estructura de la cola ---------*/
struct nodoCola{
ABB ptr;
struct nodoCola *sgte;
};
struct cola{
struct nodoCola *delante;
struct nodoCola *atras;
};

void inicializaCola(struct cola &q)


{
q.delante = NULL;
q.atras = NULL;
}

void encola(struct cola &q, ABB n)


{
struct nodoCola *p;
p = new(struct nodoCola);
p->ptr = n;
p->sgte = NULL;
if(q.delante==NULL)
q.delante = p;
else
(q.atras)->sgte = p;
q.atras = p;
}

ABB desencola(struct cola &q)


{
struct nodoCola *p;
p = q.delante;
ABB n = p->ptr;
q.delante = (q.delante)->sgte;
delete(p);
return n;
}

ABB crearNodo(int x)
{
ABB nuevoNodo = new(struct nodo);
nuevoNodo->nro = x;
nuevoNodo->izq = NULL;
nuevoNodo->der = NULL;

return nuevoNodo;
}
void insertar(ABB &arbol, int x)
{
if(arbol==NULL)
{
arbol = crearNodo(x);
cout<<"\n\t Insertado..!"<<endl<<endl;
}
else if(x < arbol->nro)
insertar(arbol->izq, x);
else if(x > arbol->nro)
insertar(arbol->der, x);
}

void preOrden(ABB arbol)


{
if(arbol!=NULL)
{
cout << arbol->nro <<" ";
preOrden(arbol->izq);
preOrden(arbol->der);
}
}

void enOrden(ABB arbol)


{
if(arbol!=NULL)
{
enOrden(arbol->izq);
cout << arbol->nro << " ";
enOrden(arbol->der);
}
}

void postOrden(ABB arbol)


{
if(arbol!=NULL)
{
enOrden(arbol->izq);
enOrden(arbol->der);
cout << arbol->nro << " ";
}
}

void verArbol(ABB arbol, int n)


{
if(arbol==NULL)
return;
verArbol(arbol->der, n+1);

for(int i=0; i<n; i++)


cout<<" ";

numNodos++;
cout<< arbol->nro <<endl;

verArbol(arbol->izq, n+1);
}

bool busquedaRec(ABB arbol, int dato)


{
int r=0; // 0 indica que lo encontre

if(arbol==NULL)
return r;

if(dato<arbol->nro)
r = busquedaRec(arbol->izq, dato);

else if(dato> arbol->nro)


r = busquedaRec(arbol->der, dato);

else
r = 1; // son iguales, lo encontre

return r;
}

ABB unirABB(ABB izq, ABB der)


{
if(izq==NULL) return der;
if(der==NULL) return izq;

ABB centro = unirABB(izq->der, der->izq);


izq->der = centro;
der->izq = izq;
return der;
}
void elimina(ABB &arbol, int x)
{
if(arbol==NULL) return;

if(x<arbol->nro)
elimina(arbol->izq, x);
else if(x>arbol->nro)
elimina(arbol->der, x);

else
{
ABB p = arbol;
arbol = unirABB(arbol->izq, arbol->der);
delete p;
}
}

int alturaAB(ABB arbol)


{
int AltIzq, AltDer;

if(arbol==NULL)
return -1;
else
{
AltIzq = alturaAB(arbol->izq);
AltDer = alturaAB(arbol->der);

if(AltIzq>AltDer)
return AltIzq+1;
else
return AltDer+1;
}
}

void recorrerxNivel(ABB arbol)


{
struct cola q;
inicializaCola(q);
cout << "\t";

if(arbol!=NULL)
{
encola(q, arbol);
while(q.delante!=NULL)
{
arbol = desencola(q);
cout << arbol->nro << ' ';

if(arbol->izq!=NULL)
encola(q, arbol->izq);
if(arbol->der!=NULL)
encola(q, arbol->der);
}
}
}

ABB arbolEspejo(ABB arbol)


{
ABB temp;

if(arbol!=NULL)
{
temp = arbol->izq;
arbol->izq = arbolEspejo(arbol->der);
arbol->der = arbolEspejo(temp);
}
return arbol;
}

void nodosMenoresQueK(ABB arbol, int n)


{
if(arbol==NULL)
return;
nodosMenoresQueK(arbol->der, n+1);
if(arbol->nro<k)
numK++;
nodosMenoresQueK(arbol->izq, n+1);

int contarHojas(ABB arbol)


{
if (arbol==NULL)
{
return 0;
}
if ((arbol->der ==NULL)&&(arbol->izq ==NULL))
{
return 1;
}
else
{
return contarHojas(arbol->izq) + contarHojas(arbol->der);
}
}
void menu()
{
//system("cls");
cout << "\n\t\t ..[ ARBOL BINARIO DE BUSQUEDA ].. \n\n";
cout << "\t [1] Insertar elemento \n";
cout << "\t [2] Mostrar arbol \n";
cout << "\t [3] Recorridos de profundiad \n";
cout << "\t [4] Buscar elemento \n";
cout << "\t [5] Eliminar elemento \n";
cout << "\t [6] Recorrido por nieveles (Amplitud) \n";
cout << "\t [7] Altura del arbol \n";
cout << "\t [8] Construir arbol reflejo \n";
cout << "\t [9] Contar nodos \n";
cout << "\t [x] Contar hojas \n";
cout << "\t [11] Nodos menores de 'k' \n";
cout << "\t [12] SALIR \n";

cout << "\n\t Ingrese opcion : ";


}

void menu2()
{
//system("cls"); // para limpiar pantalla
cout << endl;
cout << "\t [1] En Orden \n";
cout << "\t [2] Pre Orden \n";
cout << "\t [3] Post Orden \n";
cout << "\n\t Opcion : ";
}

int main()
{
ABB arbol = NULL;
int x;
int op, op2;

//system("color f9"); // poner color a la consola


do
{
menu(); cin>> op;
cout << endl;
switch(op)
{
case 1:
cout << " Ingrese valor : "; cin>> x;
insertar( arbol, x);
break;

case 2:
verArbol(arbol, 0);
break;

case 3:
menu2(); cin>> op2;

if(arbol!=NULL)
{
switch(op2)
{
case 1:
enOrden(arbol); break;
case 2:
preOrden(arbol); break;
case 3:
postOrden(arbol); break;
}
}
else
cout << "\n\t Arbol vacio..!";
break;

case 4:
bool band;

cout<<" Valor a buscar: "; cin>> x;

band = busquedaRec(arbol,x);

if(band==1)
cout << "\n\tEncontrado...";
else
cout << "\n\tNo encontrado...";
break;
case 5:
cout<<" Valor a eliminar: "; cin>> x;
elimina(arbol, x);
cout << "\n\tEliminado..!";
break;

case 6:
cout<<"\n\n Mostrando recorrido por amplitud\n\n";
recorrerxNivel(arbol);
break;

case 7:
int h;
h = alturaAB(arbol);
cout << " La altura es : "<< h << endl;
break;

case 8:
ABB espejo;
espejo = NULL;

cout << "\n\n Arbol incial \n\n";

verArbol(arbol, 0);

cout << "\n\n Arbol espejo \n\n";

espejo = arbolEspejo(arbol);

verArbol(espejo, 0);
break;

case 9:
verArbol(arbol, 0);
cout << "\n\n El numero de nodos es : ";
cout << numNodos;
break;

case 11:
cout << " Ingrese k: "; cin>> k;
nodosMenoresQueK(arbol, 0);
cout <<" Son "<< numK << " numeros";
break;
case 12:
exit(0);
}

cout<<"\n\n\n";
//system("pause"); // hacer pausa y presionar una tecla para continuar
}while(op!=11);

18)
#include <cstdlib>
#include <iostream>

using namespace std;

/*
*
*
*
*
*
*/

struct misDatos{
string nombre;
int edad;
int num;
};

struct node{
misDatos datos;
node *left;
node *right;
};

node * newTree();
node * insert(int data, string name);

int printPreOrden(node* ptr);


int printRoot(node* head);
int printPostOrden(node *ptr);
int printInOrden(node *ptr);
int findMinimumValueLEFT(node* node);
int findMinimumValueRIGHT(node* node);

int size(node *node); //MAXIMO DE NODOS


int maxALTURA(node *node); //ALTURA

node * insert_right( int data);


node * insert_left( int data);
int contar_hojas(node *p) ;
int nodosInteriores(node *raiz );

node *max(node *n);


node *min(node *n);

int MAXIMOprintPreOrden(node* ptr);

int main() {

//ARBOL 1
node *raiz = newTree();
node *current = raiz;
node *arbol=raiz;

/*

----> GRAFICO DEL ARBOL BINARIO

A
/ \
B C
/\ / \
D E F G

*/

//A=10
//B=14
//C=16
//D=18
//E=20
//F=24
//G=30

arbol->left = insert(14,"B");
arbol->right = insert(16,"C");

current=arbol->left;

current->left = insert(50,"D");
current->right = insert(60,"E");

current=arbol->right;

current->left = insert(24,"F");
current->right = insert(30,"G");

cout << "\tR E C O R R I D O P R E - O R D E N\n";


printPreOrden(raiz);

//cout<<endl<<endl;
// cout << "\tR E C O R R I D O P O S T - O R D E N\n";
//printPostOrden(raiz);

//cout<<endl<<endl;
//cout << "\tR E C O R R I D O I N - O R D E N\n";
// printInOrden(raiz);
cout<<endl<<endl<<endl<<endl;

cout<<"NUMERO DE NODOS: "<<size(raiz)<<endl;

cout<<"ALTURA : "<<maxALTURA(raiz)<<endl;
cout<<"NUMERO DE HOJAS: "<<contar_hojas(raiz)<<endl;

cout<<"NODOS INTERIORES: "<<nodosInteriores(raiz)<<endl;

return 0;
}
//---------------> NEW TREE
node * newTree() {
node *nuevo;
nuevo = new node;

nuevo->datos.nombre= "A";
nuevo->datos.edad = 10; //
nuevo->right = nuevo;
nuevo->left = nuevo;

return nuevo;
}

// -------------> INSERTANDO
node* insert(int data, string name) {

node *nuevo;
nuevo = new node;
nuevo->datos.edad=data;
nuevo->datos.nombre=name;
nuevo->left=NULL;
nuevo->right=NULL;

return nuevo;
}

// -------------> O R D E N A M I E N T O S

// -----> PREORDEN

int printPreOrden(node* ptr) {

node *current = ptr;

printRoot(current); //RAIZ

if(current->left!=NULL){
printPreOrden(current->left); //IZQUIERDA
}

if(current->right!=NULL){
printPreOrden(current->right); //DERECHA
}
return current->datos.edad;
}

int printPostOrden(node *ptr){


node *current= ptr;

if(current->left!=NULL){
printPostOrden(current->left); //IZQUIERDA
}

if(current->right!=NULL){
printPostOrden(current->right); //DERECHA
}

printRoot(current);

int printInOrden(node *ptr){


node *current= ptr;

if(current->left!=NULL){
printInOrden(current->left); //IZQUIERDA
}

printRoot(current);

if(current->right!=NULL){
printInOrden(current->right); //DERECHA
}

//numero de nodos
int size(node *node){
if(node==NULL)
return 0;
else
return (size(node->left)+1+size(node->right));
}
//altura del arbol
int maxALTURA(node *node){
if(node==NULL)
return 0;
else{
int s=maxALTURA(node->left);
int m=maxALTURA(node->right);
if (s>m)
return (m+1);
else
return (s+1);
}return (size(node->left)+1+size(node->right));
}

//IMPRIMIENDO RAIZ

int printRoot(node* head)


{
cout<<"Nombre: "<<head->datos.nombre<<" Edad: "<< head->datos.edad<<"\n"<<endl;

//VALORES MAXIMOS Y MINIMOS


node *min(node *n) {
if (n == NULL || n->left == NULL)
return n;
else
return min(n->left);
}

node *max(node *n) {


if (n == NULL || n->left == NULL)
return n;
else
return max(n->left);
}

//-----> CONTAR HOJAS


int contar_hojas(node *p){
if (p == NULL)
return 0;
else if (p->left == NULL && p->right == NULL)
return 1;
else
return contar_hojas(p->left) + contar_hojas(p->right);
}

//NODOS INTERIORES
int nodosInteriores(node *raiz ){
return size(raiz)-contar_hojas(raiz);
}

Anda mungkin juga menyukai