#include<stdio.h>
#include<conio.h>
struct bnode{
       struct bnode *child[5];
       int count;
       int value[5];
       };
       typedef struct bnode node;
       
node * root=NULL;
void insert(int val);
int put(int,node*,int *,node**);
void fillnode(int ,node *,node*,int);
int search(int,node *,int *);
void insert(int val)
{
     int i,flag;
     node * c,*n;
     flag=put(val,root,&i,&c);
     if(flag)
     {
                n=(node*)malloc(sizeof(node));
                n->count=1;//setting count 1
                n->value[1]=i;//setting node's value to i
                n->child[0]=root;//setting o th child to root
                n->child[1]=c;//setting first child to null/(Split case)
     }
}
int put(int value,node * n,int * p,node ** c)
{
    int l;//storing the position
    if(n==NULL)//checking if n is NULL
    {
               *p=value;//setting value
               *c=NULL;//setting child to NULL
               return 1;
    }
    else{
         if(search(value,n,&l))//if search return 1 ie is successful
          {printf("\nDuplicate value not allowed\n");
           }
         if(put(value,n->child[l],p,c))
          {     
                if(n->count<4)
                {
                              fillnode(*p,*c,n,l);
                              return 0;
                }
         }return 0;
         
    
}}
void fillnode(int val,node * c,node * n,int k)
{
     int i;
     for(i=n->count;i>k;i--)
     {
          n->value[i+1]=n->value[i];//shifting down
          n->child[i+1]=n->child[i];
          
     }
     n->value[k+1]=val;//setting value
     n->child[k+1]=c;//setting child
     n->count++;//setting count
}
int search(int val,node* n,int *pos)
{
    if(val<n->value[1])
    {*pos=0;return 0;//value cant exist if smaller than first value
    }
     else 
     {
      *pos=n->count;
      while((val<n->value[*pos])&& *pos>1)
      {
       (*pos)--;     
      }          
      if(val==n->value[*pos])//successful search
      return 1;
      else return 0;        
     }                  
}
void display(node * root)
{
     int i;
     if(root!=NULL)
     {
                   for(i=1;i<root->count;i++)
                   {
                      display(root->child[i-1]);
                      printf("%d\t",root->value[i]);
                   }
                   display(root->child[i-1]);
     }
}
//int main()
//{   
//    int ch=1,value;
//    do{
//        printf("\n\t\t\t\tMenu\n1)Insert2)Delete3)Display0)Exit\n");
//        printf("Enter choice please\n");
//        scanf("%d",&ch);
//        switch(ch)
//        {
//                  case 0:return;
//                  case 1:printf("\nEnter value to insert");scanf("%d",&value);insert(value);display(root);break;
//                  case 3:display(root);break;
//        }
//        printf("\nDo you want to continue\n1/0");
//        scanf("%d",&ch);
//}while(ch!=0);
//    getch();
//    return 0;
//}
int main()
{
int i=0;
for(i=1;i<=14;i++)
insert(i*i);
display(root);
getch();
return 0;
}

I have made this program for 4 key B-TREE.It gives no output when I run it.
I am not able to figure out what the problem is.
Please help.

Dani AI

Generated

Immediate cause of "no output": in 's insert() the new node n is allocated and populated but never installed as root. That leaves root==NULL, so display(root) prints nothing. Add an assignment and initialize children after allocating n:

/* after creating and populating n */
for (i = 0; i < 5; ++i) n->child[i] = NULL;   /* avoid garbage pointers */
root = n;                                      /* critical: make n the tree root */

Other bugs that will surface once the root is set

  • Duplicate handling: search() currently prints a message but put() still proceeds. If search() returns 1, put() should abort the insertion (return 0) so duplicates are not propagated.
  • Splitting is missing: when a node is full (count == 4) the code does not split the node and propagate the median upward. Implement a split routine that creates a new node, moves the upper half keys/children to it, sets *p to the median key and *c to the new right node.
  • Display/indexing: the display loop should iterate keys 1..count (inclusive) and then recurse to child[count]. For example:
    for (i = 1; i <= root->count; ++i) {
    display(root->child[i-1]);
    printf("%d\t", root->value[i]);
    }
    display(root->child[root->count]);
  • Portability and safety: include <stdlib.h>, check malloc() return, remove or replace conio.h/getch() for portable builds.

Quick debugging checklist

  • Compile with full warnings (gcc: -Wall -Wextra) and fix all warnings.
  • Add short printf() traces in put()/insert() to see control flow.
  • Test with a few inserts (1, 4, 9 ...) and validate tree shape before trying larger sequences that require splits.

As suggested, rethink the insertion flow; as noted, there are concrete code errors to fix. Fix the root assignment first to get visible output, then implement proper split/propagation and boundary-safe indexing.

The problem is in insertion you need to think about it again

the problem is in your code.

commented: Brilliant deduction. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.