0

Does anyone have any good examples of building a tree data structure both iteratively and recursively in C Language (!C++)? Also to traverse through the tree and cover every node..

I'm likely to use this struct:

typedef struct treeNode *treeLink;
struct treeNode {
    Item item;
    treeLink left, right;
};
paddy
  • 60,864
  • 6
  • 61
  • 103
ryantata
  • 137
  • 1
  • 3
  • 12
  • Is this also likely to be homework? – Greg Hewgill Aug 13 '12 at 02:07
  • 3
    Naturally, you tried a Google search for something like [building a binary tree in C](http://www.google.co.nz/search?q=binary+tree+in+C) and reviewed the plethora of useful results prior to posting this question... – paddy Aug 13 '12 at 02:14
  • yeah its homework and im always used to arrays, i've built a small tree structure with 3 levels of random ints with pure iteration. But am right now am finding cleaner code to recursively program this binary tree without missing branches or levels. – ryantata Aug 13 '12 at 11:18

1 Answers1

1
treeLink Traverse_In_Order(treeLink  current){
          if(curernt == NULL) return NULL;
          Traverse(current->left);
          operation(current->item);
          Traverse(current->right);
          return current; // in case you want to do something on this node
}


treeLink Traverse_Pre_Order(treeLink  current){
          if(curernt == NULL) return NULL;
          operation(current->item);
          Traverse(current->left);        
          Traverse(current->right);
          return current; // in case you want to do something on this node
}

treeLink Traverse_Pos_Order(treeLink  current){
          if(curernt == NULL) return NULL;
          Traverse(current->left);        
          Traverse(current->right);
          operation(current->item);
          return current; // in case you want to do something on this node
}
Jing
  • 895
  • 6
  • 14
  • 38