944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1796
  • C++ RSS
Oct 30th, 2007
-1

searching binary search tree

Expand Post »
I would like to know what would be the best way to count the nodes accessed while searching for an item in a binary search tree. I have to keep a tally for each item I search for. I have included my search method from my program.
C++ Syntax (Toggle Plain Text)
  1. void BinarySearchTree::find(int d)
  2. {
  3. //Locate the element
  4. bool found = false;
  5. if(isEmpty())
  6. {
  7. cout<<" This Tree is empty! "<<endl;
  8. return;
  9. }
  10. tree_node* curr;
  11. tree_node* parent;
  12. curr = root;
  13. while(curr != NULL)
  14. {
  15. if(curr->data == d)
  16. {
  17. found = true;
  18. cout << " Item " << d << " found" << endl;
  19.  
  20. break;
  21. }
  22. else
  23. {
  24. parent = curr;
  25. if(d>curr->data) curr = curr->right;
  26. else curr = curr->left;
  27. }
  28. }
  29. if(!found)
  30. {
  31. cout << " " << d <<" Data not found! "<<endl;
  32. return;
  33. }
  34. if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
  35. && curr->right == NULL))
  36. {
  37. if(curr->left == NULL && curr->right != NULL)
  38. {
  39. if(parent->left == curr)
  40. parent->left = curr->right;
  41. else
  42. parent->right = curr->right;
  43. }
  44. else // left child present, no right child
  45. {
  46. if(parent->left == curr)
  47. parent->left = curr->left;
  48. else
  49. parent->right = curr->left;
  50.  
  51. }
  52. return;
  53. }
  54. //We're looking at a leaf node
  55. if( curr->left == NULL && curr->right == NULL)
  56. {
  57. if(parent->left == curr)
  58. parent->left = NULL;
  59. else parent->right = NULL;
  60. return;
  61. }
  62. //Node with 2 children
  63. // replace node with smallest value in right subtree
  64. if (curr->left != NULL && curr->right != NULL)
  65. {
  66. tree_node* chkr;
  67. chkr = curr->right;
  68. if((chkr->left == NULL) && (chkr->right == NULL))
  69. {
  70. curr = chkr;
  71. curr->right = NULL;
  72. }
  73. else // right child has children
  74. {
  75. //if the node's right child has a left child
  76. // Move all the way down left to locate smallest element
  77. if((curr->right)->left != NULL)
  78. {
  79. tree_node* lcurr;
  80. tree_node* lcurrp;
  81. lcurrp = curr->right;
  82. lcurr = (curr->right)->left;
  83. while(lcurr->left != NULL)
  84. {
  85. lcurrp = lcurr;
  86. lcurr = lcurr->left;
  87. }
  88. curr->data = lcurr->data;
  89. lcurrp->left = NULL;
  90. }
  91. else
  92. {
  93. tree_node* tmp;
  94. tmp = curr->right;
  95. curr->data = tmp->data;
  96. curr->right = tmp->right;
  97. }
  98. }
  99. return;
  100. }
Thank you. I hope my question was
Last edited by Ancient Dragon; Oct 30th, 2007 at 8:40 am. Reason: corrected code tags
Similar Threads
Reputation Points: 8
Solved Threads: 0
Newbie Poster
maverick786 is offline Offline
23 posts
since Mar 2007
Oct 30th, 2007
0

Re: searching binary search tree

A more important question is: why is that find method returning void? A find method normally locates an instance of something and returns the result to the calling function. If it didn't there would be no point to the function as it appears to be with your function.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ scores
Next Thread in C++ Forum Timeline: beginner of c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC