![]() |
A binary Search Tree is a binary tree where the value of any node is greater than the left subtree and less than the right subtree. In this article, we will discuss Binary Search Trees and various operations on Binary Search trees using C programming language. Properties of Binary Search TreeFollowing are some main properties of the binary search tree in C:
Example:![]() Binary Search Tree Binary Tree Structure in Cstruct BinaryTreeNode { int key; struct nodeBinaryTreeNode *left, *right; }; here,
Operations on BST C
1. Search Operation on BST in CThe algorithm for the search operation is:
where the target is the key to search for in the tree. Time Complexity: O(log n) 2. Insertion in BSTThe insertNode function inserts a new node with a specific value into a Binary Search Tree while maintaining the binary search tree property. Algorithm
Time Complexity: O(log n) 4. Deletion in BSTThere are few cases at the time of deleting a node in BST a. Deleted node is the leaf nodeIn this case, simply delete the node from the tree. b. Deleted node has a single child nodeIn this case, replace the target node with its child (single child) and then delete that child node. c. Deleted node has two childrenIn this case, at first, we will find the inorder successor of that node. And then replace the node with the inorder successor. Then remove the inorder successor from its original position. Algorithm
Time Complexity: O(log n) C Program to Implement Binary Search TreeC
Output
60 found 20 40 30 60 80 70 50 50 30 20 40 70 60 80 20 30 40 50 60 70 80 After Delete: 20 30 40 50 60 80 To know more about Binary Search Tree, refer to the article – Binary Search Tree |
Reffered: https://www.geeksforgeeks.org
Binary Search Tree |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |