Given a min-heap tree and struct node that has a node pointer to the right child and a node pointer to the left child and a value, implement an iterator class that has 3 main functions; a constructor, a function that gets the next node in a min-heap tree and a function to check if there is some next node to point to.
Anonymous
class Iterator { private: Node * root; queue q; public: Iterator(Node * r): root(r) { if(r != NULL) q.push(r); } boolean hasNext() { return !q.empty(); } Node * next() { Node * n = q.pop(); if(n->left != NULL) q.push(n->left); if(n->right != NULL) q.push(n->right); return n; } }
Check out your Company Bowl for anonymous work chats.