Skip to content

Commit 5a9bd2a

Browse files
committed
BST added
1 parent c386130 commit 5a9bd2a

13 files changed

+236
-0
lines changed

Data Structure/BST/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Binary Search Tree (BST)
2+
3+
A **Binary Search Tree (BST)** is a tree in which all the nodes follow the below-mentioned properties −
4+
5+
- The left subtree of a node contains only nodes with keys less than the node’s key.
6+
- The right subtree of a node contains only nodes with keys greater than the node’s key.
7+
- The left and right subtree each must also be a binary search tree.
8+
9+
BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has a key and an associated value. While searching, the desired key is compared to the keys in BST and if found, the associated value is retrieved.
10+
11+
![BST](binary_search_tree.jpg)
12+
13+
**Basic Operations:**
14+
15+
Following are the basic operations of a tree −
16+
17+
- **Search** − Searches an element in a tree.
18+
- **Insert** − Inserts an element in a tree.
19+
- **Pre-order Traversal** − Traverses a tree in a pre-order manner.
20+
- **In-order Traversal** − Traverses a tree in an in-order manner.
21+
- **Post-order Traversal** − Traverses a tree in a post-order manner.
22+
23+
##### Search Operation
24+
25+
To search a given key in Binary Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for left subtree.
26+
27+
![BST Search](bst_search.png)
28+
29+
##### Insertion
30+
31+
Whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left subtree and insert the data. Otherwise, search for the empty location in the right subtree and insert the data.
32+
33+
![BST Insert](bst_insert.png)
34+
35+
##### Deletion
36+
37+
When we delete a node, there possibilities arise.
38+
- **Node to be deleted is leaf:** Simply remove from the tree.
39+
40+
![BST Remove-1](bst-remove-case-1.png)
41+
42+
- **Node to be deleted has only one child:** Copy the child to the node and delete the child.
43+
44+
![BST Remove-2-1](bst-remove-case-2-1.png)
45+
![BST Remove-2-2](bst-remove-case-2-2.png)
46+
![BST Remove-2-3](bst-remove-case-2-3.png)
47+
48+
- **Node to be deleted has two children:** Find inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor.
49+
50+
![BST Remove-3-3](bst-remove-case-3-3.png)
51+
![BST Remove-3-4](bst-remove-case-3-4.png)
52+
![BST Remove-3-5](bst-remove-case-3-5.png)
53+
![BST Remove-3-6](bst-remove-case-3-6.png)
54+
55+
#### Complexity Analysis
56+
- Insertion - O(log n)
57+
- Deletion - O(log n)
58+
- Access - O(log n)
59+
- Search - O(log n)
60+
61+
### More on this topic
62+
- https://www.tutorialspoint.com/data_structures_algorithms/binary_search_tree.htm
63+
- http://www.algolist.net/Data_structures/Binary_search_tree
64+
- http://quiz.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/
65+
- http://quiz.geeksforgeeks.org/binary-search-tree-set-2-delete/
66+
- https://en.wikibooks.org/wiki/Data_Structures/Trees#Binary_Search_Trees
10.6 KB
Loading
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
typedef struct
5+
{
6+
int number;
7+
struct node *leftChild;
8+
struct node *rightChild;
9+
10+
} node;
11+
12+
node* root = NULL;
13+
14+
void insertNode(int value)
15+
{
16+
node* tempNode;
17+
node* currentNode = NULL;
18+
19+
tempNode = (node *) malloc(sizeof(node));
20+
tempNode->number = value;
21+
tempNode->leftChild = NULL;
22+
tempNode->rightChild = NULL;
23+
24+
25+
if(root == NULL) //For the very first call
26+
{
27+
root = tempNode;
28+
}
29+
else
30+
{
31+
currentNode = root;
32+
33+
while(1)
34+
{
35+
if(value <= currentNode->number)
36+
{
37+
if(currentNode->leftChild == NULL)
38+
{
39+
currentNode->leftChild = tempNode;
40+
break;
41+
}
42+
43+
currentNode = currentNode->leftChild;
44+
}
45+
else
46+
{
47+
if(currentNode->rightChild == NULL)
48+
{
49+
currentNode->rightChild = tempNode;
50+
break;
51+
}
52+
53+
currentNode = currentNode->rightChild;
54+
}
55+
}
56+
}
57+
}
58+
59+
void inOrderPrint(node *rootNode) // InOrder Traversal (left-root-right)
60+
{
61+
if(rootNode==NULL)
62+
return;
63+
64+
inOrderPrint(rootNode->leftChild);
65+
66+
printf("%d ", rootNode->number);
67+
68+
inOrderPrint(rootNode->rightChild);
69+
}
70+
71+
node* searchOnTree(int value)
72+
{
73+
node* currentNode = root;
74+
75+
while(currentNode != NULL)
76+
{
77+
if(value == currentNode->number)
78+
{
79+
break;
80+
}
81+
else if(value <= currentNode->number)
82+
currentNode = currentNode->leftChild;
83+
else
84+
currentNode = currentNode->rightChild;
85+
}
86+
87+
return currentNode;
88+
}
89+
90+
node * findMinimum(node *currentNode)
91+
{
92+
if(currentNode->leftChild==NULL)
93+
return currentNode;
94+
95+
return findMinimum(currentNode->leftChild);
96+
}
97+
98+
node * deleteNode(node *currentNode, int value)
99+
{
100+
if(currentNode==NULL) // empty tree
101+
return NULL;
102+
else if(value < currentNode->number) // value is less than node's number. so go to left subtree
103+
currentNode->leftChild = deleteNode(currentNode->leftChild, value);
104+
else if(value > currentNode->number) // value is greater than node's number. so go to right subtree
105+
currentNode->rightChild = deleteNode(currentNode->rightChild, value);
106+
else // node found. Let's delete it!
107+
{
108+
//node has no child
109+
if(currentNode->leftChild == NULL && currentNode->rightChild == NULL)
110+
{
111+
currentNode = NULL;
112+
}
113+
else if(currentNode->leftChild == NULL) // node has only right child
114+
{
115+
currentNode = currentNode->rightChild;
116+
}
117+
else if(currentNode->rightChild == NULL) // node has only left child
118+
{
119+
currentNode = currentNode->leftChild;
120+
}
121+
else // node has two children
122+
{
123+
node *tempNode = findMinimum(currentNode->rightChild);
124+
currentNode->number = tempNode->number;
125+
currentNode->rightChild = deleteNode(currentNode->rightChild, tempNode->number);
126+
}
127+
128+
}
129+
130+
return currentNode;
131+
}
132+
133+
134+
int main()
135+
{
136+
int i, value;
137+
//int numbers[] = {45, 54, 40, 49, 38, 70, 30, 39, 41, 45, 44};
138+
int numbers[] = {5, 2, 17, 6, 20};
139+
int numbers_length = sizeof(numbers)/sizeof(numbers[0]);
140+
node* node_to_delete;
141+
142+
for(i=0;i<numbers_length;i++)
143+
{
144+
insertNode(numbers[i]);
145+
}
146+
147+
printf("\nIn-Order Tree printing:\n");
148+
inOrderPrint(root);
149+
puts("");
150+
151+
value = 70;
152+
if(searchOnTree(value) != NULL)
153+
printf("\n%d is found on Tree.\n", value);
154+
else
155+
printf("\n%d is not found on Tree.\n", value);
156+
157+
value = 100;
158+
if(searchOnTree(value) != NULL)
159+
printf("\n%d is found on Tree.\n", value);
160+
else
161+
printf("\n%d is not found on Tree.\n", value);
162+
163+
deleteNode(root, 5);
164+
165+
printf("\nIn-Order Tree printing:\n");
166+
inOrderPrint(root);
167+
puts("");
168+
169+
return 0;
170+
}
11.4 KB
Loading
10.2 KB
Loading
11.9 KB
Loading
7.17 KB
Loading
12.3 KB
Loading
12.9 KB
Loading
12.6 KB
Loading

0 commit comments

Comments
 (0)