2

I am new in C programming and I am learning data structures in it. I recently bumped into level order traversal of Binary Search Tree and I wrote a code and i can't figure it out why my code is not working.

Please feel free for any suggestions.The code that I wrote doesn't print any thing after levelOrder() is called int the main.I expect to print tree but it doesn't prints any thing and the process ends.I tried making queue with array and putting address of my tree nodes in an pointer to struct node array.My code is:-

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

struct node{
    int data;
    struct node *left;
    struct node *right;
};

struct node *Insert(struct node *a,int b);
int Search(struct node *d,int s);
int findmax(struct node *c);
int findmin(struct node *b);
struct node *getnode(int c);
int recfindmin(struct node *j); //recursive method(rec).
int recfindmax(struct node *h);
int maxv(int a,int b);
int findheight(struct node *g);
void levelOrder(struct node *t);

int main(){
    struct node *root;
    root=NULL;
    int front=-1 ;
    int rear=-1;
    int v;
    root=Insert(root,66);
    root=Insert(root,22);
    root=Insert(root,18);
    root=Insert(root,65);
    root=Insert(root,60);
    root=Insert(root,2);
    root=Insert(root,13);
    root=Insert(root,65);
    printf("\n max value is %d rec %d\n",findmax(root),recfindmax(root));
    printf("\n min value is %d rec %d\n",findmin(root),recfindmin(root));
    printf("enter the no to be searched");
    scanf("%d",&v);
    if(Search(root,v)){
        printf("found");
    }else {
        printf("not found");
    }
    
    printf("\nheight is %d",findheight(root));
    printf("\n");
    levelOrder(root);
    
}

struct node *getnode(int c){
    struct node *new=(struct node*)malloc(sizeof(struct node));
    new->data=c;
    new->left=new->right=NULL;
    return new;
}

int Search(struct node *d,int s){
    if(d==NULL){
        return 0;
    }
    else if(d->data==s){
        return 1;
    }
    else if(s<=d->data){
        return Search(d->left,s);
    }
    else{
    return Search(d->right,s);
    }
    
}

struct node *Insert(struct node *a,int b){
    if (a==NULL){
        a=getnode(b);
    }
    else if(b<=a->data){
         a->left=Insert(a->left,b);
    }
    else{
        a->right=Insert(a->right,b);
    }
    return a;
}

int findmin(struct node *b){
    if(b==NULL){
        printf("tree is empty");
        return -1;
    }
    while(b->left!=NULL){
        b=b->left;
    }
    return b->data;
}

int findmax(struct node *c){
    if(c==NULL){
        printf("tree is empty");
        return -1;
    }
    while(c->right!=NULL){
        c=c->right;
    }
    return c->data;
}

int recfindmin(struct node *j){
    if(j==NULL){
        printf("tree is empty");
        return -1;
    }
    else if(j->left==NULL){
        return j->data;
    }
    return recfindmin(j->left);
}

int recfindmax(struct node *h){
    if(h==NULL){
        printf("tree is empty");
        return -1;
    }
    else if(h->right==NULL){
        return h->data;
    }
    return recfindmin(h->right);
}

int findheight(struct node *g){
    if(g==NULL){
        return -1;
    }
    return maxv(findheight(g->left),findheight(g->right))+1;

}

int maxv(int a,int b){
    if(b>a){
        return b;
    }else if(a>b)
    {
        return a;
    }
    else if(a==b){
        return a;
    }
}

void levelOrder(struct node *t){
    if(t==NULL){
        printf("empty");
        return;
    }
    enqueue(t);
    while(!isempty()){
        struct node *current=dequeue();
        printf(" %d ",current->data);
        if(current->left!=NULL){
            enqueue(current->left);
        }
        if(current->right!=NULL){
            enqueue(current->right);
        }
    }
    printf("hey");

}

and in my header file I have this

#ifndef queryeu
#define queryeu

#define max 40

struct node *A[max];
int rear;
int front;



int isempty(){
    return (front==-1 && rear==-1)?1:0;
    
}

int isfull(){
    return ((rear+1)%max==front)?1:0;
}

void enqueue(struct node *a){
    if(isfull()){
        printf("queue full");
            return;
    }
    if(isempty()){
        front=rear=0;
    }
    else{
        rear=(rear+1)%max;
    }
    A[rear]=a;
}

struct node *dequeue(){
    struct node *b;
    b=A[front];
    if(isempty()){
        printf("the queue is empty");
    }
    else if(front==rear){
        front=rear=-1;
    }else {
        front=(front+1) %max;
    }
    return b;
}

struct node *fron(){
    return A[front];
}

#endif
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    can you also add what the o/p of ur run is – Vishal T Aug 05 '20 at 05:30
  • 1
    Did you try running your code line by line in a debugger while monitoring the values of all variables? If not, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Aug 05 '20 at 05:31
  • 1
    If using a debugger does not actually solve the problem, then it will at least allow you to isolate the problem further and to create a [mre], so that it will be easier for other people to help you. Most people will not be prepared to debug your whole program for you, as that should be your job. – Andreas Wenzel Aug 05 '20 at 05:40
  • 2
    The statements `int front=-1 ;` and `int rear=-1;` do not do anything useful. They define local variables and do not affect the `int rear;` and `int front;` defined in `Queue_fortree.h`. Remove them from `main`. In `Queue_fortree.h`, change the definitions to `int front=-1;` and `int rear=-1;`. (Once your program is debugged, redesign this. It is bad form to define objects or functions in header files. Header files should generally be used only for declaring things—making names known to the compiler. Definitions should be put in source files.) – Eric Postpischil Aug 05 '20 at 10:30
  • And if i put all the definition and functions in the source file my source file will get messy and not that neat and clean.Aren't we suppose to put functions and defination in header files? can you direct me to some tutorials in maintaining and using header files? – Coder Manish Aug 05 '20 at 13:30
  • 1
    @CoderManish: The main point of a header file is to put declarations and definitions into it that are shared by several translation units (i.e. ".c" files). If you only have one translation unit, there is not much point in having a header file. See [this question](https://stackoverflow.com/questions/1945846/what-should-go-into-an-h-file) for further information (it is about C++, though, not C). – Andreas Wenzel Aug 05 '20 at 14:27
  • oh oh you mean that only declaration of functions and definitions of constants is to be put in a header file. ok but what about those function we call after using headers file like ctype.h in C programming.It has different functions like tolower(),isalpha(),toupper().etc ani in ctype,h header file these functions are only declared? then how can we use such function only after declaration ? function should have code. – Coder Manish Aug 06 '20 at 06:52
  • I mean what about the function implementation where is that done ? like in cytype.h has only declaration and prototypes of function tolower(),toupper(),isalpha(), etc .then where is the implementation of those function done? and what if not only declaration and definition but we also wanna share functions over multiple file then what to do, if not writing functions in the header file? like in the above code i posted if i wanted this queue header in other files to so I would need to write those functions in header file,wouldn't I? – Coder Manish Aug 06 '20 at 11:32
  • 1
    You are right that generally, all functions that are declared must also be defined somewhere. However, these definitions don't always have to be in your source code. They can also be defined in an external library. All functions of the C standard library (for example functions defined in `ctype.h`, `stdio.h`, `stdlib.h`, etc. are part of such an external library. It is likely that your [linker](https://en.wikipedia.org/wiki/Linker_(computing)) is configured to, by default, link with this external library which contains all definitions of the C standard library. – Andreas Wenzel Aug 06 '20 at 11:44
  • 1
    If you want several translation units (i.e. `.c` source files) to be able to call the same function, then you must only share the function declaration, i.e. the prototype, in a header file. The definition should not go into the header file (except if the function is supposed to be [inline](https://en.wikipedia.org/wiki/Inline_function)). The function (unless it is inline) should only be defined in one `.c` file. The linker will take care of making the definition of the function available to all translation units. – Andreas Wenzel Aug 06 '20 at 11:52
  • However, the situation is different with `struct` definitions. Every translation unit needs access to the full definition, so the full definition of the `struct` must go into the header files. In that respect, `struct` definitions are very different from function definitions, because with functions, only the function declarations (i.e. the function prototypes) should go into the header file. – Andreas Wenzel Aug 06 '20 at 11:57
  • With variables, the situation is similar to functions. Variables must be declared in every translation unit that uses them (for example by including a common header file in which they are declared), but only defined in one file (so should not be defined in a header file). – Andreas Wenzel Aug 06 '20 at 12:12
  • oh! you mean that if I only put a declaration in a header file one.h and put the definition ,implementation of functions in two.c file which has this header file one.h included in it then in three.c if I include one.h then I don't need to write the implementation and definitions of those function prototypes declared in one.h in three.c ? The header file takes care of that? – Coder Manish Aug 06 '20 at 12:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219333/discussion-between-andreas-wenzel-and-coder-manish). – Andreas Wenzel Aug 06 '20 at 12:23
  • ok in chat room i sent some more qn – Coder Manish Aug 07 '20 at 10:56

0 Answers0