Thread: Problem with recursive funtion

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Problem with recursive funtion

    Hi!

    I have a problem with this recursive function. Tell me what changes variables i and j.

    Code:
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <ctype.h>
    
    # define MAX 1001
    
    typedef struct st
    {
    	int dat;
    	struct st *left;
    	struct st *right;
    };
    typedef struct st* OBST;
    
    BST B_Tree(int i, int j)
    {
    	OBST New = (OBST)malloc(sizeof(struct st));
    	int x;
    
    	if(i == j)
    	{
    		New = NULL;
    		return New;
    	}
    	else
    	{
    		x = r[i][j];
    		New->dat = keys[x];			//write key
    		New->right = B_Tree(x, j);	//repeat
    		New->left = B_Tree(i, x-1);	
    		printf ("\ni = %d\tj = %d\tx = %d", i, j, x);
    		return New;
    	}
    }
    int main ()
    {
        OBST root;
    
        root = B_Tree(0, 1000);
        return 0;
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    New->right = B_Tree(x, j);
    New->left = B_Tree(i, x-1);
    You are calling the function here with different values.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Yes I know that but how are i and j variables changed. In B_Tree function there is nothing like i++ or j++ (i-- or j--) that would change the values. So please explain this to me.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Not sure what you mean but here

    New->right = B_Tree(x, j);

    you call B_Tree with a different value for i. The value for i in the new call is equal to x. And here

    New->left = B_Tree(i, x-1);

    The value for j in the new call is equal to x-1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function problem
    By trnd in forum C Programming
    Replies: 5
    Last Post: 01-30-2009, 12:36 AM
  2. recursive function problem
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 10-25-2002, 06:02 AM
  3. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  4. Problem with a recursive function
    By fofone in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 08:21 AM
  5. Recursive problem...
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2002, 04:20 PM