Iterative insertion into a binary search tree
In the previous article Binary Search Tree , we discussed the recursive method to insert a node in BST. In this article, we will discuss the iterative method to insert a node in BST. It is better than the recursive method because the iterative insertion algorithm does not require additional space.
Binary Search Tree Iterative Insertion Algorithm
Assume root
that is the root node of BST and key
is the element we want to insert.
-
Create the node to be inserted -
toinsert
. -
Initialize two pointers, ,
curr
to point toroot
, and ,prev
to point to null. (curr
Traverse the tree,prev
keeping track of it). -
When
curr
!=NULL
, do the following.-
Updated
prev
tocurr
keep track of it. -
If
curr->data
>key
, setcurr
tocurr->left
, discarding the right subtree. -
If
curr->data
<key
, setcurr
tocurr->right
, discarding the left subtree.
-
Updated
-
If
prev
==NULL
, the tree is empty. Createroot
a node. -
Otherwise if
prev->data
>key
, thenprev
inserttoinsert
= toprev->left
the left oftoinsert
. -
Otherwise if
prev->data
<key
, thenprev
insert = on the righttoinsert
sideprev->right
oftoinsert
.
BST Iterative Insertion Diagram
-
First, we
root
initialize the BST by creating a node and insert it into it5
. -
3
is smaller than5
, so it is inserted5
to the left of . -
4
is smaller than5
, but3
larger than , so insert3
to the right of , but insert4
to the left of . -
2
is the smallest element in the current tree, so it is inserted at the leftmost position. -
1
is the smallest element in the current tree, so it is inserted at the leftmost position. -
6
is the largest element in the current tree, so it is inserted at the rightmost position.
This is how we insert elements inside a BST.
Iterative implementation of binary search tree insertion
#include <iostream>
using namespace std;
class Node {
public:
int key;
Node *left, *right;
};
Node *newNode(int item) {
Node *temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(Node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->key << " ";
inorder(root->right);
}
}
void insert(Node* &root, int key)
{
Node* toinsert = newNode(key);
Node* curr = root;
Node* prev = NULL;
while (curr != NULL) {
prev = curr;
if (key < curr->key)
curr = curr->left;
else
curr = curr->right;
}
if (prev == NULL) {
prev = toinsert;
root = prev;
}
else if (key < prev->key)
prev->left = toinsert;
else
prev->right = toinsert;
}
int main() {
Node *root = NULL;
insert(root, 5);
insert(root, 3);
insert(root, 8);
insert(root, 6);
insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 7);
inorder(root);
}
Complexity of iterative insertion algorithm for binary search tree
Time Complexity
- Average situation
On average, the time complexity of inserting a node in a BST is comparable to the height of the binary search tree. On average, the height of a BST is O(logn)
. This happens when the formed BST is a balanced BST. Therefore, the time complexity is [Big Theta]: O(logn)
.
- Best Case
In the best case, the tree is a balanced BST. The time complexity of insertion in the best case is O(logn)
. It is the same as the time complexity in the average case.
- Worst case scenario
In the worst case, we may have to traverse from the root node to the deepest leaf node, which is the entire height of the tree h
. If the tree is unbalanced, that is, it is skewed, the height of the tree may become n
, so the worst-case time complexity of insertion and search operations is O(n)
.
Space complexity
The space complexity of the iterative insertion operation is O(1)
, since no additional space is required.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert a binary tree to a binary search tree
Publish Date:2025/03/18 Views:52 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
In-order descendants in a binary search tree
Publish Date:2025/03/18 Views:107 Category:ALGORITHM
-
The in-order descendant of a binary tree is the next node in the in-order traversal of the binary tree. So, for the last node in the tree, it is NULL . Since the in-order traversal of a binary search tree is a sorted array. The node with th
Binary Search Tree Deletion
Publish Date:2025/03/18 Views:93 Category:ALGORITHM
-
In the article Binary Search Trees: Searching and Inserting , we discussed how to insert an element into a binary search tree and how to search for a value in a binary search tree. In this article, we will discuss how to delete a node from
Binary Search Tree Check
Publish Date:2025/03/18 Views:79 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left children and right children. For a binary tree to be a BST, it must satisfy the following pr
Binary Search Tree
Publish Date:2025/03/18 Views:181 Category:ALGORITHM
-
A binary search tree (BST) is an ordered binary tree data structure based on nodes. A node has a value and two child nodes (a binary tree has at most two child nodes) connected to each other. A node has a value and two child nodes (a binary
Binary Tree Traversal
Publish Date:2025/03/18 Views:115 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
Circular Doubly Linked List
Publish Date:2025/03/18 Views:67 Category:ALGORITHM
-
A circular doubly linked list is a combination of a circular linked list and a doubly linked list. Its two nodes are connected by a previous and next pointer. The next pointer of the last node points to the first node, and the previous poin
Circular Linked List
Publish Date:2025/03/18 Views:172 Category:ALGORITHM
-
A circular linked list is a data structure that is slightly more complex than a linked list. It is a linked list where all the nodes are connected in a loop and form a chain. The last node does not have a NULL . Instead, it stores the addre
Doubly Linked List
Publish Date:2025/03/18 Views:69 Category:ALGORITHM
-
Doubly linked list is a linear data structure. It is a collection of objects defined as nodes. But unlike a linked list, the node has two pointers, one is the previous pointer and the other is the next pointer. Just like the linked list nod