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.
....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; }
Comment