- C Program For Insertion And Deletion In Bst
- C Program For Insertion Deletion And Traversal In Binary Search Tree
A Binary Search Tree (BST) is a binary tree in which all the elements stored in the left subtree of node x are less then x and all elements stored in the right subtree of node x are greater then x. Below I have shared a C program for binary search tree insertion. Binary Search Tree Delete Node() natekelsey. My grader said that my delete function doesn't even work. I've ran it myself and debugged prior to turning it in and from what I could see it did work. Wondering if there was something that I missed or fat-fingered? General C++ Programming; Lounge; Jobs; Home page.
Hello,
After about a week of trying to figure out how to write a function that would take into consideration all aspects of deleting a node from a binary tree... I succeeded. After writing the function, I looked at it, and said to myself, 'There has to be a way to make this easier to write and easier to read.'
Since I am teaching myself, I wanted the communities input as to how I could have approached this differently, how I can reduce my code, if necessary (I have a feeling I could have used recursion), comments, critisism, etc. Below is the code:
Thank you for taking the time to read this thread and provide any comments.
We have discussed BST search and insert operations. In this post, delete operation is discussed. When we delete a node, three possibilities arise.
1) Node to be deleted is leaf: Simply remove from the tree.
1) Node to be deleted is leaf: Simply remove from the tree.
2) Node to be deleted has only one child: Copy the child to the node and delete the child
3) Node to be deleted has two children:Find inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor. Note that inorder predecessor can also be used.
The important thing to note is, inorder successor is needed only when right child is not empty. In this particular case, inorder successor can be obtained by finding the minimum value in right child of the node.
C/C++
// C program to demonstrate delete operation in binary search tree #include<stdlib.h> struct node int key; }; // A utility function to create a new BST node { struct node *temp = ( struct node *) malloc ( sizeof ( struct node)); temp->left = temp->right = NULL; } // A utility function to do inorder traversal of BST { { printf ( '%d ' , root->key); } /* A utility function to insert a new node with given key in BST */ { if (node NULL) return newNode(key); /* Otherwise, recur down the tree */ node->left = insert(node->left, key); node->right = insert(node->right, key); /* return the (unchanged) node pointer */ } /* Given a non-empty binary search tree, return the node with minimum key value found in that tree. Note that the entire tree does not struct node * minValueNode( struct node* node) struct node* current = node; /* loop down to find the leftmost leaf */ current = current->left; return current; /* Given a binary search tree and a key, this function deletes the key struct node* deleteNode( struct node* root, int key) // base case // If the key to be deleted is smaller than the root's key, if (key < root->key) // If the key to be deleted is greater than the root's key, else if (key > root->key) // if key is same as root's key, then This is the node else // node with only one child or no child { free (root); } { free (root); } // node with two children: Get the inorder successor (smallest struct node* temp = minValueNode(root->right); // Copy the inorder successor's content to this node root->right = deleteNode(root->right, temp->key); return root; int main() /* Let us create following BST / / / struct node *root = NULL; root = insert(root, 30); root = insert(root, 40); root = insert(root, 60); printf ( 'Inorder traversal of the given tree n' ); root = deleteNode(root, 20); printf ( 'Inorder traversal of the modified tree n' ); root = deleteNode(root, 30); printf ( 'Inorder traversal of the modified tree n' ); root = deleteNode(root, 50); printf ( 'Inorder traversal of the modified tree n' ); } |
Java
// Java program to demonstrate delete operation in binary search tree { /* Class containing left and right child of current node and key value*/ { Node left, right; public Node( int item) key = item; } Node root; // Constructor { } // This method mainly calls deleteRec() { } /* A recursive function to insert a new key in BST */ { if (root null ) return root; /* Otherwise, recur down the tree */ root.left = deleteRec(root.left, key); root.right = deleteRec(root.right, key); // if key is same as root's key, then This is the node else // node with only one child or no child return root.right; return root.left; // node with two children: Get the inorder successor (smallest root.key = minValue(root.right); // Delete the inorder successor } return root; { while (root.left != null ) minv = root.left.key; } } // This method mainly calls insertRec() { } /* A recursive function to insert a new key in BST */ { /* If the tree is empty, return a new node */ { return root; if (key < root.key) else if (key > root.key) return root; void inorder() inorderRec(root); // A utility function to do inorder traversal of BST { { System.out.print(root.key + ' ' ); } public static void main(String[] args) BinarySearchTree tree = new BinarySearchTree(); /* Let us create following BST / / / tree.insert( 50 ); tree.insert( 20 ); tree.insert( 70 ); tree.insert( 80 ); System.out.println( 'Inorder traversal of the given tree' ); tree.deleteKey( 20 ); System.out.println( 'Inorder traversal of the modified tree' ); tree.deleteKey( 30 ); System.out.println( 'Inorder traversal of the modified tree' ); tree.deleteKey( 50 ); System.out.println( 'Inorder traversal of the modified tree' ); } |
Python
# in binary search tree # A Binary Tree Node def __init__( self , key): self .left = None # A utility function to do inorder traversal of BST if root is not None : print root.key, # A utility function to insert a new node with given key in BST if node is None : if key < node.key: else : return node # Given a non-empty binary search tree, return the node # with minum key value found in that tree. Note that the def minValueNode( node): while (current.left is not None ): # Given a binary search tree and a key, this function def deleteNode(root, key): # Base Case return root # If the key to be deleted is smaller than the root's if key < root.key: # If the kye to be delete is greater than the root's key elif (key > root.key): # If key is same as root's key, then this is the node else : # Node with only one child or no child temp = root.right return temp elif root.right is None : root = None # Node with two children: Get the inorder successor temp = minValueNode(root.right) # Copy the inorder successor's content to this node root.right = deleteNode(root.right , temp.key) '' Let us create following BST / / / root = insert(root, 50 ) root = insert(root, 20 ) root = insert(root, 70 ) root = insert(root, 80 ) print 'Inorder traversal of the given tree' root = deleteNode(root, 20 ) inorder(root) print 'nDelete 30' print 'Inorder traversal of the modified tree' root = deleteNode(root, 50 ) inorder(root) # This code is contributed by Nikhil Kumar Singh(nickzuck_007) |
C#
// operation in binary search tree { child of current node and key value*/ { public Node left, right; public Node( int item) key = item; } Node root; // Constructor { } // This method mainly calls deleteRec() { } /* A recursive function to insert a new key in BST */ { if (root null ) return root; /* Otherwise, recur down the tree */ root.left = deleteRec(root.left, key); root.right = deleteRec(root.right, key); // if key is same as root's key, then This is the node else // node with only one child or no child return root.right; return root.left; // node with two children: Get the // in the right subtree) root.right = deleteRec(root.right, root.key); return root; { while (root.left != null ) minv = root.left.key; } } // This method mainly calls insertRec() { } /* A recursive function to insert a new key in BST */ { /* If the tree is empty, return a new node */ { return root; if (key < root.key) else if (key > root.key) return root; void inorder() inorderRec(root); // A utility function to do inorder traversal of BST { { Console.Write(root.key + ' ' ); } public static void Main(String[] args) BinarySearchTree tree = new BinarySearchTree(); /* Let us create following BST / / / tree.insert(50); tree.insert(20); tree.insert(70); tree.insert(80); Console.WriteLine( 'Inorder traversal of the given tree' ); tree.deleteKey(20); Console.WriteLine( 'Inorder traversal of the modified tree' ); tree.deleteKey(30); Console.WriteLine( 'Inorder traversal of the modified tree' ); tree.deleteKey(50); Console.WriteLine( 'Inorder traversal of the modified tree' ); } // by PrinciRaj1992 |
Output:
C Program For Insertion And Deletion In Bst
Illustration:
Time Complexity: The worst case time complexity of delete operation is O(h) where h is height of Binary Search Tree. In worst case, we may have to travel from root to the deepest leaf node. The height of a skewed tree may become n and the time complexity of delete operation may become O(n)
![C program for insertion and deletion in bst C program for insertion and deletion in bst](/uploads/1/2/6/0/126002644/476269706.jpg)
Optimization to above code for two children case :
In the above recursive code, we recursively call delete() for successor. We can avoid recursive call by keeping track of parent node of successor so that we can simply remove the successor by making child of parent as NULL. We know that successor would always be a leaf node.
In the above recursive code, we recursively call delete() for successor. We can avoid recursive call by keeping track of parent node of successor so that we can simply remove the successor by making child of parent as NULL. We know that successor would always be a leaf node.
// C++ program to implement optimized delete in BST. using namespace std; struct Node { struct Node *left, *right; Node* newNode( int item) Node* temp = new Node; temp->left = temp->right = NULL; } // A utility function to do inorder traversal of BST { inorder(root->left); inorder(root->right); } /* A utility function to insert a new node with given key in BST */ { if (node NULL) if (key < node->key) else return node; /* Given a binary search tree and a key, this function deletes the key Node* deleteNode(Node* root, int k) // Base case return root; // Recursive calls for ancestors of if (root->key > k) { return root; else if (root->key < k) { return root; // to be deleted. // If one of the children is empty Node* temp = root->right; return temp; else if (root->right NULL) { delete root; } // If both children exist Node *succ = root->right; succParent = succ; } // Delete successor. Since successor // we can safely make successor's right succParent->left = succ->right; // Copy Successor Data to root delete succ; } int main() /* Let us create following BST / / / Node* root = NULL; root = insert(root, 30); root = insert(root, 40); root = insert(root, 60); printf ( 'Inorder traversal of the given tree n' ); root = deleteNode(root, 20); printf ( 'Inorder traversal of the modified tree n' ); root = deleteNode(root, 30); printf ( 'Inorder traversal of the modified tree n' ); root = deleteNode(root, 50); printf ( 'Inorder traversal of the modified tree n' ); } |
Thanks to wolffgang010 for suggesting above optimization.
Related Links:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
C Program For Insertion Deletion And Traversal In Binary Search Tree
Recommended Posts:
Improved By : Manoj Kumar 20, wolffgang010, princiraj1992, Sarvesh Ranjan