Binary Search Tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes.
Below are the various operations that can be performed on a BST:
- Insert a node into a BST: A new key is always inserted at leaf. Start searching a key from root till a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.
// C program to insert a node// in a BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to do inorder traversal of BSTvoidinorder(structnode* root){if(root != NULL) {inorder(root->left);printf("%d ", root->key);inorder(root->right);}}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// inserting value 50root = insert(root, 50);// inserting value 30insert(root, 30);// inserting value 20insert(root, 20);// inserting value 40insert(root, 40);// inserting value 70insert(root, 70);// inserting value 60insert(root, 60);// inserting value 80insert(root, 80);// print the BSTinorder(root);return0;}chevron_rightfilter_noneOutput:20 30 40 50 60 70 80
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Inorder traversal: In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order.
// C program to implement// inorder traversal of BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to do inorder traversal of BSTvoidinorder(structnode* root){if(root != NULL) {inorder(root->left);printf("%d ", root->key);inorder(root->right);}}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function Callinorder(root);return0;}chevron_rightfilter_noneOutput:
20 30 40 50 60 70 80
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Preorder traversal: Preorder traversal first visits the root node and then traverses the left and the right subtree. It is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree.
// C program to implement// preorder traversal#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to do preorder traversal of BSTvoidpreOrder(structnode* root){if(root != NULL) {printf("%d ", root->key);preOrder(root->left);preOrder(root->right);}}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallpreOrder(root);return0;}chevron_rightfilter_noneOutput:50 30 20 40 70 60 80
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Postorder traversal: Postorder traversal first traverses the left and the right subtree and then visits the root node. It is used to delete the tree.
// C program to implement// postorder traversal#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to do postorder traversal of BSTvoidpostOrder(structnode* root){if(root != NULL) {postOrder(root->left);postOrder(root->right);printf("%d ", root->key);}}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallpostOrder(root);return0;}chevron_rightfilter_noneOutput:20 40 30 60 80 70 50
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Level order traversal: Level order traversal of a BST is breadth first traversal for the tree. It visits all nodes at a particular level first before moving to the next level.
// C program to implement// level order traversal#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Returns height of the BSTintheight(structnode* node){if(node == NULL)return0;else{// Compute the depth of each subtreeintlDepth = height(node->left);intrDepth = height(node->right);// Use the larger oneif(lDepth > rDepth)return(lDepth + 1);elsereturn(rDepth + 1);}}// Print nodes at a given levelvoidprintGivenLevel(structnode* root,intlevel){if(root == NULL)return;if(level == 1)printf("%d ", root->key);elseif(level > 1) {// Recursive CallprintGivenLevel(root->left, level - 1);printGivenLevel(root->right, level - 1);}}// Function to line by line print// level order traversal a treevoidprintLevelOrder(structnode* root){inth = height(root);inti;for(i = 1; i <= h; i++) {printGivenLevel(root, i);printf("\n");}}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallprintLevelOrder(root);return0;}chevron_rightfilter_noneOutput:50 30 70 20 40 60 80
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Print nodes at given Level : It prints all the nodes at a particular level of the BST.
// C program to print nodes// at a given level#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Print nodes at a given levelvoidprintGivenLevel(structnode* root,intlevel){if(root == NULL)return;if(level == 1)printf("%d ", root->key);elseif(level > 1) {// Recursive CallprintGivenLevel(root->left, level - 1);printGivenLevel(root->right, level - 1);}}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallprintGivenLevel(root, 2);return0;}chevron_rightfilter_noneOutput:30 70
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Print all leaf nodes: A node is a leaf node if both left and right child nodes of it are NULL.
// C program to print all// leaf nodes of a BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to print leaf nodes// from left to rightvoidprintLeafNodes(structnode* root){// If node is null, returnif(!root)return;// If node is leaf node,// print its dataif(!root->left && !root->right) {printf("%d ", root->key);return;}// If left child exists,// check for leaf recursivelyif(root->left)printLeafNodes(root->left);// If right child exists,// check for leaf recursivelyif(root->right)printLeafNodes(root->right);}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallprintLeafNodes(root);return0;}chevron_rightfilter_noneOutput:20 40 60 80
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Print all non leaf node: A node is a non leaf node if either of its left or right child nodes are not NULL.
// C program to print all// non leaf nodes of a BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to print all non-leaf// nodes in a treevoidprintNonLeafNode(structnode* root){// Base Casesif(root == NULL|| (root->left == NULL&& root->right == NULL))return;// If current node is non-leaf,if(root->left != NULL|| root->right != NULL) {printf("%d ", root->key);}// If root is Not NULL and its one// of its child is also not NULLprintNonLeafNode(root->left);printNonLeafNode(root->right);}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallprintNonLeafNode(root);return0;}chevron_rightfilter_noneOutput:50 30 70
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Right view of BST: Right view of a Binary Search Tree is set of nodes visible when tree is visited from Right side.
// C program to print// right view of a BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to print the right view// of a binary tree.voidrightViewUtil(structnode* root,intlevel,int* max_level){// Base Caseif(root == NULL)return;// If this is the last Node of its levelif(*max_level < level) {printf("%d\t", root->key);*max_level = level;}// Recur for right subtree first,// then left subtreerightViewUtil(root->right, level + 1,max_level);rightViewUtil(root->left, level + 1,max_level);}// Wrapper over rightViewUtil()voidrightView(structnode* root){intmax_level = 0;rightViewUtil(root, 1, &max;_level);}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallrightView(root);return0;}chevron_rightfilter_noneOutput:50 70 80
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Left view of BST: Left view of a Binary Search Tree is set of nodes visible when tree is visited from Left side.
// C program to print// left view of a BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to print left view of// binary treevoidleftViewUtil(structnode* root,intlevel,int* max_level){// Base Caseif(root == NULL)return;// If this is the first node// of its levelif(*max_level < level) {printf("%d\t", root->key);*max_level = level;}// Recur for left and right subtreesleftViewUtil(root->left, level + 1,max_level);leftViewUtil(root->right, level + 1,max_level);}// Wrapper over leftViewUtil()voidleftView(structnode* root){intmax_level = 0;leftViewUtil(root, 1, &max;_level);}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function CallleftView(root);return0;}chevron_rightfilter_noneOutput:50 30 20
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Height of BST: It is recursively calculated using height of left and right subtrees of the node and assigns height to the node as max of the heights of two children plus 1.
// C program to print// height of a BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Returns height of the BSTintheight(structnode* node){if(node == NULL)return0;else{// Compute the depth of each subtreeintlDepth = height(node->left);intrDepth = height(node->right);// Use the larger oneif(lDepth > rDepth)return(lDepth + 1);elsereturn(rDepth + 1);}}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function Callprintf("%d", height(root));return0;}chevron_rightfilter_noneOutput:3
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Delete a Node of BST: It is used to delete a node with specific key from the BST and return the new BST.
// C program to delete// a node of BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to do inorder traversal of BSTvoidinorder(structnode* root){if(root != NULL) {inorder(root->left);printf("%d ", root->key);inorder(root->right);}}// Function that returns the node with minimum// key value found in that treestructnode* minValueNode(structnode* node){structnode* current = node;// Loop down to find the leftmost leafwhile(current && current->left != NULL)current = current->left;returncurrent;}// Function that deletes the key and// returns the new rootstructnode* deleteNode(structnode* root,intkey){// base Caseif(root == NULL)returnroot;// If the key to be deleted is// smaller than the root's key,// then it lies in left subtreeif(key < root->key) {root->left= deleteNode(root->left, key);}// If the key to be deleted is// greater than the root's key,// then it lies in right subtreeelseif(key > root->key) {root->right= deleteNode(root->right, key);}// If key is same as root's key,// then this is the node// to be deletedelse{// Node with only one child// or no childif(root->left == NULL) {structnode* temp = root->right;free(root);returntemp;}elseif(root->right == NULL) {structnode* temp = root->left;free(root);returntemp;}// Node with two children:// Get the inorder successor(smallest// in the right subtree)structnode* temp = minValueNode(root->right);// Copy the inorder successor's// content to this noderoot->key = temp->key;// Delete the inorder successorroot->right= deleteNode(root->right, temp->key);}returnroot;}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function Callroot = deleteNode(root, 60);inorder(root);return0;}chevron_rightfilter_noneOutput:20 30 40 50 70 80
Time Complexity: O(log N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Smallest Node of the BST: It is used to return the node with the smallest value in the BST.
// C program print smallest// element of BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function that returns the node with minimum// key value found in that treestructnode* minValueNode(structnode* node){structnode* current = node;// Loop down to find the leftmost leafwhile(current && current->left != NULL)current = current->left;returncurrent;}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function Callprintf("%d", minValueNode(root)->key);return0;}chevron_rightfilter_noneOutput:20
Time Complexity: O(log N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Total number of nodes in a BST: The function returns the total count of nodes in the BST.
// C program to print total// count of nodes in BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to get the total count of// nodes in a binary treeintnodeCount(structnode* node){if(node == NULL)return0;elsereturnnodeCount(node->left)+ nodeCount(node->right) + 1;}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// Function Callprintf("%d", nodeCount(root));return0;}chevron_rightfilter_noneOutput:7
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1) - Delete a BST: It is used to completely delete the BST and deallocate the memory.
// C program to delete a BST#include <stdio.h>#include <stdlib.h>// Given Node nodestructnode {intkey;structnode *left, *right;};// Function to create a new BST nodestructnode* newNode(intitem){structnode* temp= (structnode*)malloc(sizeof(structnode));temp->key = item;temp->left = temp->right = NULL;returntemp;}// Function to insert a new node with// given key in BSTstructnode* insert(structnode* node,intkey){// If the tree is empty, return a new nodeif(node == NULL)returnnewNode(key);// Otherwise, recur down the treeif(key < node->key) {node->left = insert(node->left, key);}elseif(key > node->key) {node->right = insert(node->right, key);}// Return the node pointerreturnnode;}// Function to do inorder traversal of BSTvoidinorder(structnode* root){if(root != NULL) {inorder(root->left);printf("%d ", root->key);inorder(root->right);}}// Function to delete the BSTstructnode* emptyBST(structnode* root){structnode* temp;if(root != NULL) {// Traverse to left subtreeemptyBST(root->left);// Traverse to right subtreeemptyBST(root->right);printf("Released node:%d \n", root->key);temp = root;// Require for free memoryfree(temp);}returnroot;}// Driver Codeintmain(){/* Let us create following BST50/ \30 70/ \ / \20 40 60 80*/structnode* root = NULL;// Creating the BSTroot = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);printf("BST before deleting:\n");inorder(root);// Function Callroot = emptyBST(root);return0;}chevron_rightfilter_noneOutput:BST before deleting: 20 30 40 50 60 70 80 Released node:20 Released node:40 Released node:30 Released node:60 Released node:80 Released node:70 Released node:50
Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)
Recommended Posts:
- Endian order and binary files
- Find k-th bit in a binary string created by repeated invert and append operations
- Best meeting point in 2D binary array
- Rethinking binary with Quantum computers
- Friedman Test
- Biased GPT -3
- Blockchain of Things (BoT)
- The SON Algorithm and Map - Reduce
- Screen Density and Its terminologies
- Proof that NAE-4-SAT is NP Complete
- Effective ways to increase Traffic on your Website
- Bisecting K-Means Algorithm Introduction
- Flutter - Fading a Widget
- Composite Transformation in 2-D graphics
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


