0% found this document useful (0 votes)
76 views6 pages

Typedef Struct Int Struct: # Include # Include # Include

This document discusses the implementation of a heap sort algorithm in C. It defines functions to manage a max heap, including heapify() to reorder elements and insert() to insert new elements. The main() function takes user input to build a max heap, repeatedly extracts the maximum element to sort the array. It outputs the sorted array. Key functions include manage() to insert elements into the heap, and heapsort() to reorder elements during extraction of maximum values.

Uploaded by

chindhuz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views6 pages

Typedef Struct Int Struct: # Include # Include # Include

This document discusses the implementation of a heap sort algorithm in C. It defines functions to manage a max heap, including heapify() to reorder elements and insert() to insert new elements. The main() function takes user input to build a max heap, repeatedly extracts the maximum element to sort the array. It outputs the sorted array. Key functions include manage() to insert elements into the heap, and heapsort() to reorder elements during extraction of maximum values.

Uploaded by

chindhuz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

# include <stdio.h> # include <conio.h> # include <stdlib.

h> typedef struct BST { int data; struct BST *lchild,*rchild; }node; void void void void node insert(node *,node *); inorder(node *); preorder(node *); postorder(node *); *search(node *,int,node **);

void main() { int choice; char ans='N'; int key; node *new_node,*root,*tmp,*parent; node *get_node(); root=NULL; clrscr(); printf("nProgram For Binary Search Tree "); do { printf("n1.Create"); printf("n2.Search"); printf("n3.Recursive Traversals"); printf("n4.Exit"); printf("nEnter your choice :"); scanf("%d",&choice); switch(choice) { case 1: do { new_node=get_node(); printf("nEnter The Element "); scanf("%d",&new_node->data); if(root==NULL) /* Tree is not Created */ root=new_node; else insert(root,new_node);

printf("nWant To enter More Elements?(y/n)"); ans=getch(); }while(ans=='y'); break; case 2: printf("nEnter Element to be searched :"); scanf("%d",&key); tmp = search(root,key,&parent); printf("nParent of node %d is %d", tmp->data,parent->data); break; case 3: if(root==NULL) printf("Tree Is Not Created"); else { printf("nThe Inorder display : "); inorder(root); printf("nThe Preorder display : "); preorder(root); printf("nThe Postorder display : "); postorder(root); } break; } }while(choice!=4); } /* Get new Node */ node *get_node() { node *temp; temp=(node *)malloc(sizeof(node)); temp->lchild=NULL; temp->rchild=NULL; return temp; } /* This function is for creating a binary search tree */ void insert(node *root,node *new_node) {

if(new_node->data < root->data) { if(root->lchild==NULL) root->lchild = new_node; else insert(root->lchild,new_node); } if(new_node->data > root->data) { if(root->rchild==NULL) root->rchild=new_node; else insert(root->rchild,new_node); } } /* This function is for searching the node from binary Search Tree */ node *search(node *root,int key,node **parent) { node *temp; temp=root; while(temp!=NULL) { if(temp->data==key) { printf("n The %d Element is Present",temp->data); return temp; } *parent=temp; if(temp->data>key) temp=temp->lchild; else temp=temp->rchild; } return NULL; } /* This function displays the tree in inorder fashion */ void inorder(node *temp) { if(temp!=NULL) { inorder(temp->lchild); printf("%d",temp->data); inorder(temp->rchild); }

} /* This function displays the tree in preorder fashion */ void preorder(node *temp) { if(temp!=NULL) { printf("%d",temp->data); preorder(temp->lchild); preorder(temp->rchild); } } /* This function displays the tree in postorder fashion */ void postorder(node *temp) { if(temp!=NULL) { postorder(temp->lchild); postorder(temp->rchild); printf("%d",temp->data); } }

[468x15] Output :
Program For Binary Search Tree 1.Create 2.Search 3.Recursive Traversals 4.Exit Enter your choice :1 Enter The Element 5 Do u Want To enter More Elements?(y/n) Enter The Element 12 Do u Want To enter More Elements?(y/n) Enter The Element 6 Do u Want To enter More Elements?(y/n) Enter The Element 3 Do u Want To enter More Elements?(y/n) 1.Create

2.Search 3.Recursive Traversals 4.Exit Enter your choice :3 The Inorder display The Preorder display The Postorder display 1.Create 2.Search 3.Recursive Traversals 4.Exit Enter your choice :2 Enter Element to be searched :3 The 20 Element is Present Parent of node 3 is 5 : : : 3 5 3 5 3 6 6 12 12 6 12 5

#include<stdio.h> #include<conio.h> void manage(int *, int); void heapsort(int *, int, int); int main() { int arr[20]; int i,j,size,tmp,k; printf("\n\t------- Heap sorting method -------\n\n"); printf("Enter the number of elements to sort : "); scanf("%d",&size); for(i=1; i<=size; i++) { printf("Enter %d element : ",i); scanf("%d",&arr[i]); manage(arr,i); } j=size; for(i=1; i<=j; i++) { tmp=arr[1]; arr[1]=arr[size]; arr[size]=tmp; size--; heapsort(arr,1,size); }

printf("\n\t------- Heap sorted elements -------\n\n"); size=j; for(i=1; i<=size; i++) printf("%d ",arr[i]); getch(); return 0; }

void manage(int *arr, int i) { int tmp; tmp=arr[i]; while((i>1)&&(arr[i/2]<tmp)) { arr[i]=arr[i/2]; i=i/2; } arr[i]=tmp; }

void heapsort(int *arr, int i, int size) { int tmp,j; tmp=arr[i]; j=i*2; while(j<=size) { if((j<size)&&(arr[j]<arr[j+1])) j++; if(arr[j]<arr[j/2]) break; arr[j/2]=arr[j]; j=j*2; } arr[j/2]=tmp; }

You might also like