Level order Traversal of a Binary Tree ?? 
Author Message
 Level order Traversal of a Binary Tree ??

| Hi Folks,
|
| Looking for some examples (real source code) of a level order traversal of a
| Binary Tree.

YALS

struct Node
{
  struct Node *left, *right;
  char name[20];

Quote:
};

You need a queue for all the nodes at the same level. Usually the tree
is traversed left to right:

void enqueue( struct Node* );
struct Node* dequeue( void );

void
traverse( void )
{
  enqueue( root );
  go();

Quote:
}

void
go( void )
{
  struct Node *tmp = dequeue();

  while ( tmp ) {
    if ( tmp->left ) enqueue( tmp->left );
    if ( tmp->right ) enqueue( tmp->right );


    printf( "%s\n", tmp->name );

    /* let's get next sucker */
    tmp = dequeue();
  }

Quote:
}

Queue implementation is left to the reader.
Hope I didn't miss anything... :)

igorR
--
Vy vstaete s posteli, a lezhashaya ryadom devushka hvataet vas za ruku i
govorit:
- Save changes? Yes or No?



Mon, 21 May 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. given preorder and postorder traversals of a binary tree How can we find inorder

2. Binary tree traversal - a complex case - Help!

3. binary tree traversal

4. HELP!: non-recursive binary tree inorder traversal algorithm

5. Building Tree for list of nodes in level order

6. level order binary search

7. Binary trees question - unusual order traversing

8. print ascending order in a binary tree

9. printing binary trees pre-order, and indented.

10. HELP with Huffman Tree Traversal - huff2.c (1/1)

11. HELP with Huffman Tree Traversal - huff2.c (1/1)

12. HELP with Huffman Tree Traversal - huff2.c (0/1)

 

 
Powered by phpBB® Forum Software