Thread: question on binary tree

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    3

    Lightbulb question on binary tree

    I needed to print out the left and right child pointer but I dont know how could someone help?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your question could be interpreted several ways. Do you want to just print the value of the pointers?
    Code:
    printf("%p -- %p\n", (void *)root->left, (void *)root->right);
    Or do you want to print the contents of each node?
    Code:
    if (node->left != NULL)
      visit(root->left);
    if (node->right != NULL)
      visit(root->right);
    Or do you really want an inorder traversal?
    Code:
    void traverse(node *root, void (*action)(node *p))
    {
      if (root == NULL)
        return;
      traverse(root->left, action);
      action(root);
      traverse(root->right, action);
    }
    It's best to be as detailed as possible when asking a question. That way you can get an answer as quickly as possible.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. best STL method to implement a binary tree
    By MatthewDoucette in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2006, 07:08 AM
  3. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 09:11 AM
  4. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 04:08 PM