Operation On Binary Search Tree(BST)
- Create:- Create tree with 'n' nodes.
- Traverse:- Visit each & every node of tree.
- Search:- Searches for a specific element in the tree.
- Insert:- The new element will be inserted according to its value.
- Delete:- Delete a specific node from the tree.
- Counting total nodes
- Counting leaf nodes
- Copying tree to another
- Mirror of tree
- Create:-
Some rules satisfy for create a nodes.
Step 1:- Start
Step 2:- root=NULL
Step 3:- Read the value and create a newnode to store this value.
Step 4:- If root==NULL
root=newnode
goto Step 7
Step 5:- temp=root
Step 6:- If key<temp->info
If temp doesn't have left child
attach newnode to temp->left
go to Step 7
Else
temp=temp->left;
go to Step 7
Else
IF temp doesn't have right child,
attach newnode to temp->right
Else
temp=temp->right
go to Step 6
Step 7:- Repeat from 3 till all values have been inserted in tree.
Step 8:- Stop
- Traverse
- Traversing tree means visiting all it's nodes to get the complete tree tree information.
- Data structure like array and linked list are linear. Hence it is easy to traverse all element.
- A tree is a non linear data structure hence tree traversal is complicated.
Three methods to traverse tree:-
- Perorder(Data-Left-Right):-
- Visit the root.
- Traverse left subtree in Preorder.
- Treverse right subtree in Preorder.
- Inorder(Left-Data-Right)
- Traverse left subtree in Inorder.
- Visit the root.
- Treverse right subtree in Inorder.
- Postorder(Left-Right-Data)
- Traverse left subtree in Postorder.
- Treverse right subtree in Postorder.
- Visit the root.
Code:-
👇
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int info;
- struct node *left, *right;
- }NODE;
- //TREE FUNCTION
- NODE * createbst(NODE *root)
- {
- NODE *newnode, *temp, *parent;
- int i, n, num;
- printf("\nHow many nodes you want to enter?\t");
- scanf("%d", &n);
- for(i=1; i<=n; i++)
- {
- newnode = (NODE*)malloc(sizeof (NODE));
- printf("Enter the %d elements:\t", n);
- scanf("%d", &num);
- newnode->info=num;
- newnode->left = newnode->right = NULL;
- if(root == NULL)
- {
- root = newnode;
- continue;
- }
- temp = root;
- while(temp != NULL)
- {
- parent = temp;
- if(num < temp->info)
- temp = temp->left;
- else
- temp = temp->right;
- }
- if(num < parent->info)
- parent->left = newnode;
- else
- parent->right = newnode;
- }
- return(root);
- }
- // Traversal Function
- void preorder(NODE * root)
- {
- NODE *temp = root;
- if(temp != NULL)
- {
- printf("%d\t", temp->info);
- preorder(temp->left);
- preorder(temp->right);
- }
- }
- void inorder(NODE * root)
- {
- NODE *temp = root;
- if(temp != NULL)
- {
- inorder(temp->left);
- printf("%d\t", temp->info);
- inorder(temp->right);
- }
- }
- void postorder(NODE * root)
- {
- NODE *temp = root;
- if(temp != NULL)
- {
- postorder(temp->left);
- postorder(temp->right);
- printf("%d\t", temp->info);
- }
- }
- void main()
- {
- NODE *root=NULL;
- printf("\t>>CREATE & TRAVERSAL<<\n");
- root = createbst(root);
- //Recursive Traversal
- printf("\nPerorder traversal is:\t");
- preorder(root);
- printf("\nInorder traversal is:\t");
- inorder(root);
- printf("\nPostorder traversal is:\t");
- postorder(root);
- }
👉Execute👈
//OUTPUT
/*
>>CREATE & TRAVERSAL<<
How many nodes you want to enter? 5
Enter the 5 elements: 10
Enter the 5 elements: 5
Enter the 5 elements: 15
Enter the 5 elements: 6
Enter the 5 elements: 12
Perorder traversal is: 10 5 6 15 12
Inorder traversal is: 5 6 10 12 15
Postorder traversal is: 6 5 12 15 10
*/
Comments
Post a Comment