searching binary search tree

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 23
Reputation: maverick786 is an unknown quantity at this point 
Solved Threads: 0
maverick786 maverick786 is offline Offline
Newbie Poster

searching binary search tree

 
-1
  #1
Oct 30th, 2007
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: searching binary search tree

 
0
  #2
Oct 30th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC