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