binary tree random access

This is a discussion on binary tree random access within the C Programming forums, part of the General Programming Boards category; hello =D i've looking for some help using the search but couldnt get it.. i need to implement a binary ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    8

    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

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

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

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    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

    Code:
    int _printn(NODE *tree, int n, int *currentNum)
    {
        if (!tree) 
            return 0;
    
        _printn(tree->left, n, currentNum);
        *currentNum = *currentNum + 1;
        if (*currentNum == n)
        {
            printf("%s\n",tree->word);
            printf("lool");
            return 1;
        }
        _printn(tree->right, n, currentNum);
    }
    And this function would be called from another function, to encapsulate the mess

    Code:
    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 Cprogramming.com
    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, 02:47 PM
  4. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 09:07 PM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 03:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21