Skip to content

Instantly share code, notes, and snippets.

@realFranco
Created May 31, 2022 11:03
Show Gist options
  • Save realFranco/917ffec76b1b36c148e02b8973507a41 to your computer and use it in GitHub Desktop.
Save realFranco/917ffec76b1b36c148e02b8973507a41 to your computer and use it in GitHub Desktop.
Searching Binary Tree constructor
/* 11*10*17
- TDA arboles
31*05*21
- This is a recovery file, expose a preview on binary trees data structures.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct N
{
int data;
struct N *left;
struct N *right;
}nodo;
//typedef nodo *root; // Deitel pag 445.pdf
typedef struct
{
nodo *T; // raiz del arbol
}root;
nodo *build( );
void in_AB( root *Raiz, int n );
nodo *build( )
{ // crea nodo
nodo *Temp;
Temp = (nodo *)malloc( sizeof( nodo ) );
Temp->data = 0;
Temp->left = NULL;
Temp->right = NULL;
return ( Temp );
}
void in_AB( root *Raiz, int n ) // Arbol Binario de Búsqueda
{
if( Raiz->T == NULL ) // arbol vacio
{
Raiz->T = build(); //*Raiz->T = malloc( sizeof( nodo ) );
if(Raiz->T != NULL)
{ // mem. asignada
Raiz->T->data = n;
Raiz->T->left = NULL;
Raiz->T->right = NULL;
}
}
else
{
if( n < Raiz->T->data )
{
in_AB( (root *)&(Raiz->T->left) , n );
}
else
{
if( Raiz->T->data < n )
{
in_AB( (root *)&(Raiz->T->right) , n );
}
}
}
}
int main()
{
clock_t tic = clock();
int i, j, x, y, Test, N;
root *Raiz;
root *temp;
nodo *T;
nodo *aux, *auxb;
Raiz = (root *)malloc( sizeof( root ) );
temp = Raiz;
printf("\t\tentrada < ");
scanf("%d", &N );
getchar();
for( i = 0; i < N; i++ )
{
scanf("%d", &x );
printf("%d", x);
if( i < N-1 )
printf(", ");
in_AB( Raiz, x );
}
printf(" > \n\n\n");
printf("\t\tpre_orden < ");
// preorden( Raiz->T );
printf(" >\n");
printf("\t\tin_orden < ");
// inorden( Raiz->T );
printf(" >\n");
printf("\t\tpostorden < ");
// postorden( Raiz->T );
printf(" >\n");
return( 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment