尝试在C中为普通树建立广度的首先搜索算法

发布于 2025-01-27 17:26:46 字数 1515 浏览 3 评论 0原文

我尝试为普通树构建广度的第一个搜索算法,一个节点可以有多个(可以超过两个)的孩子。
我的算法的主要思想是我经历了所有首个kid,然后通过get()函数递归通过其右兄弟。所有结果都存储在2维char数组arr中。

  1. 一直向下行驶:row ++; col = 0
  2. 兄弟的正确行驶:col ++
  3. 上升并重复步骤2。
    (似乎我在将父亲和孩子存放到二维阵列中时失去了关系,我该如何保留) 如果每个人都进展顺利(实际上不是,GCC报告没有问题,但没有输出),则该输出应该像:
A
B C D
E F G

树结构:

struct node *head;
struct node { 
    struct node *firstKid;
    struct node *rightBro;
    char *token; 
};

主要功能:我忽略所有参数的creat-tree函数程序。

int main() {
    int m=100,n=100;
    // creat-tree function ignored here, the root of the tree  would be head(used in get()func

    char ***arr = (char ***)malloc(n*sizeof(char **));
    for(int i=0;i<n;i++) arr[i] = (char **)malloc(m*sizeof(char*));
    get(head,0,0,arr);
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            printf("%s",arr[i][j]);
        }
    }

}

我的迭代器:

void get(struct node *node,int row,int col,char ***arr){ 
    struct node *kid;
    struct node *bro;
    if(node!=NULL){
        arr[row][col]=node->token;
        row=row+1;col=0;
        if(node->firstKid)get(node->firstKid,row,col,arr);
        else{
            bro = node->rightBro;
            while(bro!=NULL){
                col ++;
                arr[row][col]=bro->token;
                bro = node->rightBro;
            }
        }
    }
}

I try to build a Breadth First Search algorithm for normal tree, which one node can have multiple(could more than two) children.
The main idea of my algorithm is that I go through all the first-kid, and then go through their right-brother by the get() function recursively. All results are stored in a 2-dimentional *char array arr.

  1. traveling all the way down :row++;col=0
  2. going right for brothers :col++
  3. going up and repeat step2.
    (It's seems like I lost the relationship between father and children when store them into the 2-D array, how can I remain it)
    If every goes well(which is actually not, gcc reports no problem but no output), the output is supposed to be like:
A
B C D
E F G

the tree struct:

struct node *head;
struct node { 
    struct node *firstKid;
    struct node *rightBro;
    char *token; 
};

the main function:I ignore the creat-tree function for all the parameters are passed from another program.

int main() {
    int m=100,n=100;
    // creat-tree function ignored here, the root of the tree  would be head(used in get()func

    char ***arr = (char ***)malloc(n*sizeof(char **));
    for(int i=0;i<n;i++) arr[i] = (char **)malloc(m*sizeof(char*));
    get(head,0,0,arr);
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            printf("%s",arr[i][j]);
        }
    }

}

my iterator:

void get(struct node *node,int row,int col,char ***arr){ 
    struct node *kid;
    struct node *bro;
    if(node!=NULL){
        arr[row][col]=node->token;
        row=row+1;col=0;
        if(node->firstKid)get(node->firstKid,row,col,arr);
        else{
            bro = node->rightBro;
            while(bro!=NULL){
                col ++;
                arr[row][col]=bro->token;
                bro = node->rightBro;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱我好吗 2025-02-03 17:26:46

广度优先的遍历:

  1. 创建一个访问的队列。
  2. 将根添加到队列中。
  3. 虽然队列不是空的,但
    1. dequeue a节点。
    2. 访问排水节点。
    3. 将节点的孩子添加到队列中。

搜索是相同的,除非找到感兴趣的节点后退出循环。

您没有队列(或任何服务目的的东西)。您的一般方法是有缺陷的。实际上,您正在通过使用堆栈(通过递归)而不是队列执行部分深度优先的遍历。


我没有解决特定问题,因为您必须重写代码,并且因为您未能提供该问题的证明。

Breadth-first traversal:

  1. Create a to-visit queue.
  2. Add the root to the queue.
  3. While the queue isn't empty,
    1. Dequeue a node.
    2. Visit the dequeued node.
    3. Add the node's children to the queue.

A search is the same, except you exit the loop once you find the node of interest.

You have no queue (or anything serving that purpose). Your general approach is flawed. In fact, you are performing a partial depth-first traversal by using a stack (through recursion) instead of a queue.


I didn't address the specific problem because you'll have to rewrite the code and because you failed to provide a demonstration of the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文