Programs Tree, Searching, Sorting
Programs Tree, Searching, Sorting
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// Preorder traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// Postorder traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
// Create a new Node
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
int main() {
struct node* root = initTree(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
insertRight(root->left,6);
insertLeft(root->right, 8);
insertRight(root->right,10);
freeTree(root);
}
#define MAX 4
#define MIN 2
struct btreeNode {
int val[MAX + 1], count;
struct btreeNode *link[MAX + 1];
};
struct btreeNode *root;
/* creating new node */
struct btreeNode * createNode(int val, struct btreeNode *child) {
struct btreeNode *newNode;
newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));
newNode->val[1] = val;
newNode->count = 1;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
}
/* Places the value in appropriate position */
void addValToNode(int val, int pos, struct btreeNode *node, struct btreeNode *child) {
int j = node->count;
while (j > pos) {
node->val[j + 1] = node->val[j];
node->link[j + 1] = node->link[j];
j--;
}
node->val[j + 1] = val;
node->link[j + 1] = child;
node->count++;
}
/* split the node */
void splitNode (int val, int *pval, int pos, struct btreeNode *node, struct btreeNode *child, struct btreeNode
**newNode) {
int median, j;
int main() {
int val, ch;
while (1) {
printf("\n1. Insertion");
printf("\n2. Searching\n3. Traversal\n");
printf("4. Exit\nEnter your choice: ");
scanf("%d", &ch);
printf("\n");
switch (ch) {
case 1:
printf("\nEnter your input: ");
scanf("%d", &val);
insertion(val);
break;
case 2:
printf("\nEnter the element to search: ");
scanf("%d", &val);
searching(val, &ch, root);
break;
case 3:
printf("\n");
traversal(root);
break;
case 4:
exit(0);
default:
printf("\nU have entered wrong option!!\n");
break;
}
printf("\n");
}
}
if(!p->left){
hL = 0;
} else{
hL = p->left->height + 1;
}
if(!p->right){
hR = 0;
} else{
hR = p->right->height + 1;
}
bFactor = hL - hR;
int main(void){
Node *root = NULL;
struct tree
{
int val;
struct tree *right;
struct tree *left;
int thread;
};
struct tree *root = NULL;
struct tree* insert_node(struct tree *root, struct tree *ptr, struct tree *rt)
{
if(root == NULL)
{
root = ptr;
if(rt != NULL)
{
root->right = rt;
root->thread = 1;
}
}
else if(ptr->val < root->val)
root->left = insert_node(root->left, ptr, root);
else
if(root->thread == 1)
{
root->right = insert_node(NULL, ptr, rt);
root->thread=0;
}
else
root->right = insert_node(root->right, ptr, rt);
return root;
}
void main()
{
//struct tree *root=NULL;
create_threaded_tree();
printf(" \n The in–order traversal of the tree can be given as : ");
inorder(root);
getch();
}
void display()
{
int i;
if(n==0)
{
printf("Heap is empty\n");
return;
}
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
void insert(int num,int loc)
{
int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}
arr[loc]=arr[par];
loc=par;
}
arr[0]=num; /*assign num to the root node */
}
for(i=0;i<n;i++)
{
if(num==arr[i])
break;
}
if( num!=arr[i] )
{
printf("%d not found in heap\n",num);
return;
}
arr[i]=arr[n-1];
n=n-1;
par=(i-1)/2; /*find parent of node i */
if(arr[i] > arr[par])
{
insert( arr[i],i);
return;
}
left=2*i+1; /*left child of i*/
right=2*i+2; /* right child of i*/
while(right < n)
{
if( arr[i]>=arr[left] && arr[i]>=arr[right] )
return;
if( arr[right]<=arr[left] )
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}
if( left==n-1 && arr[i]<arr[left] ) /* right==n */
{ temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
}
}
main()
{
int choice,num;
n=0;/*Represents number of nodes in the heap*/
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num,n);
n=n+1;
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice\n");
}
}
}
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
int main()
{
int arr[100], i, n;
return 0;
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
while(i<j){
while(number[i]<=number[pivot] && i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
return 0;
}
#define size 10
void main()
{
int arr[size], i, n;
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);