For the complexity, n
is the number of items in the LinkedList.
-
Time complexity:
- Best case:
- Insert:
O(1)
, we always insert a new item in front of the LinkedList. - Delete:
O(1)
, the item to delete is the first element of the LinkedList.
- Insert:
- Worst case:
- Insert:
O(1)
, we always insert a new item in front of the LinkedList. - Delete:
O(n)
, the item to delete is the last element of the LinkedList.
- Insert:
- Best case:
-
Space Complexity:
O(n)
For the complexity, n
is the number of items in the Stack. Also, this implementation use the LinkedList to store items.
-
Time complexity:
- Best case:
- Push:
O(1)
, we always insert a new item in front of the LinkedList. - Pop:
O(1)
, the item to delete is the first element of the LinkedList.
- Push:
- Worst case:
- Push:
O(1)
, we always insert a new item in front of the LinkedList. - Pop:
O(1)
, the item to delete is always the first element of the LinkedList.
- Push:
- Best case:
-
Space Complexity:
O(n)
For the complexity, n
is the number of items in the Queue. Also, this implementation use the LinkedList to store items. This LinkedList always insert an element in the end of the list. To do this operation in O(1)
, the list keep a pointer to the last element of the list.
- Time complexity:
- Best case:
- Enqueue:
O(1)
, we always insert a new item after the last element of the LinkedList that has a pointer to it last element. - Dequeue:
O(1)
, the item to delete is the first element of the LinkedList.
- Enqueue:
- Worst case:
- Enqueue:
O(1)
, we always insert a new item after the last element of the LinkedList that has a pointer to it last element. - Dequeue:
O(1)
, the item to delete is the first element of the LinkedList.
- Enqueue:
- Best case:
For the complexity, n
is the number of items in the Heap.
- Time complexity:
- Best case:
- max_heapify:
O(1)
, here is when we need to look only in a level belowi
. - build_max_heap:
O(n)
, for each element from positionn // 2 - 1
to0
, we need to callmax_heapify
.
- max_heapify:
- Worst case:
- max_heapify:
O(lgn)
, we need to look all levels from the root until leaves. - build_max_heap:
O(n)
, for each element from positionn // 2 - 1
to0
, we need to callmax_heapify
.
- max_heapify:
- Best case:
- Space Complexity:
O(n)
,build_max_heap
create a max heap in place.
For the complexity, n
is the number of items in the Heap.
- Time complexity:
- Best case:
- min_heapify:
O(1)
, here is when we need to look only in a level belowi
. - build_min_heap:
O(n)
, for each element from positionn // 2 - 1
to0
, we need to callmin_heapify
.
- min_heapify:
- Worst case:
- min_heapify:
O(lgn)
, we need to look all levels from the root until leaves. - build_min_heap:
O(n)
, for each element from positionn // 2 - 1
to0
, we need to callmin_heapify
.
- min_heapify:
- Best case:
- Space Complexity:
O(n)
,build_min_heap
create a min heap in place.
For the complexity, n
is the number of items in the Binary search tree and h
is the tree's height.
-
Time complexity: In the worst case,
h = n
, the binary search tree is like a linked list.- search:
O(h)
, we do a binary search to find an element. - insert:
O(h)
, Either we need to find the element in the tree or we need to find the correct place for the new element. Both start in the tree's root and do a binary search. - delete:
O(h)
, We need to find the element in the tree and delete it. To find the element, we use thesearch
method.
- search:
-
Space Complexity:
O(n)
Here, n
is the number of items to be sorted. Also, the videos below represent each algorithm in execution which red bars represent comparations.
- Time complexity:
- Best case and Worst case:
O(n^2)
, we select ani th
item and compare it with allj th
items, wherei < j < n
- Best case and Worst case:
- Space Complexity:
O(n)
- Time complexity:
- Best case:
- Worst case:
- Space Complexity:
O(n)
- Time complexity:
- Space Complexity:
O(n)
Here, n
is the number of nodes and h
is the height of the Tree.
- Time complexity:
O(n)
, we always need to visit each node of the tree. - Space complexity:
O(h)
, we are using a recursive algorithm andh
is the height of the tree recursive call.
- Time complexity:
O(n)
, we always need to visit each node of the tree. - Space complexity:
O(h)
, we are using a recursive algorithm andh
is the height of the tree recursive call.
- Time complexity:
O(n)
, we always need to visit each node of the tree. - Space complexity:
O(h)
, we are using a recursive algorithm andh
is the height of the tree recursive call.