473,396 Members | 1,754 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,396 software developers and data experts.

5 erroer in level by level func

6
Hi, i made a display()function that uses level-order traversal of the tree to display nodes level-by-level.I was told that I needed to put a queue class in my project. the hint was to put the queue in the root, do regular traversal but when we meet the marker, get() the marker from the queue and break the line. Thanks, I need to get the something like this in my output
....40
..30..50
20.35.45.55
but I am getting 5 errors, don't know why? line #49 says left is not a member of st::node. line# 52 says left of '->left' must point to class/struct/union/generic type. line #55 says error 'right' : is not a member of 'ST::node'. line#58 says error left of '->right' must point to class/struct/union/generic type. line #61 says error left of '->item' must point to class/struct/union/generic type.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<queue>
  3. using namespace std;
  4.  
  5. class ST
  6. {
  7. private:
  8.     struct node
  9.     {
  10.     int item;
  11.     node *l, *r;
  12.     node(int x)
  13.     {
  14.         item = x; l = 0; r = 0; }
  15.     };
  16.  
  17. typedef node *link;
  18. link head;
  19.  
  20. int searchR(link h, int v)
  21. {    if (h == 0) return -55;
  22.     int t = h->item;
  23.     if (v == t) return h->item;
  24.     if (v < t) return searchR(h->l, v); //looking in the left subtree
  25.     else return searchR(h->r, v); // looking in the right subtree
  26. }
  27.  
  28. void insertR(link& h, int x)
  29. {
  30.     if (h == 0)
  31.     {
  32.     h = new node(x);
  33.     return;
  34.     }
  35.     if (x < h->item)
  36.     insertR(h->l, x);
  37.     else insertR(h->r, x);
  38. }
  39.  
  40. //display function using level-by-level
  41. void display(node *link)
  42. {
  43.     queue<node*> q;
  44.     int currentCount = 0, countInLine = 1, countInNextLine = 0;
  45.     q.push(link);
  46.     while (! q.empty() )
  47.     {
  48.         currentCount++;
  49.         if (q.front()->left != 0)        //error number 1
  50.         {
  51.             countInNextLine++;
  52.             q.push(q.front->left);       //error number 2
  53.         }
  54.  
  55.         if (q.front()->right != 0)         //error number 3
  56.         {
  57.             countInNextLine++;
  58.             q.push(q.front->right);          //error number 4
  59.         }
  60.  
  61.         cout << q.pop()->item;          //error number 5
  62.  
  63.         if (currentCount == countInLine)
  64.         {
  65.             cout << endl;
  66.             currentCount = 0;
  67.             countInLine = countInNextLine;
  68.             countInNextLine = 0;
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. public:
  75.     ST()
  76.     { head = 0; }
  77.     int search(int v)
  78.     { return searchR(head, v); }
  79.     void insert(int x)
  80.     { insertR(head, x); }
  81.  
  82.     void levelOrder()
  83.     { display(head); }
  84.  
  85. };
  86.  
  87. int main()
  88. {
  89.     ST tree;
  90.     char oneMore;
  91.     int num;
  92.     do{
  93.         cout<<"Enter an integer:";
  94.         cin>>num;
  95.         tree.insert(num);
  96.         cout<<"Enter 'y' to enter another integer:";
  97.         cin>>oneMore;
  98.     }while(oneMore== 'y');
  99.     tree.traverse();
  100.  
  101.     tree.levelOrder();
  102.  
  103.  
  104.     system("pause");
  105.     return 0;
  106. }
Jun 19 '09 #1
4 1602
donbock
2,426 Expert 2GB
There should have been a line number associated with each error message. Please provide those line numbers, being careful to translate each so it corresponds to the line numbers in the code segment your provided in your post.

That is, the line number reported by the compiler might be 250, but because you only posted a portion of the source file, that same line is identified as 23 in your code snippet. To avoid misunderstanding, you should state that the line numbers were translated.
Jun 19 '09 #2
star33
6
okay, i just edit it, so I hope u can tell me my errors or how to fix them. thanks
Jun 19 '09 #3
Banfa
9,065 Expert Mod 8TB
Just as your error messages say, your structure node does not contain left and right items. It does contain l and r items is that what you meant to use?

For the 5th error pop returns void. It can not be used in the context you have it in. I sugggest you read the documentation on queue<> and re-write that section of code using front and pop.
Jun 20 '09 #4
star33
6
thanks banfa, I got it to work, u were right!
Jun 20 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Thomas Guettler | last post by:
Hi! I use xemacs. Is it possible to show the next higher level in the status bar? Up to now it looks like this: Noconv-----XEmacs: foo.py (Python Font)----L1177-C0--79%--- It would be...
90
by: Mark Hahn | last post by:
"Michael Geary" <Mike@Geary.com> wrote ... >Does anyone have some sample code where obj$func() would be used? > (Apologies if I missed it.) There have been so many messages about delegation...
3
by: Danny Shevitz | last post by:
Howdy, I am trying to call class methods that have been created via a "type" function. I have enclosed a simplified example that shows what I am trying to do. In particular I am calling from the...
3
by: Jason | last post by:
Hi, Im running windows xp pro and compiling using dev c++ 4. I have the following situation: #include <iostream> #include <string> using namespace std; int main() {
5
by: modemer | last post by:
I saw someone use the following code: void func(MyClass *& myCls) { myCls->test(); } // call func(): func(new MyClass);
32
by: Robin Becker | last post by:
Is there some smart/fast way to flatten a level one list using the latest iterator/generator idioms. The problem arises in coneverting lists of (x,y) coordinates into a single list of...
2
by: robert | last post by:
When employing complex UI libs (wx, win32ui, ..) and other extension libs, nice "only Python stack traces" remain a myth. Currently I'm hunting again a rare C-level crash bug of a Python based...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.