Bst not running problem

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

my code is stopped.can anyone help me?

#include<iostream>
#include <cstdlib>
using namespace std;

int n;
int a[100];

struct bst{
    int data;
    struct bst* left;
    struct bst* rigth;
}*node,*p,*q;

void insert(){
    node->data=a[0];

    for (int i = 0; i < n; i++)
    {
        /* code */
        q->data=a[i];
        p=node;
        while (true)
        {
            /* code */
            if(q->data<=p->data)
            {
               if(p->left==NULL)
               {
                   p->left=q;
                   break;
               }
               else
               {
                   p=p->left;
                   continue;
               }

            }

            else if(q->data>p->data)
            {
                if(p->rigth==NULL)
                {
                    p->rigth=q;
                    break;
                }
                else
                {
                    p=p->rigth;
                    continue;
                }

            }
        }

    }


}

void display()
{

}
int main()
{
    cin>>n;
    //a=(int*)malloc(n*sizeof(int));
    for (int i = 0; i < n; i++)
    {
        /* code */
        cin>>a[i];
        //cout<<*(a+i);
    }

    insert();

}

The binary search tree problem.

The first reason it segfaults is you’re accessing memory you haven’t allocated

the first instance of it is node but you haven’t allocated anything else yet

that malloc you commented out is only not needed as int[100] allocates the memory for you

the code is a bit of a mess in my opinion, I’d start cleaning it up to remove global variables for a start, and changing the spelling of right

as a reminder, you have to allocate memory before you use it

Thanks I work with it tomorrow.