TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Insertion in Binary Search Tree

Insertion in Binary Search Tree with Introduction, Asymptotic Analysis, Array, Pointer, Structure, Singly Linked List, Doubly Linked List, Circular Linked List, Binary Search, Linear Search, Sorting, Bucket Sort, Comb Sort, Shell Sort, Heap Sort, Merge Sort, Selection Sort, Counting Sort, Stack, Qene, Circular Quene, Graph, Tree, B Tree, B+ Tree, Avl Tree etc.

<< Back to INSERTION

Insertion

Insert function is used to add a new element in a binary search tree at appropriate location. Insert function is to be designed in such a way that, it must node violate the property of binary search tree at each value.

  1. Allocate the memory for tree.
  2. Set the data part to the value and set the left and right pointer of tree, point to NULL.
  3. If the item to be inserted, will be the first element of the tree, then the left and right of this node will point to NULL.
  4. Else, check if the item is less than the root element of the tree, if this is true, then recursively perform this operation with the left of the root.
  5. If this is false, then perform this operation recursively with the right sub-tree of the root.

Insert (TREE, ITEM)

  • Step 1: IF TREE = NULL
        Allocate memory for TREE
       SET TREE -> DATA = ITEM
      SET TREE -> LEFT = TREE -> RIGHT = NULL
      ELSE
       IF ITEM < TREE -> DATA
        Insert(TREE -> LEFT, ITEM)
      ELSE
       Insert(TREE -> RIGHT, ITEM)
      [END OF IF]
      [END OF IF]
  • Step 2: END

insertion in binary search tree

C Function

#include<stdio.h>
#include<stdlib.h>
void insert(int);
struct node
{
	int data;
	struct node *left; 
	struct node *right; 
};
struct node *root;
void main ()
{
	int choice,item;
	do 
	{
		printf("\nEnter the item which you want to insert?\n");
		scanf("%d",&item);
		insert(item);
		printf("\nPress 0 to insert more ?\n");
		scanf("%d",&choice);
	}while(choice == 0);
}
void insert(int item)
{
	struct node *ptr, *parentptr , *nodeptr;
	ptr = (struct node *) malloc(sizeof (struct node));
	if(ptr == NULL)
	{
		printf("can't insert");
	}
	else 
	{
	ptr -> data = item;
	ptr -> left = NULL;
	ptr -> right = NULL; 
	if(root == NULL)
	{
		root = ptr;
		root -> left = NULL;
		root -> right = NULL;
	}
	else 
	{
		parentptr = NULL;
		nodeptr = root; 
		while(nodeptr != NULL)
		{
			parentptr = nodeptr; 
			if(item < nodeptr->data)
			{
				nodeptr = nodeptr -> left; 
			} 
			else 
			{
				nodeptr = nodeptr -> right;
			}
		}
		if(item < parentptr -> data)
		{
			parentptr -> left = ptr; 
		}
		else 
		{
			parentptr -> right = ptr; 
		}
	}
	printf("Node Inserted");
	}
}

Output

Enter the item which you want to insert?
12
Node Inserted
Press 0 to insert more ?
0

Enter the item which you want to insert?
23
Node Inserted
Press 0 to insert more ?
1

Next TopicDoubly Linked List




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf