C Program: Binary Tree deletion with BST maintenance
5. Binary Tree Deletion Extensions
Write a C program that implements a deletion function for a binary tree. Allow users to delete nodes while maintaining the binary search tree structure.
Sample Solution:
C Code:
Output:
Input a value to insert into the binary search tree (enter 0 to stop): 75 Input a value to insert into the binary search tree (enter 0 to stop): 42 Input a value to insert into the binary search tree (enter 0 to stop): 35 Input a value to insert into the binary search tree (enter 0 to stop): 12 Input a value to insert into the binary search tree (enter 0 to stop): 10 Input a value to insert into the binary search tree (enter 0 to stop): 0 In-order Traversal (Sorted Order) of the Binary Search Tree: 10 12 35 42 75 Input a value to delete from the binary search tree (enter 0 to stop): 35 In-order Traversal after deleting 35: 10 12 42 75 Input a value to delete from the binary search tree (enter 0 to stop): 0
Explanation:
In the exercise above,
- Structure Definition:
- The program defines a structure "TreeNode" representing a node in a binary tree. Each node has an integer data value, a left child (left), and a right child (right).
- Node Creation:
- The createNode function allocates memory for a new node, initializes its data, left, and right pointers, and returns the created node.
- Node Insertion:
- The insertNode function inserts a node with a given value into the binary search tree. It recursively traverses the tree, compares values, and inserts the new node accordingly.
- Finding Minimum Value Node:
- The findMinValueNode function finds the node with the minimum value in a binary search tree. It is used during deletion.
- Node Deletion:
- The deleteNode function deletes a node with a given value from the binary search tree while maintaining the binary search tree property. It handles cases with one or two children.
- In-order Traversal:
- The inOrderTraversal function performs an in-order traversal of the binary search tree and prints the elements in sorted order.
- Freeing Memory:
- The freeTree function frees the memory allocated for the entire binary tree by recursively traversing and deallocating each node.
- Main Function:
- The main function allows users to insert nodes into the binary search tree and displays the sorted order of elements through in-order traversal.
- Users can then input values to delete from the binary search tree, and the program prints the tree after each deletion.
- Memory is freed before program completion.
Flowchart:
For more Practice: Solve these Related Problems:
- Write a C program to delete a node with two children from a BST and replace it with its in-order successor.
- Write a C program to implement both recursive and iterative deletion in a BST and compare their performance.
- Write a C program to simulate lazy deletion in a BST by marking nodes as deleted instead of physically removing them.
- Write a C program to delete a node from a BST and then automatically balance the tree by converting it into an AVL tree.
C Programming Code Editor:
Previous: C Program: Calculate height of Binary Tree with graceful handling.
Next:C Program: Binary Tree mirroring for a mirror image.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.