[go: up one dir, main page]

0% found this document useful (0 votes)
18 views1 page

BST Program 10

The document discusses a circular queue data structure and binary search tree (BST) implementation in C. It includes functions to insert, delete, display items in the circular queue as well as functions to create, insert, search, and traverse nodes in a BST.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

BST Program 10

The document discusses a circular queue data structure and binary search tree (BST) implementation in C. It includes functions to insert, delete, display items in the circular queue as well as functions to create, insert, search, and traverse nodes in a BST.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.h> CIRCULAR QUEUE return (item); } } #include<stdio.

h> BST PROGRAM 10


#define SIZE 5 void display() { #include<stdlib.h>
int CQ[SIZE]; int i; struct BST {
int front = -1, rear = -1; if (isEmpty()) int data;
int item; printf(" \n Empty Queue\n"); struct BST *lchild;
int isFull() { else { struct BST *rchild; };
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) printf("\n Front ->[%d] ", front); typedef struct BST * NODE;
return 1; printf("\n Items -> "); NODE create() {
return 0; } for (i = front; i != rear; i = (i + 1) % SIZE) { NODE temp;
int isEmpty() { printf("%d ", CQ[i]); } temp = (NODE) malloc(sizeof(struct BST));
if (front == -1) return 1; printf("%d ", CQ[i]); printf("\nEnter The value: ");
return 0; } printf("\n Rear -> [%d] \n", rear); } } scanf("%d", &temp->data);
void insert() int main() { temp->lchild = NULL;
{ if (isFull()) int ch; temp->rchild = NULL;
printf("\n Queue is full!! \n"); else { while (1) { return temp; }
if (front == -1) front = 0; printf("\n\n 1.INSERT Operation\n"); void insert(NODE root, NODE newnode);
printf("\n Enter Inserted item:" ); printf("2.Delete Operation\n"); void inorder(NODE root);
scanf("%d", &item); printf("3.Display the Queue\n"); void preorder(NODE root);
rear = (rear + 1) % SIZE; printf("4.Exit\n"); void postorder(NODE root);
CQ[rear] = item; } } printf("Enter your choice of operations : "); void search(NODE root);
int deQueue() { scanf("%d", &ch); void insert(NODE root, NODE newnode) {
int item; switch (ch) { if (newnode->data < root->data) {
if (isEmpty()) { case 1: insert(); if (root->lchild == NULL)
printf("\n Queue is empty !! \n"); break; root->lchild = newnode; else
return (-1); } else { case 2: deQueue(); insert(root->lchild, newnode); }
item = CQ[front]; break; if (newnode->data > root->data) { if (root->rchild == NULL)
if (front == rear) { case 3: display(); root->rchild = newnode; else insert(root->rchild, newnode); } }
front = -1; break; void search(NODE root) {
rear = -1; } else { case 4: exit(0); int key;
front = (front + 1) % SIZE; } default: NODE cur;
printf("\n Deleted element -> %d \n", item); printf("Incorrect choice \n"); } }} if(root == NULL) { printf("\nBST is empty."); return; }

BST PROGRAM 10 #include<stdio.h> GRAPH 11 PROGRAM

void main() { #include<stdlib.h>

printf("\nBST is empty."); return; } int ch, key, val, i, n; int a[50][50], n, visited[50];

printf("\nEnter Element to be searched: "); NODE root = NULL, newnode; int q[20], front = -1,rear = -1;

scanf("%d", &key); while(1){ printf("\n~~~~BST MENU~~~~"); int s[20], top = -1, count=0;

cur = root; printf("\n1.Create a BST"); printf("\n2.Search"); void bfs(int v) {

while (cur != NULL) { if (cur->data == key){ printf("\n3.BST Traversals: ");printf("\n4.Exit"); int i, cur;

printf("\nKey element is present in BST"); return; } printf("\nEnter your choice: ");scanf("%d", &ch); visited[v] = 1;

if (key < cur->data) cur = cur->lchild; switch(ch){ q[++rear] = v;

else cur = cur->rchild; } case 1: printf("\nEnter the number of elements: "); while(front!=rear) {

printf("\nKey element is not found in the BST"); } scanf("%d", &n); for(i=1;i<=n;i++) { cur = q[++front];

if (cur->data == key) { newnode = create(); if (root == NULL) for(i=1;i<=n;i++) {

printf("\nKey element is present in BST"); return; } root = newnode; else if((a[cur][i]==1)&&(visited[i]==0)) {

if (key < cur->data) insert(root, newnode); break; q[++rear] = i;

cur = cur->lchild; else case 2: if (root == NULL) visited[i] = 1; printf("%d ", i); {{{{

cur = cur->rchild; } printf("\nTree Is Not Created"); else { void dfs(int v) { int i;

printf("\nKey element is not found in the BST"); } printf("\nThe Preorder display : "); visited[v]=1;

void inorder(NODE root) { if(root != NULL) { preorder(root); s[++top] = v;

inorder(root->lchild); printf("%d ", root->data); printf("\nThe Inorder display : "); for(i=1;i<=n;i++) {

inorder(root->rchild); } } inorder(root); if(a[v][i] == 1&& visited[i] == 0 ) {

void preorder(NODE root) { printf("\nThe Postorder display : "); printf("%d ", i);

if (root != NULL){ postorder(root); } break; dfs(i);}}}

printf("%d ", root->data); case 3: search(root); int main() {

preorder(root->lchild); break; int ch, start, i,j;

preorder(root->rchild);}} printf("\nEnter the number of vertices in graph: ");

void postorder(NODE root) { case 4: exit(0); scanf("%d",&n);

if (root != NULL){ } printf("\nEnter the adjacency matrix:\n");

postorder(root->lchild); } for(i=1; i<=n; i++) {

postorder(root->rchild); } for(j=1;j<=n;j++)

printf("%d ", root->data) } } scanf("%d",&a[i][j]); } for(i=1;i<=n;i++)

visited[i]=0;

printf("\nEnter the starting vertex: "); #include <stdio.h> HARSH TABLE 12 printf("Collision avoided successfully using LINEAR PROBING\n");

scanf("%d",&start); #include <stdlib.h> if(count == MAX) {

printf("\n==>1. BFS: Print all nodes reachable from a given starting node"); #include <conio.h> printf("\n Hash table is full");

printf("\n==>2. DFS: Print all nodes reachable from a given starting node"); #define MAX 20 display(a);

printf("\n==>3:Exit"); printf("\nEnter your choice: "); int create(int); exit(1); }

scanf("%d", &ch);switch(ch) { void linear_prob(int[], int, int); for(i=key+1; i<MAX; i++)

case 1: printf("\nNodes reachable from starting vertex %d are: ", start); void display (int[]); if(a[i] == -1) { a[i] = num;

bfs(start); void main() { flag =1; break; } i=0;

for(i=1;i<=n;i++) { int a[MAX],num,key,i; while((i<key) && (flag==0)) {

if(visited[i]==0) int ans=1; if(a[i] == -1) {

printf("\nThe vertex that is not reachable is %d" ,i); } break; display(a); a[i] = num;

case 2: printf("\nNodes reachable from starting vertex %d are:\n",start); printf(" collision handling by linear probing : \n"); flag=1;

dfs(start);break; for (i=0;i<MAX;i++) { a[i]=-1; } do { break; }

case 3: exit(0); printf("\n Enter 4 digit employee key\n"); scanf("%4d", &num); i++; } } }

default: printf("\nPlease enter valid choice:");}} key=create(num); void display(int a[MAX]) {

linear_prob(a,key,num); int i,choice;

printf("\n Do you wish to continue ? (1/0) "); while(1) {

scanf("%d",&ans); }while(ans); } printf("\nMENU\n1.Display ALL\n2.Filtered Display\n3.Exit\n");

int create(int num) { int key; scanf("%d",&choice); switch(choice) {

key=num%10; case 1:printf("\nThe hash table is\n");

return key; } for(i=0; i<MAX; i++)

void linear_prob(int a[MAX], int key, int num) { printf("\n %d %d ", i, a[i]);

int flag, i, count=0; flag=0; break;

if(a[key]== -1) case 2: printf("\nThe hash table is\n");

a[key] = num; for(i=0; i<MAX; i++)

else { if(a[i]!=-1) {

printf("\nCollision Detected...!!!\n"); printf("\n %d %d ", i, a[i]); continue; }

i=0; break;

while(i<MAX) { case 3: exit(0); }

if (a[i]!=-1) }}

count++; i++; }

You might also like