Welcome
Binary Search treeInsertion, Search and Deletion operation and BST sort
Team Members
Israt Jahan Fateha (151-15-5036)
Md. Eyakub Sorkar (151-15-4674)
Mehedi Imam Shafi (151-15-5248)
Sajiya (151-15-5256)
Sanjida Tasnim (143-15-4397)
B S T
inary
earch ree
Condition for a binary tree to be a BST
Left child is smaller than parent
Right child is greater than parent
55
45 65
58 675335
Example of binary search tree
Basic Operations
• Insertion
• Search
• Deletion
• Traversal
• Sort
Insertion
 There are three rules before we go to the binary insertion search. The first rule is , A parent node
should not have more than two children. If it does, it is not binary search tree. The second rule is,
the value of parent is always greater than the right child. And the third rule is, the value of parent
is always smaller than the left child.
40
8010
Binary search tree insertion
Key is 6
inserted
10
-5 16
-8 7 18
6
Binary search tree insertion
Key is 17
inserted
10
-5 16
-8 7 18
6 17
Binary search tree insertion
Key is 19
inserted
10
15
16
18
19
Time complexity
How much time doe it take to insertion into binary search tree. The worst case, the time it takes
to insert into binary search tree is O(n). Its possible all the nodes have just one child and they are
linear like this one and the worst case here will be O(n).
10
15
16
18
19
Time complexity
If the tree is a balanced tree, the worst case to insert into such kind of tree is O(log(n)).
10
-5 16
-8 7 18
6 17
Search
What will we search?
How to search?
65
9
-4 5
4
5 is less than 6
go to the left child
5 is more than 4
go to the right child
Searched value has
been found
Algorithm:
Here k is the key that is searched for and x is the start
node.
BST-Search(x, k)
1. y ← x
2. while y ≠ nil do
3. if key[y] = k then return y
4. else if key[y] < k then y ← right[y]
5. else y ← left[y]
6. return (“NOT FOUND”)
CODE in C++:
bool BinarySearchTree::search( int value) {
if (root == NULL)
return false;
else
return root->search(value);
}
bool BSTNode::search( int value) {
if (value == this->value)
return true;
else if (value < this->value) {
if (left == NULL)
return false;
else
return left->search(value);
} else if (value > this->value) {
if (right == NULL)
return false;
else
return right->search(value);
}
return false;
}
Time Complexity of Searching in
Binary Search Tree
O( log(n) )O(n)
Deletion
Deleting a node has no child:
5
182
-4 3
5
18
2
3
Deleting a node with one child:
5
-4
2
25
18
21
19
3
5
-4
2
19
21
253
Deleting a node with two children:
5
9
12
19
213-4
2
21
5
9
19
213-4
2
2519
Sort
Already sorted during the step of insertion
TraversalPre-order In-order Post-order
In-order
55
45 65
58 675335
In-order traversal: 35 45 53 55 58 65 67
Pre-Order
55
45 65
58 675335
Pre-order traversal: 55 45 35 53 65 58 67
Post-Order
55
45 65
58 675335
Post-order: 35 53 45 58 67 65 55
Real Life Use of Binary Search Tree
10,000,0007log(10000000)
Maintaining a huge library
Game Objects
References:
1. http://en.wikipedia.org/binary=search-tree/
2. http://geeksourceforge.com/bst-basic
3. http://github.com/cpp@master/binary-tree/bst.cpp
4. https://www.quora.com/What-are-the-real-world-examples-of-binary-trees-not-search-tree
5. http://www.geeksforgeeks.org/618/
Thank You all
For being with us.

Binary Search Tree