Find Nearest Record in Binary Search Tree | C++ Implementation

Click For Summary
SUMMARY

This discussion focuses on implementing a function in C++ to find the nearest records in a Binary Search Tree (BST) when a specified key is not found. The function, BST_findNearest, takes a tree root, a key string x, and references to two BSTNode pointers n1 and n2. If the key is not found, n1 is assigned the address of the node with the largest key smaller than x, while n2 is assigned the address of the node with the smallest key larger than x. The implementation ensures that no new memory is allocated for the returned nodes.

PREREQUISITES
  • Understanding of Binary Search Trees (BST)
  • Proficiency in C++ programming
  • Knowledge of pointer manipulation in C++
  • Familiarity with string comparison in C++
NEXT STEPS
  • Study C++ pointer references and their usage in function parameters
  • Learn about Binary Search Tree traversal techniques
  • Explore C++ memory management and avoiding memory leaks
  • Investigate algorithms for nearest neighbor search in data structures
USEFUL FOR

Software developers, computer science students, and anyone interested in data structures and algorithms, particularly those working with Binary Search Trees in C++.

skyhyyzlz
Hi everyone, i have a binary search tree proble, please help me..


* Find record with key x in the tree. If not found, return the two nearest records in the tree (in alphabetical order)
* Return values:
* FOUND: Return the address of the node in the tree which contains key x, set n1 = n2 = NULL;
* NOT FOUND: Return NULL, n1, n2 are set to the nearest node to x in the tree, if exist.
* n1 is set to the address of the BSTNode whose key is just smaller than x in alphabetical order, if exists (Otherwise NULL).
* n2 is set to the address of the BSTNode whose key is just larger than x in alphabetical order, if exists (Otherwise NULL).
* NOTE:
* Always return the address of the existing "BSTNode"s. DO NOT allocate new spaces for returned strings.

How can I implement n1 and n2 in the following code for finding the nearest record when the key x is NOT FOUND?


const BSTNode * BST_findNearest( BSTNode * treeRoot, const string & x, BSTNode* &n1, BSTNode* &n2 )
{
if(treeRoot==NULL)
{
return NULL;
}
else if(x < treeRoot->name)
{
return BST_findNearest(treeRoot->left,x,n1,n2);
}
else if(x > treeRoot->name)
{
return BST_findNearest(treeRoot->right,x,n1,n2);
}
else
{
n1 = NULL;
n2 = NULL;
return treeRoot;
}

}
 
Technology news on Phys.org
From your question, I think I'm taking same course with you, in same University.
Just simply set n1 or n2 before returning the next node.

const BSTNode* BST_findNearest( BSTNode* treeRoot, const string &x, BSTNode* &n1, BSTNode* &n2 )
{
if (treeRoot == NULL) return NULL;
if (x == treeRoot->name) {
n1 = n2 = NULL;
return treeRoot;
}
else if (x < treeRoot->name) {
n2 = treeRoot;
return BST_findNearest(treeRoot->left, x, n1, n2);
}
else if (x > treeRoot->name) {
n1 = treeRoot;
return BST_findNearest(treeRoot->right, x, n1, n2);
}
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 7 ·
Replies
7
Views
75K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K