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

Conversation

Kamini8707
Copy link
Contributor

Issue #1545

Description: This pull request adds two key functions to the project for navigating a Binary Search Tree (BST) efficiently:

  • findPredecessor: Finds the Inorder Predecessor of a specified node, i.e., the largest node smaller than the given node in in-order traversal.
  • findSuccessor: Finds the Inorder Successor of a specified node, i.e., the smallest node larger than the given node in in-order traversal.

Changes Made

  1. BST Node Structure:
    Defined a Node structure with data, left, and right pointers to represent each node in the BST.

  2. Insert Function:
    Added an insert function to build and populate the BST, preserving the properties of a binary search tree.

  3. Inorder Predecessor and Successor Functions:

  • findPredecessor: Finds the largest node smaller than a given node (Inorder Predecessor).
  • findSuccessor: Finds the smallest node larger than a given node (Inorder Successor).
  • These functions leverage BST properties to achieve efficient traversal and node navigation.

Example Usage in Main Function:
Demonstrates the usage of findPredecessor and findSuccessor functions on an example BST.

Example Output
For a BST constructed with the following sequence: 20, 8, 22, 4, 12, 10, 14:

Testing Inorder Predecessor and Successor for the node with value 12 yields
Inorder Predecessor of 12 is 10
Inorder Successor of 12 is 14

Usage and Benefits

  • Inorder Predecessor and Successor:
    • These functions enhance the project by adding options for node navigation, which is valuable in scenarios like in-order traversal, sorted data retrieval, and hierarchical data processing.
    • Useful for applications such as:
      • Database indexing.
      • Memory management systems.
      • Any system relying on ordered data structures.

Copy link
Member

@pankaj-bind pankaj-bind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{BD5094B8-C6F5-4E8B-9E17-9C160FD85884}
Remove Merge two BSTs folder

@pankaj-bind pankaj-bind closed this Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants