Binary Tree Traversals and Expression Evaluation in C

Computer science3.47 KB

Binary Tree Traversals and Expression Evaluation in C

This document contains formatted C code snippets for expression tree evaluation and binary tree traversals (preorder, inorder and postorder). Comments and strings are corrected for clarity while preserving original content.

Expression Tree Evaluation Function

 int evaluate (NodeB n, btree T)
 (
   char ident;

   EtiquetaArbolB (& c, n, T);
   switch (c) (
     case '+':
       return evaluate (HijoIzqdaB (n, T), G) + evaluate (HijoDrchaB (n, T), T);
       break;
     case '-':
       return evaluate (HijoIzqdaB (n, T), T)-Evaluation (HijoDrchaB (n, T), T);
       break;
     case '*':
       return evaluate (HijoIzqdaB (n, T), T) * Evaluation (HijoDrchaB (n, T), T);
       break;
     case '/':
       return evaluate (HijoIzqdaB (n, T), T) / Evaluation (HijoDrchaB (n, T), T);
       break;
     default:
       return value (c);
 

Postorder from Preorder and Inorder

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

char * preorder = "GEAIBMCLDFKJH";
char * inorder = "IABEGLDCFMKHJ";
char * postorder;

/*---------------------------------------*/

void post (char * pre, char * in, char * pos, int n)
(
  int longIzqda;

  if (n> 0) (
    pos [n-1] = pre [0];
    longIzqda = strchr (in, pre [0])-in;
    post (pre +1, in, pos, longIzqda);
    post (pre +1 + longIzqda, in +1 + longIzqda, longIzqda pos + n-1-longIzqda);
  )
)

/*----------------------------------------*/

int main (int argc, char * argv [])
(
  int aux;

  aux = strlen (inorder);
  postorder = (char *) malloc (aux * sizeof (char));
  if (postorder) (
    printf ("Preorder: %s\n", preorder);
    printf ("Inorder: %s\n", inorder);
    post (preorder, inorder, postorder, aux);
    postorder [aux] = '\0';
    printf ("Postorder (calculated): %s\n", postorder);
    free (postorder);
  )
  else (
    fprintf (stderr, "Error: Out of memory\n");
    return 1;
  )

  return 0;
____________________________________

Non-Recursive Inorder Traversal (B-tree)

# include <pilas.h>
# include <arbolesB.h>


/*---------------------------------------*/

void inordenNR (btree T, void (* EscribirElemento) (void *), int size)
(
  NodeB nodoActual, aux;
  void * et;
  Stack p;
  int end;
  faltaHD int / * Indicates whether lack the right child * /

  p = CrearPila (sizeof (NodeB));
  et = malloc (size);
  if (et) (
    .... / * Error: Out of memory * /
  )

  aux = NODOB_NULO;
  Push (& aux, p);
  nodoActual = RaizB (T);
  end = 0;
  while (! end) (
    while (nodoActual! = NODOB_NULO) (
      Push (& nodoActual, p);
      nodoActual = HijoIzqdaB (nodoActual, T);
    )
    Tope (& nodoActual, p);
    Pop (p);
    if (nodoActual! = NODOB_NULO) (
      EtiquetaArbolB (et, nodoActual, T);
      (* EscribirElemento) (et);
      nodoActual = HijoDrchaB (nodoActual, T);
    )
    else order = 1;
  )

  free (et);
  DestruirPila (p);
)

Notes

  • Preserved content: All original code text is preserved and presented in formatted blocks. Angle brackets and markup characters are escaped for valid HTML.
  • Corrections: Visible strings and messages were adjusted for correct spacing and newline escapes, and descriptive text was clarified for grammar and capitalization.
  • Headings: Only h2–h4 headings are used; each heading is concise and under 60 characters.

Download

PDF Word HTML