Build and Traverse Binary Tree - C Data Structure

C examples for Data Structure:Tree

Description

Build and Traverse Binary Tree

Demo Code

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

#define MaxWordSize 20/*from   w  w w. j  av a  2s  .com*/

typedef struct {
   char word[MaxWordSize+1];
} NodeData;

typedef struct treeNode {
   NodeData data;
   struct treeNode *left, *right;
} TreeNode, *TreeNodePtr;

typedef struct {
   TreeNodePtr root;
} BinaryTree;

TreeNodePtr buildTree(FILE *);
void preOrder(TreeNodePtr);
void inOrder(TreeNodePtr);
void postOrder(TreeNodePtr);


int main() {

   FILE * in = fopen("btree.in", "r");

   BinaryTree bt;

   bt.root = buildTree(in);

   printf("\nThe pre-order traversal is: ");

   preOrder(bt.root);

   printf("\n\nThe in-order traversal is: ");

   inOrder(bt.root);

   printf("\n\nThe post-order traversal is: ");

   postOrder(bt.root);

   printf("\n\n");

   fclose(in);
}

TreeNodePtr buildTree(FILE * in) {
   char str[MaxWordSize+1];
 
   fscanf(in, "%s", str);
 
   if (strcmp(str, "@") == 0) 
      return NULL;
 
   TreeNodePtr p = (TreeNodePtr) malloc(sizeof(TreeNode));
 
   strcpy(p -> data.word, str);
 
   p -> left = buildTree(in);
 
   p -> right = buildTree(in);
 
   return p;
}

void visit(TreeNodePtr node) {
   printf("%s ", node -> data.word);
}

void preOrder(TreeNodePtr node) {
   void visit(TreeNodePtr);
   if (node != NULL) {
      visit(node);
      preOrder(node -> left);
      preOrder(node -> right);
   }
}

void inOrder(TreeNodePtr node) {
   void visit(TreeNodePtr);
   if (node != NULL) {
      inOrder(node -> left);
      visit(node);
      inOrder(node -> right);
   }
}

void postOrder(TreeNodePtr node) {
   void visit(TreeNodePtr);
   if (node != NULL) {
      postOrder(node -> left);
      postOrder(node -> right);
      visit(node);
   }
}

Related Tutorials