Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346731
  • 博文数量: 135
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1106
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-20 09:56
文章分类

全部博文(135)

文章存档

2017年(3)

2016年(18)

2015年(69)

2014年(39)

2013年(6)

我的朋友

分类: C/C++

2015-11-05 18:45:59

//前序遍历
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;
            }
        }
    }
}
阅读(537) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~