arbol binario

alex
06 de Mayo del 2004
necesito que por favor me ayuden hacer un arbol binario en c con archivos ... agradesco su colaboracion

noel solw
06 de Mayo del 2004
Supongo que tu necesitas un arbol binario implementado con pointers. Exite la posibilidad de implementar el arbol con array, cosa poco comun y un poco engorroza por cierto.

Te envio dos programas : el primero d13_1a.cpp que construyo un arbol binario con pointers y lo muestra en la pantalla, ordenado por supuesto (por que ?).
El segundo es un header file donde estan concentradas todas la definicion y funciones del arbol.
Por imposiciones de sencillez, mi programa construye el arbol a partir de un array, no tendras ningun problema en leerlo de un archivo, incluso, si no quieres trabajar mucho, leer primero el array del archivo y luego seguir el programa original.
Exito ! ! !

................................................................................................

// program d13_1a.cpp - page 319.
// binary tree : make and display.
// C exercices - daniel kilstein - 2000.

// written in borland c++ - ver 3.1

#include "d13.h"
#define N 20

void ShowArray(int *a)
{
cout << " data array : {";
for(int i = 0;i < N;i++)
{
if(i)
cout << ',';
cout << a[i];
}
cout << '}' << endl;
} // SHOW ARRAY

void main()
{
cout << "binary tree : make and display.n";
cout << "-----------------------------------------------------"
<< "-------------------------n";
int a[N] = {12,5,3,1,20,17,15,12,11,13,14,19,22,7,30,21,22,23,24,25};
ShowArray(a);
cout << "binary tree : ";
Tree p(N,a);
p.ShowTree();
cout << "-----------------------------------------------------"
<< "-------------------------n";
cout << "end of program - good bye ! ! ! n";
} // MAIN

.................................................................................................

// program d13.h - page 319.
// header file for binary tree.
// C exercices - daniel kilstein - 2000.
// written in borland c++ - ver 3.1

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <limits.h>
//------------------------------------------------------------------------------
struct Node
{
int info;
Node *left,
*right;
Node(int INFO = 0) : info(INFO),left(NULL),right(NULL) {}; // CONSTRUCTOR
friend ostream &operator<<(ostream &,const Node &);
friend istream &operator>>(istream &,Node &);
}; // NODE

ostream &operator<<(ostream &out,const Node &right)
{
out << right.info;
return out;
} // NODE OPERATOR <<

istream &operator>>(istream &in,Node &right)
{
cout << setw(20) << "enter info : ";
in >> right.info;
return in;
} // NODE OPERATOR >>
//------------------------------------------------------------------------------
struct Tree
{
Node *root;
int len;
Tree(int,int *);
~Tree();
void Insert(Node*,int);
void ShowTree();
}; // TREE

Tree::Tree(int num = 0,int *data = NULL)
{
len = 0;
if(!num)
root = NULL;
else
{
root = new Node(data[0]);
len++;
for(int i = 1;i < num;i++)
{
Insert(root,data[i]);
len++;
}
}
}; // TREE CONSTRUCTOR

void TreeDel(Node *p)
{
if(!p)
return;
TreeDel(p->left);
TreeDel(p->right);
delete p;
} // TREE DEL

Tree::~Tree()
{
if(len)
TreeDel(root);
} // TREE DESTRUCTOR

void Tree::Insert(Node *p,int data)
{
if(p->info < data)
{
if(!p->right)
p->right = new Node(data);
else
Insert(p->right,data);
}
else
{
if(!p->left)
p->left = new Node(data);
else
Insert(p->left,data);
}
} // TREE INSERT

int first;

void Traversal(Node *p)
{
if(!p)
return;
Traversal(p->left);
if(first)
{
cout << "{" << *p;
first = 0;
}
else
cout << "," << *p;
Traversal(p->right);
} // TREE TRAVERSAL

void Tree::ShowTree()
{
Node *p = root;
if(!p)
cout << "{empty tree}" << endl;
else
{
first = 1;
Traversal(p);
cout << "}" << endl;
}
} // SHOW TREE
//------------------------------------------------------------------------------