5 erroer in level by level func

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • star33
    New Member
    • Jun 2009
    • 6

    5 erroer in level by level func

    Hi, i made a display()functi on 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.

    Code:
    #include<iostream>
    #include<queue>
    using namespace std;
    
    class ST
    {
    private:
        struct node
        {
        int item;
        node *l, *r;
        node(int x)
        {
            item = x; l = 0; r = 0; }
        };
    
    typedef node *link;
    link head;
    
    int searchR(link h, int v)
    {    if (h == 0) return -55;
        int t = h->item;
        if (v == t) return h->item;
        if (v < t) return searchR(h->l, v); //looking in the left subtree
        else return searchR(h->r, v); // looking in the right subtree
    }
    
    void insertR(link& h, int x)
    {
        if (h == 0)
        {
        h = new node(x);
        return;
        }
        if (x < h->item)
        insertR(h->l, x);
        else insertR(h->r, x);
    }
    
    //display function using level-by-level
    void display(node *link)
    {
        queue<node*> q;
        int currentCount = 0, countInLine = 1, countInNextLine = 0;
        q.push(link);
        while (! q.empty() )
        {
            currentCount++;
            if (q.front()->left != 0)        //error number 1
            {
                countInNextLine++;
                q.push(q.front->left);       //error number 2
            }
           
            if (q.front()->right != 0)         //error number 3
            {
                countInNextLine++;
                q.push(q.front->right);          //error number 4
            }
           
            cout << q.pop()->item;          //error number 5
           
            if (currentCount == countInLine)
            {
                cout << endl;
                currentCount = 0;
                countInLine = countInNextLine;
                countInNextLine = 0;
            }
        }
    }
    
    
    public:
        ST()
        { head = 0; }
        int search(int v)
        { return searchR(head, v); }
        void insert(int x)
        { insertR(head, x); }
       
        void levelOrder()
        { display(head); }
       
    };
    
    int main()
    {
        ST tree;
        char oneMore;
        int num;
        do{
            cout<<"Enter an integer:";
            cin>>num;
            tree.insert(num);
            cout<<"Enter 'y' to enter another integer:";
            cin>>oneMore;
        }while(oneMore== 'y');
        tree.traverse();
    
        tree.levelOrder();
       
    
        system("pause");
        return 0;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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 misunderstandin g, you should state that the line numbers were translated.

    Comment

    • star33
      New Member
      • Jun 2009
      • 6

      #3
      okay, i just edit it, so I hope u can tell me my errors or how to fix them. thanks

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        • star33
          New Member
          • Jun 2009
          • 6

          #5
          thanks banfa, I got it to work, u were right!

          Comment

          Working...