2010-07-22 13 views
12

¿Podría alguien dirigirme a algún tutorial sobre estructuras de datos de árbol usando C. intenté googlear pero la mayoría de las implementaciones son para C++ o Java.Si alguien me puede indicar algunos tutoriales en línea que están en C sería genial.Tutorial para estructura de datos de árbol en C

Gracias ..

+0

Leer Any Good DATA STRUCTURE book. – Tauquir

+0

Consulte la sección 4 en https://ece.uwaterloo.ca/~ece250/Lectures/Slides/ El sitio tiene muchas otras estructuras de datos e implementaciones de algoritmos y explicaciones sobre ellos y su tiempo de ejecución/análisis asintótico – rrazd

Respuesta

1

He aquí hace un poco de código tutorial de un par de décadas. De hecho, ha estado mintiendo por tanto tiempo, no recuerdo de dónde vino ni quién lo escribió (podría haber sido yo, pero realmente no estoy seguro). Teóricamente es un poco no portátil, usando strdup, que no es parte de la biblioteca estándar, aunque la mayoría de los compiladores lo tienen/lo suministran.

/* Warning: untested code with no error checking included. */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

/* A tree node. Holds pointers to left and right sub-trees, and some data (a string). 
*/ 
typedef struct node { 
    struct node *left; 
    struct node *right; 
    char *string; 
} node; 

node *root; /* pointers automatically initialized to NULL */ 

int insert(const char *string, node *root) { 

    /* Add a string to the tree. Keeps in order, ignores dupes. 
    */ 
    int num = strcmp(root->string, string); 
    node *temp; 

    for(;;) { 
     if (0 == num) 
      /* duplicate string - ignore it. */ 
      return 1; 
     else if (-1 == num) { 
      /* create new node, insert as right sub-tree. 
      */ 
      if (NULL == root -> right) { 
       temp = malloc(sizeof(node)); 
       temp -> left = NULL; 
       temp -> right = NULL; 
       temp -> string = strdup(string); 
       return 2; 
      } 
      else 
       root = root -> right; 
     } 
     else if (NULL == root ->left) { 
      /* create new node, insert as left sub-tree. 
      */ 
      temp = malloc(sizeof(node)); 
      temp -> left = NULL; 
      temp -> right = NULL; 
      temp -> string = strdup(string); 
      return 2; 
     } 
     else 
      root = root -> left; 
    } 
} 


void print(node *root) { 
    /* in-order traversal -- first process left sub-tree. 
    */ 
    if (root -> left != NULL) 
     print(root->left); 
    /* then process current node. 
    */ 
    fputs(root->string, stdout); 

    /* then process right sub-tree 
    */ 
    if (root->right != NULL) 
     print(root->right); 
} 

int main() { 

    char line[100]; 

    /* Let user enter some data. Enter an EOF (e.g., ctrl-D or F6) when done. 
    */ 
    while (fgets(line, 100, stdin)) 
     insert(line, root); 

    /* print out the data, in order 
    */ 
    print(root); 
    return 0; 
} 
+1

@ Jerry ... Estaba buscando más tutorial en lugar del código real. Quería tener un buen control de los conceptos y luego probarlo yo mismo ... ¡De todos modos, gracias! –

+0

@The Stig: Ah, pero esto es solo un punto de partida - actualmente no admite la búsqueda de valores particulares (debería ser bastante fácil), eliminación (algo no trivial), mantener el equilibrio (* decididamente * no trivial), etc. –

Cuestiones relacionadas