[Dev-C++] binary tree search + need to find char*
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Peter M. <pet...@im...> - 2003-07-09 00:26:30
|
Hello,
I am still learning C++ and I am not sure how to do this.
I am writing a dictionary for use with secondary students. The words,
descriptions and a unique code get read in from a binary file on disk
and stored in individual nodes of a binary search tree according to the
code in each entry of the binary file on disk.
i can retrieve a word and description at runtime by using the code as
given by the user however, i need to be able to search the tree
structure for the word as given by the user. i can find the word by
testing recursively, but i there is a more efficient way to do this.
here is the search for the code:
////////////////////////////////////////////////////
DicTreeNode* DicBinaryTree::idBTreeSearch(DicTreeNode* nodePtr, long
dicId) {
if (nodePtr != 0) {
if (dicId < nodePtr->id){
this->idBTreeSearch(nodePtr->leftChild,dicId);
}
else if (dicId > nodePtr->id) {
this->idBTreeSearch(nodePtr->rightChild,dicId);
}
else {
rtnNodePtr = nodePtr;
}
return rtnNodePtr;
}
else {
return nodePtr = 0;
}
}
////////////////////////////////////////////////////
the above code works fine with a long as the id for the search...
the code below works but is not a good way of doing the search...
////////////////////////////////////////////////////
DicTreeNode* DicBinaryTree::wordBTreeSearch(DicTreeNode* nodePtr, char*
dicWord ){
if (nodePtr != 0) { // this method of finding the word is not
efficient...
this->wordBTreeSearch(nodePtr->leftChild,dicWord);
this->wordBTreeSearch(nodePtr->rightChild,dicWord);
if (!strcmp(nodePtr->word,dicWord)) {
this->rtnNodePtr = nodePtr;
}
return this->rtnNodePtr;
}
else{
return nodePtr = 0;
}
}
////////////////////////////////////////////////////
any ideas??
TIA
Peter Mead.
|