Skip to content

Commit

Permalink
bst.c : Binary Search Tree Added [C]
Browse files Browse the repository at this point in the history
  • Loading branch information
Om-Pandey committed Oct 2, 2018
1 parent 40893cf commit 9b1409f
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 10 deletions.
246 changes: 246 additions & 0 deletions BinarySearchTree/bst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
/*
* 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);
}
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,30 @@ This repository contains examples of various algorithms written on different pro

| Algorithm | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | | | | [:octocat:](euclidean_gcd/Python) |
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | | [:octocat:](euclidean_gcd/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | | | | [:octocat:](merge_sort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | | [:octocat:](bubble_sort/Cpp) | | |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | | | [:octocat:](shell_sort/Python) |


## Implemented Data Structures

| Data Structure | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | [:octocat:](queue/Cpp) | | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | | | [:octocat:](stack/Java) | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | | | |
<<<<<<< bst
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | | | [:octocat:](avl_tree/Java) | |
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | [:octocat:](bst/C) | | | |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | | [:octocat:](avl_tree/Java) | |

>>>>>>> master
## Sample Run

Expand All @@ -45,16 +50,17 @@ This repository contains examples of various algorithms written on different pro
## Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`.

2. Clone the forked repository to local system.
3. Create your feature branch: `git checkout -b my-new-feature`
4. Commit your changes: `git commit -am 'Add some feature'`.

Go through [Commit Messages guidelines](CONTRIBUTING.md#write-good-commit-messages)
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :smile:
5. Push to the branch: `git push origin my-new-feature`
6. Submit a pull request :smile:

See [CONTRIBUTING.md](CONTRIBUTING.md).

## Notes

Only project maintainers should merge a PR.
Other members can add their reviews to a PR but the merging should be done by only a project maintainer.
Other members can add their reviews to a PR but the merging should be done by only a project maintainer.

1 comment on commit 9b1409f

@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 9b1409f.

There are 78 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
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 6
Missing space after , [whitespace/comma] [3] BinarySearchTree/bst.c 19
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 24
Missing space before ( in while( [whitespace/parens] [5] BinarySearchTree/bst.c 33
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 34
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 38
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 61
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 63
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 69
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 71
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 72
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 80
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 86
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/bst.c 92
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 92
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 94
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 95
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 99
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 102
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 107
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 109
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 121
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 123
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/bst.c 133
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 133
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 135
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 137
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 143
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 149
Missing spaces around > [whitespace/operators] [3] BinarySearchTree/bst.c 150
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 150
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 154
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 154
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 154
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 158
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 158
Missing spaces around == [whitespace/operators] [3] BinarySearchTree/bst.c 158
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 158
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/bst.c 162
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 162
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 165
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 168
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 169
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 172
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 173
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 181
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 181
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 181
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 182
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 186
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 186
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 186
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 189
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 189
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 189
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 197
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 197
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 197
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 198
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 202
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 211
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 211
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 211
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 213
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 217
If an else has a brace on one side, it should have it on both [readability/braces] [5] BinarySearchTree/bst.c 217
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 217
Missing spaces around = [whitespace/operators] [4] BinarySearchTree/bst.c 218
Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3] BinarySearchTree/bst.c 224
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] BinarySearchTree/bst.c 224
Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3] BinarySearchTree/bst.c 224
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 228
{ should almost always be at the end of the previous line [whitespace/braces] [4] BinarySearchTree/bst.c 231
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 235
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 239
Missing space before { [whitespace/braces] [5] BinarySearchTree/bst.c 240
An else should appear on the same line as the preceding } [whitespace/newline] [4] BinarySearchTree/bst.c 244
Could not find a newline character at the end of the file. [whitespace/ending_newline] [5] BinarySearchTree/bst.c 246

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

Please sign in to comment.