Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Functionality to Find Inorder Successor and Predecessor in Binary Search Tree (BST) #1545 #1572

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>

// Structure for a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Helper function to create a new node in BST
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}

// Function to insert a new node with given data in BST
struct Node* insert(struct Node* node, int data) {
if (node == NULL) return newNode(data);

if (data < node->data) {
node->left = insert(node->left, data);
} else if (data > node->data) {
node->right = insert(node->right, data);
}
return node;
}

// Function to find the Inorder Predecessor
struct Node* findPredecessor(struct Node* root, struct Node* node) {
struct Node* predecessor = NULL;

while (root) {
if (node->data <= root->data) {
root = root->left;
} else {
predecessor = root;
root = root->right;
}
}
return predecessor;
}

// Function to find the Inorder Successor
struct Node* findSuccessor(struct Node* root, struct Node* node) {
struct Node* successor = NULL;

while (root) {
if (node->data >= root->data) {
root = root->right;
} else {
successor = root;
root = root->left;
}
}
return successor;
}

// Driver function to test the above functions
int main() {
struct Node* root = NULL;
root = insert(root, 20);
insert(root, 8);
insert(root, 22);
insert(root, 4);
insert(root, 12);
insert(root, 10);
insert(root, 14);

struct Node* node = root->left->right; // Node with value 12

struct Node* pred = findPredecessor(root, node);
if (pred != NULL) {
printf("Inorder Predecessor of %d is %d\n", node->data, pred->data);
} else {
printf("Inorder Predecessor of %d does not exist\n", node->data);
}

struct Node* succ = findSuccessor(root, node);
if (succ != NULL) {
printf("Inorder Successor of %d is %d\n", node->data, succ->data);
} else {
printf("Inorder Successor of %d does not exist\n", node->data);
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Inorder Successor and Predecessor in a Binary Search Tree (BST)

Overview
The Inorder Successor and Predecessor functions are implemented to assist with traversal operations in a Binary Search Tree (BST). These functions find specific nodes in a BST:
- The Inorder Predecessor of a given node, which is the largest node that comes before it in in-order traversal.
- The Inorder Successor of a given node, which is the smallest node that comes after it in in-order traversal.

This functionality is valuable in various applications, including database indexing, memory management, and scenarios where node navigation in a BST is required.

Function Descriptions
1. findPredecessor:
This function searches for the Inorder Predecessor of a given node in the BST. It traverses the BST to find the largest node smaller than the given node, typically residing in the left subtree. If no predecessor exists (i.e., if the given node is the smallest in the tree), the function returns NULL.

2. findSuccessor:
This function searches for the Inorder Successor of a given node. It locates the smallest node greater than the given node, typically residing in the right subtree. If no successor exists (i.e., if the given node is the largest in the tree), the function returns NULL.

Code Structure
The implementation consists of the following:
- Node Structure: Defines each node in the BST with pointers to its left and right children.
- Insert Function: Inserts new nodes into the BST in sorted order, ensuring BST properties.
- Predecessor and Successor Functions: Efficiently locate the predecessor and successor nodes by utilizing the properties of the BST.
- Main Function: Demonstrates the functionality by constructing a BST and testing the findPredecessor and findSuccessor functions on sample nodes.

Example:
Consider a BST constructed with the following elements:
20
/ \
8 22
/ \
4 12
/ \
10 14

We’ll find the Inorder Successor and Predecessor for the node with value 12.

Step-by-Step Example:

1. Create the BST:
- Insert nodes in this order: 20, 8, 22, 4, 12, 10, 14.
- The resulting BST is as shown above.

2. Find Predecessor and Successor for Node with Value 12:
- Inorder Predecessor: The largest value node smaller than 12 is 10.
- Inorder Successor: The smallest value node larger than 12 is 14.

Example Output:
Using the provided code, running the findPredecessor and findSuccessor functions for the node with value 12 would yield the following output:
Inorder Predecessor of 12 is 10
Inorder Successor of 12 is 14
Binary file not shown.
Loading