//前序遍历
void former_traverse(BTree *root)
{
Stack s;
BTree *p = root;
while (p != NULL | !s.empty())
{
while (p != NULL)
{
cout << p->data << endl;
s.push(p);
p = p->left;
}
if (!s.empty())
{
p = p.top();
p.pop();
p = p->right();
}
}
}
//中序遍历
void middle_traverse(BTree *root)
{
Stack s;
BTree *p = root;
while (p != NULL | !s.empty())
{
while (p != NULL)
{
s.push(p);
p = p->left;
}
if (!s.empty())
{
p = s.pop();
cout << p->data << endl;
s.pop();
p = p->right();
}
}
}
//后序遍历
void last_traverse(BTree *root)
{
Stack s;
BTree *p = root;
BTNode *temp = NULL;
while (p != NULL | !s.empty())
{
while (p != NULL)
{
temp = (BTNode *)malloc(sizeof(BTNode));
temp->btnode = p;
temp->isFirst = true;
s.push(temp);
p = p->left;
}
if (!s.empty())
{
temp = s.top();
s.pop();
if (temp->isFirst == true)
{
temp->isFirst = false;
s.push(temp);
p = temp->btnode->right;
}
else
{
cout << temp->btnode->datae;
p = NULL;
}
}
}
}
阅读(570) | 评论(0) | 转发(0) |