Insertion In Binary Search Tree In Java
Insertion In Binary Search Tree
A Binary Search Tree is a rooted binary tree whose internal nodes each a key greater than all the keys in the node’s left subtree and less than those in it’s right subtree. Insertion function is used to add new element in a binary search tree at appropriate position. In this article we will perfom insertion in binary search tree using recursion in java.
Algorithm To Insert In BST
Algorithm(ROOT, ITEM):
- If ROOT is null , Allocate the memory for ROOT.Set the ITEM to value and left and right pointer of ROOT , point to null.
- Check if the ITEM is less than the root element of the tree , if it is true then recursively perform this operation to the left of the tree.
- Else if the ITEM is greater than the root , then recursively perform this operation for the right of the tree.
- Insert a node where null is encountered.
- Return the ROOT of the tree.
Example To Insert A Node In Binary Search Tree
CODE FOR INSERTION IN BST
Code
Code
Run
import java.util.*; /*class Representing a Node of a tree */ class Node { int value; Node left,right; Node(int value) { this.value=value; left=right=null; } } class Insertion { Node root; //root of the tree //Constructor to intialize root with null Insertion() { root=null; } //preorder Traversal of binary tree public static void preorder(Node ptr) { if(ptr==null) return ; System.out.print(ptr.value+" "); preorder(ptr.left); preorder(ptr.right); } public void insert(int item) { root =insertNode(root,item); //calling inserNode() method } public Node insertNode(Node root,int item) { if(root==null) //if root is null create a new Node { root=new Node(item); return root; } if(item < root.value) //if item is less than the current value then traverse left subtree root.left= insertNode(root.left,item); else if(item>root.value) //if item is greater than the cureent value then traverse the right subtree root.right=insertNode(root.right,item); return root; } } public class Main{ public static void main(String[] args) { Insertion tree=new Insertion(); /*inserting node one by one in Binary Search Tree */ tree.insert(30); tree.insert(50); tree.insert(55); tree.insert(45); tree.insert(10); tree.insert(5); tree.insert(15); tree.insert(12); //print preorder traversal of binary tree tree.preorder(tree.root); } }
Output
30 10 5 15 12 50 45 55
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
Login/Signup to comment