Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct queue{ //structure for queue
- int rear;
- int front;
- int *array;
- int capacity;
- };
- struct Bstnode{
- int data; //structure for BST
- int *left;
- int *right;
- };
- struct Bstnode* Getnewnode(int data){
- struct Bstnode* root=(struct Bstnode*)malloc(sizeof(struct Bstnode));
- root->data=data;
- root->left=NULL;
- root->right=NULL;
- return root;
- }
- struct Bstnode* insert(struct Bstnode* root,int data)
- {
- if(root==NULL)
- root=Getnewnode(data);
- else if(data<=root->data)
- {
- root->left=insert(root->left,data);
- }
- else if(data>=root->data)
- {
- root->right=insert(root->right,data);
- }
- return root;
- }
- struct queue* createqueue(int capacity)
- {
- struct queue* nque=(struct queue*)malloc(sizeof(struct queue));
- return NULL;
- nque->capacity=capacity;
- nque->front=-1;
- nque->rear=-1;
- nque->array=(int*)malloc(capacity*sizeof(int));
- return nque;
- }
- int isempty(struct queue* nque1){
- return((nque1->front==-1)&&(nque1->rear==-1));
- }
- int isfull(struct queue* amp)
- {
- return (((amp->rear)+1)%(amp->capacity)==amp->front);
- }
- void enqueue(struct queue* amp,struct Bstnode* root){
- if(isfull(amp))
- return;
- else if(isempty(amp))
- {
- amp->rear=0;
- amp->front=0;
- amp->array[amp->rear]=root;
- }
- else
- {
- amp->rear=((amp->rear+1)%(amp->capacity));
- amp->array[amp->rear]=root;
- }
- }
- int dequeue(struct queue* amp){
- int value;
- if(isempty)
- return -1;
- else if(amp->front==amp->rear)
- {
- value=amp->front;
- amp->front=-1;
- amp->rear=-1;
- return amp->array[value];
- }
- else
- {
- value=amp->front;
- amp->front=(amp->front+1)%(amp->capacity);
- return amp->array[value];
- }
- }
- void leveltra(struct Bstnode* root,struct queue* amp){
- if(root==NULL)
- return;
- enqueue(amp,root);
- struct Bstnode *temp=dequeue(amp);
- while(temp!=NULL){
- printf("%d",temp->data);
- if(temp->left!=NULL)
- {
- enqueue(amp,temp->left);
- }
- if(temp->right!=NULL)
- {
- enqueue(amp,temp->right);
- }
- temp=dequeue(amp);
- }
- }
- int main(){
- struct Bstnode* root;
- struct queue* queue1=createqueue(10);
- root=insert(root,4);
- root=insert(root,3);
- root=insert(root,6);
- root=insert(root,7);
- root=insert(root,2);
- leveltra(root,queue1);
- }
- struct queue* createqueue(int capacity)
- {
- struct queue* nque=(struct queue*)malloc(sizeof(struct queue));
- return NULL;
- nque->capacity=capacity;
- nque->front=-1;
- nque->rear=-1;
- nque->array=(int*)malloc(capacity*sizeof(int));
- return nque;
- }
- struct queue* createqueue(int capacity)
- {
- struct queue* nque = malloc(sizeof(struct queue));//removed cast
- if(nque) //continue only of successful
- {
- nque->capacity=capacity;
- nque->front=-1;
- nque->rear=-1;
- nque->array = malloc(capacity*sizeof(int));//removed cast
- if(!nque->array)
- {
- free(nque);//prevent memory loss
- return NULL;
- }
- }
- return nque;
- }
Add Comment
Please, Sign In to add comment