473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

binary search trees

My program is required to build a binary search tree using integers
inputted from a file. It also searches for items and counts the nodes
accessed in each search. Also it needs to calculate the average number
of comparisons per search. I am not sure where to insert the counts
because it seems I am getting the wrong count. Here is my retrieve
function:
Expand|Select|Wrap|Line Numbers
  1.  
  2. int Retrieve(TreeNode* tree,
  3. ItemType& item, bool& found, int& num)
  4. // Recursively searches tree for item.
  5. // Post: If there is an element someItem whose key matches item's,
  6. //       found is true and item is set to a copy of someItem;
  7. //       otherwise found is false and item is unchanged.
  8. {
  9. //int counter=0;
  10. if (tree == NULL)
  11. {
  12. found = false;                     // item is not found.
  13. return num=0;
  14. }
  15. else if (item < tree->info)
  16. {
  17. Retrieve(tree->left, item, found, num); // Search left subtree.
  18. num=num+1;
  19. }
  20. else if (item tree->info)
  21. {
  22. Retrieve(tree->right, item, found, num);// Search right subtree.
  23. num = num+1;
  24. }
  25. else
  26. {
  27. item = tree->info;                 // item is found.
  28. found = true;
  29.  
  30.  
  31. }
  32.  
  33.  
  34. }
  35.  
Thanks in advance.
Mar 5 '08 #1
0 1157

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rasmus | last post by:
Hi. As partly novice in python I would like a piece of advise of how to implement (binary) trees the best way? Thanks in advance, Rasmus PS: Due to heavy spam reception (20.000+/week), I...
3
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = =="...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
8
by: sudharsan | last post by:
please gimme the logic to merge two binary search trees?I mean which node has to be the root node of the new binary tree?? Thanks in advance
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
2
by: pyguy | last post by:
Hi all, I am running into a conceptual glitch in implementing a simple binary tree class. My insertion and printing (sorting) seems to be ok, but when I search the tree, my find method isn't doing...
5
by: maxim.novak | last post by:
Hi, I have to get list of URLs one by one and to find the URLs that I have more than one time(can't be more than twice). I thought to put them into binary search tree, this way they'll be...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
1
by: mark | last post by:
I have a 5x5 Matrix. Each cell consists a string value(ex:URL of a website) I want to find out which string value occurs( i.e. repeats ) maximum no. of times in the matrix. I have a hint...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.