Thread: binary tree random access

  1. #1
    Registered User
    Join Date
    Apr 2007

    Red face binary tree random access

    hello =D i've looking for some help using the search but couldnt get it.. i need to implement a binary tree and the usual funcions.. and i need a function that shows the in-ordern N element.. i could get it to work using a simple recursive function with a statis var (int printn(NODE *tree, int n))... it works.. the first time.. the second time it fails because the non-zero static variable..
    can any1 get a simple solution on this?

    any other comment on the code would be fine too

    and sorry for my (bad) english xD

    this code only need to pass a automatic batery test.. so dont worry about memory leaks and wrong input xD

    ty in advance
    Last edited by dlcp; 03-29-2008 at 03:55 PM.

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Well, one way to get around your problem is using something like a pointer to a variable that gets updated when an element is displayed. Something like

    int _printn(NODE *tree, int n, int *currentNum)
        if (!tree) 
            return 0;
        _printn(tree->left, n, currentNum);
        *currentNum = *currentNum + 1;
        if (*currentNum == n)
            return 1;
        _printn(tree->right, n, currentNum);
    And this function would be called from another function, to encapsulate the mess

    int printn(NODE *tree, int n)
        int i = 0;
        return _printn(tree, n, &i);
    Might work, didn't test. But this is not really good looking, there might be better way to do it.
    Last edited by foxman; 03-29-2008 at 05:55 PM. Reason: Mistake in the code
    I hate real numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. binary tree start
    By curlious in forum C++ Programming
    Replies: 6
    Last Post: 01-01-2004, 03:47 PM
  4. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 04:08 PM