Skip to content

Commit

Permalink
BinarySearchTree Added
Browse files Browse the repository at this point in the history
InsertionSortJava Added

InserstionSortjava

InserstionSortjava

InserstionSort
  • Loading branch information
Om-Pandey authored and gitmate-bot committed Oct 2, 2018
1 parent 43c82c4 commit cbdb087
Showing 1 changed file with 288 additions and 0 deletions.
288 changes: 288 additions & 0 deletions BinarySearchTree/C/bst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
/*
* C Program to Construct a Binary Search Tree and perform deletion, inorder traversal on it
*/
#include <stdio.h>
#include <stdlib.h>

struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);

int flag = 1;

void main()
{
int ch;

printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}

/* To insert a node in the tree */
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}

/* To create a node */
void create()
{
int data;

printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;
temp->l = temp->r = NULL;
}

/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t)
{
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}

/* recursive function to perform inorder traversal of tree */
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}

/* To check for the deleted node */
void delete()
{
int data;

if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}

/* To find the preorder traversal */
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}

/* To find the postorder traversal */
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}

/* Search for the appropriate position to insert the new node */
void search1(struct btnode *t, int data)
{
if ((data>t->value))
{
t1 = t;
search1(t->r, data);
}
else if ((data < t->value))
{
t1 = t;
search1(t->l, data);
}
else if ((data==t->value))
{
delete1(t);
}
}

/* To delete a node */
void delete1(struct btnode *t)
{
int k;

/* To delete leaf node */
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}

/* To delete node having one left hand child */
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;
}
else if (t1->l == t)
{
t1->l = t->l;

}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}

/* To delete node having right hand child */
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}

/* To delete node having two child */
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
}

}

/* To find the smallest element in the right sub tree */
int smallest(struct btnode *t)
{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value);
}

/* To find the largest element in the left sub tree */
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value);
}

1 comment on commit cbdb087

@sangamcse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on cbdb087.

There are 125 results for the section all.cpp. They have been shortened and will not be shown inline because they are more than 10.

Message File Line
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 6
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 8
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 13
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 13
Missing space after , [whitespace/comma] [3] BinarySearchTree/C/bst.c 22
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 25
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 25
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 27
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 27
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 29
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 31
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 31
Missing space before ( in while( [whitespace/parens] [5] BinarySearchTree/C/bst.c 39
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 40
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 44
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 45
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 48
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 51
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 54
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 57
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 60
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 62
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 64
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 68
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 68
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 71
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 73
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 75
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 76
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 78
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 78
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 81
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 83
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 83
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 90
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 90
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 93
Lines should be <= 80 characters long [whitespace/line_length] [2] BinarySearchTree/C/bst.c 94
Lines should be <= 80 characters long [whitespace/line_length] [2] BinarySearchTree/C/bst.c 98
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 103
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 103
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 106
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 108
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 112
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 115
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 118
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 118
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 121
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 123
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 123
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 125
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 135
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 135
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 138
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 140
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 145
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 147
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 150
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 150
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 153
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 155
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 159
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 161
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 165
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 165
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 168
Missing spaces around > [whitespace/operators] [3] BinarySearchTree/C/bst.c 169
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 170
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 174
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 175
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 179
Missing spaces around == [whitespace/operators] [3] BinarySearchTree/C/bst.c 179
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 180
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 184
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 184
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 187
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 189
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 189
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 192
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 194
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 197
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 198
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 205
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 205
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 207
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 208
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 210
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 214
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 215
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 217
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 217
Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3] BinarySearchTree/C/bst.c 217
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 219
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 220
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 227
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 227
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 229
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 230
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 232
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 236
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 244
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 244
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 246
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 246
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 247
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 250
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 254
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 255
Missing spaces around = [whitespace/operators] [4] BinarySearchTree/C/bst.c 256
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 262
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 262
Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3] BinarySearchTree/C/bst.c 262
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 264
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 264
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 267
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 270
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 274
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 274
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/C/bst.c 277
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 277
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 280
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/C/bst.c 282
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/C/bst.c 286
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/C/bst.c 286
Could not find a newline character at the end of the file. [whitespace/ending_newline] [5] BinarySearchTree/C/bst.c 288

Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.

Please sign in to comment.