简体   繁体   English

我的二进制搜索树的预遍历代码正在工作,但是每个元素都是指向结构的指针的堆栈如何工作?

[英]My code for pre-order traversal of Binary Search Tree is working but how is the stack whose each element is a pointer to the structure working?

This is my code of preorder traversal of BST. 这是我的BST遍历代码。 It's working fine on Ubuntu. 在Ubuntu上运行正常。 But I don't understand one thing. 但是我不明白一件事。 In the function iterative_preorder() , I actually wanted a stack to store the pointers to the structure that I defined on the top. 在函数iterative_preorder() ,我实际上想要一个堆栈来存储指向我在顶部定义的结构的指针。 I want to know the concept that how is it working. 我想知道它是如何工作的。 Since, while allocating memory to the stack, I didn't specify anywhere separately that stack should contain size number of pointers to the structure. 因为在向堆栈分配内存时,我没有在任何地方单独指定堆栈应该包含指向该结构的指针的size数。

Like, when we define: 就像,当我们定义:

int stack[size];

We know that stack[1] will be the second block in the stack. 我们知道stack[1]将是stack[1]中的第二个块。 But here, I used malloc , which actually just makes one block of the size specified as size * sizeof(node *) . 但是在这里,我使用了malloc ,它实际上仅使一个块的大小指定为size * sizeof(node *)

So when the program executes: 因此,当程序执行时:

stack[++top] = root;

How does the program understand that it has to go to the next pointer to the structure in the stack? 程序如何理解它必须转到指向堆栈中结构的下一个指针? I hope my question is clear. 希望我的问题清楚。

I made another small program, based on the confusion that I had. 基于我的困惑,我制作了另一个小程序。 Here, instead of structure, I used int . 在这里,我使用int代替结构。 I tried to create a stack of size 2, which stores pointers to the integer. 我试图创建一个大小为2的堆栈,该堆栈存储指向整数的指针。 Here's the code: 这是代码:

#include <stdio.h>
#include <stdlib.h>

void main() {
    int** stack = (int**)malloc(2 * sizeof(int*));
    printf("%d", *stack[0]);
}

But this code is throwing segmentation fault (core dumped) . 但是此代码引发了segmentation fault (core dumped) As both the codes used the same logic, just that this one used int instead of structure, I don't understand why this is throwing error. 由于这两个代码使用相同的逻辑,只是该代码使用int而不是结构,所以我不明白为什么这会引发错误。

#include <stdio.h>
#include <stdlib.h>

int size = 0;

typedef struct mylist {
    int data;
    struct mylist *left;
    struct mylist *right;
} node;
node *root;

void create_root(node *root) {
    root = NULL;
}

//Inserting nodes
node *insert(node *root, int val) {
    node *ptr, *parentptr, *nodeptr;
    ptr = (node*)malloc(sizeof(node));
    ptr->data = val;
    ptr->left = NULL;
    ptr->right = NULL;
    if (root == NULL)
        root = ptr;
    else {
        parentptr = NULL;
        nodeptr = root;
        while (nodeptr != NULL) {
            parentptr = nodeptr;
            if (val < nodeptr->data)
                nodeptr = nodeptr->left;
            else
                nodeptr = nodeptr->right;
        }
        if (val < parentptr->data)
            parentptr->left = ptr;
        else
            parentptr->right = ptr;
    }
    return root;
}

void iterative_preorder(node *root) {
    if (root != NULL) {
        int top = -1;
        node **stack = (node**)malloc(size * sizeof(node*));
        node *cur;
        stack[++top] = root;
        while (top > -1) {
            cur = stack[top--];
            printf("%d\t", cur->data);
            if (cur->right != NULL)
                stack[++top] = cur->right;
            if (cur->left != NULL)
                stack[++top] = cur->left;
        }
    }
}

void main() {
    int option, val;
    node *ptr;
    int flag = 1;
    create_root(root);
    while (flag != 2) {
        printf("\nChoose-\n1-Insert\n2-Iterative Preorder Traversal\n3-Exit\n");
        scanf("%d", &option);
        switch (option) {
        case 1: {
                printf("\nEnter the value of new node\n");
                size++;
                scanf("%d", &val);
                root = insert(root, val);
            }
            break;
        case 2:
            iterative_preorder(root);
            break;
        case 3:
            flag = 2;
            break;
        default:
            printf("\nWrong entry\n");
        }
    }
}

Your code has a dereference of uninitialized pointer error. 您的代码取消引用了未初始化的指针错误。

    int** stack = (int**)malloc(2*sizeof(int*));
    printf("%d",*stack[0]);

In the above code, stack points to an array of two int pointers, what stack[0] points to? 在上面的代码中, stack指向一个包含两个int指针的数组,堆栈[0]指向什么? it's not initialized. 它没有初始化。

A live test of your code is available here segfault . segfault可以在此处进行代码的实时测试。 you can modify and test it again. 您可以修改并再次测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM