Thread: Binary Search Tree in DLL

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    Sunny, Malta in the Mediterranean
    Posts
    9

    Binary Search Tree in DLL

    Hi,

    I have written the code for the basic functions of a binary search tree. All code works ok and I can use printf to view any results. The problem is I need to port the functions in a DLL and then link to the dll from a console application or a .net frontend.

    In the function that traverses the tree which I have written to use recursion, I need to get each value encountered to the front end. My function now returns a void. What can I use to return each node value from the function when it is compiled in the dll? Can I still use printf or is there a way to "return" each integer?
    I tried using return but this just gets the first value and returns (obviously). I just need it to return a value and loop or recurse in the function to get the rest of the node's values.
    I've been trying so many things I've ended up running in loops!

    Thanks for any input anyone may contribute.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Callback? Delegate?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    Sunny, Malta in the Mediterranean
    Posts
    9
    Excuse my ignorance, but what do you mean by Callback and delegate?

    Ok, looked them up, (took me a while to realise what you meant, sorry). Callback seems to be related to "special warnings". What I really want is to give the root node to a traversetree recursive function and for this function to give me back each integer it comes across so that I can print them on a screen. The traversetree function will be in a dll so it needs to pass the integers to the calling program.
    Last edited by Silvio; 05-09-2009 at 11:43 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    No a callback or delegate is what you want.
    No idea where you got "special warnings" from. Whatever purpose one person used it for is pretty much irrelevant. E.g. Is the sole purpose of exceptions to report division by zero? I don't think so!

    There are plenty of things callbacks can be used for. Just look up how it actually works.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    Sunny, Malta in the Mediterranean
    Posts
    9
    I found this interesting site on function pointers

    The Function Pointer Tutorials - Introduction

    It uses quicksort example of how to use function pointers to implement callback functions.

    I read the topic on function pointers and that of the callback functions but still cannot imagine, in my ignorance of course, how to use them.

    What would I pass as parameter to the traversetree function and how could it return every integer it comes across in the nodes? Or do I need to write an iterative traversetree function instead of a recursive one?

    I'm really baffled by this as this is my first C assignment and also my first need to implement a program in a library instead of the normal "everything included in program" sort of thing.

    For what help it may be, this is my traverse function:
    Code:
    /* Tree traversal */
    int TraverseTree ( NODE* traverse_Node )
    {
        if ( traverse_Node )
          {
            printf ( "%i ", traverse_Node->valKey );
            TraverseTree ( traverse_Node->Left );
            TraverseTree ( traverse_Node->Right );
          }
    }
    Thanks for your input

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Another approach could be to implement a linked list, providing an unmanaged interface to manipulate and query the list, and to add each item found in the tree to the list.

    Yet another approach would be to have the caller pass an array, with a length parameter as well, and have the function fill the array until length elements have been used. Your tree interface should have a method that retrieves the number of items in the tree (which can be kept track of by insertion/removal functions). The CLR will marshall the array type correctly. I think this one would be the easiest to implement out of the two.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Location
    Sunny, Malta in the Mediterranean
    Posts
    9
    The tree is implemented on heap, using win32 heap functions. The heap is declared of a certain size and as each node is created a part of the heap is reserved for it.
    So, I think by using a linked list to hold the values traversed would incur another amount of memory, although it is not important hwo much to use.

    I agree with you about the array. It had crossed my mind but I sort of put it aside for the reason, correct me if I'm wrong, that if the tree is traversed in pre-order, how would I display the values back on the calling program screen? I was thinking of getting each value returned to the caller program so it can be put on screen in any way, graphically or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 09:11 AM
  2. Binary Tree Search
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2004, 12:40 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Binary Search Tree, Inserting node
    By cheryl_li in forum C Programming
    Replies: 1
    Last Post: 09-13-2003, 03:53 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM