1

i am having a normal binary tree that i am trying to apply iterative deepening depth first search on using c :

struct node {
    int data;
    struct node * right;
    struct node * left;
};

typedef struct node node;

and i am using a function to insert nodes into tree, now i need to implement the search function to be something like this: function search(root,goal,maxLevel) so it search using depth first search but to a specific max level then stop that was my first try,it doesn't work :

currentLevel = 0;
void search(node ** tree, int val, int depth)
{
    if(currentLevel <= depth) {
        currentLevel++;
        if((*tree)->data == val)
        {
            printf("found , current level = %i , depth = %i", currentLevel,depth);

        } else if((*tree)->left!= NULL && (*tree)->right!= NULL)
        {
            search(&(*tree)->left, val, depth);
            search(&(*tree)->right, val, depth);
        }
    }
}

please help, thanks ...

Tordek
  • 10,628
  • 3
  • 36
  • 67
Heidar
  • 689
  • 4
  • 12
  • Please describe *what* is not working. – Keith Randall Apr 20 '13 at 22:51
  • Why don't you decrement `depth` everytime you go deeper? Then check for it to be more than zero. – Kninnug Apr 20 '13 at 22:55
  • You probably don't want a global variable `currentLevel()`. You probably shouldn't search both the left and right sub-trees — you should probably search the left sub-tree if the value is smaller than the value in the current node, and the right sub-tree if the value is larger than the value in the current node. You probably need to return the pointer to the node where the value was found; you may need some way to distinguish between 'not found', 'too deep' and 'found'. – Jonathan Leffler Apr 20 '13 at 22:59
  • 1
    Since you're not modifying the tree, there's no obvious reason to use a `node **tree` for the argument; it would be sufficient to use `node *tree` (or, better, `const node *tree`). – Jonathan Leffler Apr 20 '13 at 23:04

2 Answers2

2

You never stop...

node *search(node ** tree, int val, int depth)
{
    if (depth <= 0)
    {
        return NULL; // not found
    }

    if((*tree)->data == val)
    {
        return *tree;
    }

    if((*tree)->left)
    {
        node * left = search(&(*tree)->left, val, depth - 1);
        if (left) return left; // found
    }
    if((*tree)->right)
    {
        node * right = search(&(*tree)->left, val, depth - 1);
        return right; // whatever is result of right
    }
    return NULL; // not found
}
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
  • This implementation will decrement depth twice (1 in left recursive call and 1 in right) so it won't reach the desired depth . – Heidar Apr 23 '13 at 18:56
1

The global variable won't work for this. You want to have something like

void search(node ** tree, int val, int remainingDepth) {
    if (remainingDepth == 0) return;

then

        search(&(*tree)->left, val, remainingDepth - 1);
        search(&(*tree)->right, val, remainingDepth - 1);

You probably also want to check left & right for null separately, as each could be independently null.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54