Question & Answer: Using the examples in Figures 24-6 and 24-7 to suggest algorithms, implement iterator classes fo…..

1) can any one give me solution for this pleaseee

Using the examples in Figures 24-6 and 24-7 to suggest algorithms, implement iterator classes for preorder,
postorder, and level-order traversals of a binary tree.Begin by writing iterative versions of the traversals similar to the method interativeInorderTraverse given in segment 24.13

FIGURE 24-6 Using a stack to traverse a binary tree in (a) preorder; (b) postorder (aTraversal order a Stack after cach puslh or pop (b) Traversal order Stack after each puslh or pop

FIGURE 24-6 Using a stack to traverse a binary tree in (a) preorder; (b) postorder (aTraversal order a Stack after cach puslh or pop (b) Traversal order Stack after each puslh or pop

Expert Answer

 

It is a simple algorithmic implementation

// class stack has four operation: 1. push( T x)

// 2. pop()

// 3. top()

// 4. size()

// class queue has four operation: 1. push( T x)

// 2. pop()

// 3. top()

// 4. size()

// treenode{

// int val;

// treenode* left,right;

//}

class BinaryTree{

queue<treenode> q;

class preorder{

stack<treenode> s;

public:

preorder(){

this->s.push(treehead);

}

preorder(treenode* node){

this->s.push(node);

}

boolean hasNext(){

if(this->s.size()==0)

return false;

else

return true;

}

E Next(){

if(this->s.size() == 0)

throw(error);

else

{

treenode* node=this->s.top();

this->s.pop();

if(node->right!=null)

this->s.push(node->right);

if(node->left!=null)

this->s.push(node->left);

return node;

}

}

void remove(){

if(this->s.size)

}

}

class postorder{

stack<treenode> s;

public:

postorder(){

treenode* cur=treehead;

if(treehead!=null)

this->s.push(treehead);

cur=this->s.top();

while(cur-left!=null&&cur->right!=null){

if(cur->right!=null){

this->s.push(cur->left)

}

if(cur->left!=null){

this->s.push(cur->left)

}

cur=this->s.top();

}

}

postorder(treenode* node){

this->s.push(node);

cur=this->s.top();

while(cur-left!=null&&cur->right!=null){

if(cur->right!=null){

this->s.push(cur->left)

}

if(cur->left!=null){

this->s.push(cur->left)

}

cur=this->s.top();

}

}

boolean hasNext(){

if(this->s.size()==0)

return false;

else

return true;

}

E Next(){

if(this->s.size() == 0)

throw(error);

else

{

treenode* node = this->s.top();

this->s.pop();

cur=this->s.top();

if(cur->left!=node && cur->right!=node){

while(cur-left!=null && cur->right!=null){

if(cur->right!=null){

this->s.push(cur->left)

}

if(cur->left!=null){

this->s.push(cur->left)

}

cur=this->s.top();

}

}

}

return node;

}

}

class levelorder{

queue<treenode> q;

public:

preorder(){

this->q.push(treehead);

}

preorder(treenode* node){

this->q.push(node);

}

boolean hasNext(){

if(this->q.size()==0)

return false;

else

return true;

}

E Next(){

if(this->q.size() == 0)

throw(error);

else

{

treenode* node=this->q.top();

this->q.pop();

if(node->right!=null)

this->q.push(node->right);

if(node->left!=null)

this->q.push(node->left);

return node;

}

}

void remove(){

if(this->q.size()){

this->q.pop();

}

}

}

}

Still stressed from student homework?
Get quality assistance from academic writers!