Advertisement
heavenriver

binarytree.c

Nov 24th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. /* implementation and operations with binary trees */
  2.  
  3.  /* pointer arithmetics:
  4.   *
  5.   * push(pointer, v) INDICATES *(pointer++) = v
  6.   * pop(pointer) INDICATES *(--pointer)
  7.   * (*++v)[0] INDICATES (**++v)
  8.   *           INDICATES first char in v
  9.   *           INDICATES name of string/vector v
  10.   * likewise, *v[0] INDICATES **v
  11.   *       and *v[n] INDICATES **(v + n)
  12.   * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
  13.   * char **argv INDICATES pointer to char pointer
  14.   * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
  15.   * int *v[len] INDICATES vector "v" of "len" pointers to int elements
  16.   * void *funct() INDICATES function "funct" that returns a pointer-to-void
  17.   * void (*funct)() INDICATES pointer to a function "funct" that returns void
  18.   *
  19.   */
  20.  
  21.  /* useful characters: [] # */
  22.  
  23.  # include <stdio.h>
  24.  # include <ctype.h>
  25.  # include <string.h>
  26.  
  27.  # define MAXL 100
  28.  
  29.  /* binary tree node */
  30.  
  31.  struct node {
  32.    char * data;
  33.    int value;
  34.    struct node * left;
  35.    struct node * right;
  36.  };
  37.  
  38.  /* node manipulation */
  39.  
  40.  struct node * newtree(struct node * n, char * data);
  41.  void print(struct node * n);
  42.  int getvalue(char * data, int value);
  43.  
  44.  /* main */
  45.  
  46.  int main(int argc, char * argv[])
  47.     {
  48.      struct node * root; // binary tree root
  49.      char data[MAXL];
  50.      root = NULL;
  51.      
  52.      while(getword(data, MAXL) != EOF) // extracts data until the end of file...
  53.     root = newtree(root, data); // ...and parses it into a new tree node
  54.      print(root); // prints the tree
  55.      return 0;
  56.     }
  57.  
  58.  /* creates a new node and appends it to the current tree, generating a new subtree */
  59.  
  60.  struct node * newtree(struct node * n, char * data)
  61.     {
  62.      if(n = NULL) // case 1: the tree has just been created
  63.     {
  64.     n = (struct node *)malloc(sizeof(struct node)); // IMPORTANT: creates a new node by allocating it in memory directly
  65.     n->data = strdup(data); // copies the data into the new node
  66.     n->value = 1;
  67.     n->left = n->right = NULL;
  68.     }
  69.      else // case 2: the tree is already present and needs a subtree
  70.     {
  71.      if(strcmp(data, n->data) == 0) // word repeated
  72.         n->value++;
  73.      else if(strcmp(data, n->data) < 0) // adds to left subtree
  74.         n->left = newtree(n->left, data);
  75.      else // adds to right subtree
  76.         n->right = newtree(n->right, data);
  77.     }
  78.     return n;
  79.     }
  80.  
  81.  /* prints the binary tree */
  82.  
  83.  void print(struct node * n)
  84.     {
  85.      if(n != NULL)
  86.     {
  87.      print(n->left);
  88.      printf("Value: %d Data: %s\n", n->data);
  89.      print(n->right);
  90.     }
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement