Home > Back-end >  Help: non-recursive traversing binary tree, why run results is the stack is full???????
Help: non-recursive traversing binary tree, why run results is the stack is full???????

Time:09-29

#include
using namespace std;
#include
Template
The class SeqStack
{
Private:
T data [100];
Int top;
Public:
SeqStack (void)
{
Top=0;
};
~ SeqStack (void) {};
Void Push (const T& Item)
{
If (top==100)
{
Cout & lt; <"The stack is full" & lt; exit(0);
}
Data [top]=item;
Top++;
}
T Pop (void)
{
If (top==0)
{
Cout & lt; <"The stack is empty" & lt; exit(0);
}
Top -;
The return data [top];
}
T GetTop (void) const
{
If (top==0)
{
Cout & lt; <"The stack is empty" & lt; exit(0);
}
The return data [top - 1);
}
Int NotEmpty (void) const
{
Return (top!=0);
}
};
Template
The class BiTreeNode
{
Private:
BiTreeNode * leftChild;
BiTreeNode * rightChild;
Public:
T data;
BiTreeNode (T item, BiTreeNode * left=NULL, BiTreeNode * right=NULL), data (item), leftChild (left), rightChild (right) {}
~ BiTreeNode () {}
BiTreeNode * & amp; The Left () {return leftChild; }
BiTreeNode * & amp; Right () {return rightChild; }
};
Template
BiTreeNode * GetTreeNode (const T item, BiTreeNode * left=NULL, BiTreeNode * right=NULL)
{
BiTreeNode * p;
P=new BiTreeNode (item, left, right);
return p;
}
Template
Void PreOrder (BiTreeNode * t, void Visit data) (t)
{
SeqStack The stack;
Stack. Push (t - & gt; The data);
While (stack. NotEmpty ())
{
If (t - & gt; Right ()!=NULL)
Stack. Push (t - & gt; Right () - & gt; The data);
If (t - & gt; The Left ()!=NULL)
Stack. Push (t - & gt; The Left () - & gt; The data);
}
}
Template
Void Visit (T data) {cout & lt; Void MakeCharTree (BiTreeNode * & amp; Root)
{
BiTreeNode C, *, b * * d * e, f, * g * null=null;
G=GetTreeNode (" g ");
D=GetTreeNode (' d ', null, g);
B=GetTreeNode (' b, d);
E=GetTreeNode (' e ');
F=GetTreeNode (" f ");
C=GetTreeNode (' c, e, f);
Root=GetTreeNode (' A ', b, c);
}
Int main ()
{
BiTreeNode * root1;
MakeCharTree (root1);
PreOrder (root1, Visit);
return 0;
}

CodePudding user response:

 
While (stack. NotEmpty ())//stack, Push, will never be empty,
{
If (t - & gt; Right ()!=NULL)//t never change
Stack. Push (t - & gt; Right () - & gt; The data);
If (t - & gt; The Left ()!=NULL)
Stack. Push (t - & gt; The Left () - & gt; The data);
}

CodePudding user response:

Really thank you very much

CodePudding user response:

reference 1st floor GKatHere response:
 
While (stack. NotEmpty ())//stack, Push, will never be empty,
{
If (t - & gt; Right ()!=NULL)//t never change
Stack. Push (t - & gt; Right () - & gt; The data);
If (t - & gt; The Left ()!=NULL)
Stack. Push (t - & gt; The Left () - & gt; The data);
}


Could you tell me how to modify should be
  • Related