私は通常の二分木を持っており、 c を使用して反復的な深さの最初の検索を適用しようとしています:
struct node {
int data;
struct node * right;
struct node * left;
};
typedef struct node node;
ノードをツリーに挿入する関数を使用していますが、検索関数を次のように実装する必要があります。つまり、
function search(root,goal,maxLevel)
深さ優先検索を使用して検索しますが、特定の最大レベルまで検索してから停止します。これは私の最初の試みでした。作業:
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);
}
}
}
助けてください、ありがとう...