Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void binTree::levelorderIterative()
- {
- std::string slash;
- if(!root)
- throw emptyTree();
- linkedBTNQueue current, next;
- binNode * curr;
- // Push the root onto the current queue
- current.push(root);
- // While the current queue is not empty, loop
- while(!current.isEmpty()){
- // Set the current node to the front element of the current queue
- // Pop the front element of the current queue
- curr = current.front();
- try{
- current.pop();
- }
- catch(linkedBTNQueue::outOfQueueBounds){
- throw linkedBTNQueue::outOfQueueBounds();
- }
- // If curr is not null, print out the data of the current node
- if(curr){
- std::cout << curr->data << ' ';
- // If the left branch of the current node is not null, push the
- // left branch of the node onto the "next" queue
- if(curr->left != NULL){
- next.push(curr->left);
- slash.push_back('/');
- }
- // If the right branch of the current node is not null, push the
- // right branch of the node onto the "next" queue
- if(curr->right != NULL){
- next.push(curr->right);
- slash.push_back('\\');
- }
- }
- // Transfer the elements of the "next" queue to the current queue
- if(current.isEmpty()){
- std::cout << std::endl;
- std::cout << slash << std::endl;
- slash.clear();
- while(!next.isEmpty()){
- try{
- current.push(next.pop());
- }
- catch(linkedBTNQueue::outOfQueueBounds){
- throw linkedBTNQueue::outOfQueueBounds();
- }
- }
- }
- else{
- slash.push_back(' ');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement