数据结构之二叉树

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef struct tnode TN;
typedef struct tnode* TT;
struct tnode{
    int data;
    TT left,right;
};
void  creatree(TT &k)            //前序遍历建立二叉树
{
    int data1;
    printf("请输入该分支的头节点:");
    cin >> data1;
    if (data1 ==-1)
        k = NULL;
    else{
        k = (TT)malloc(sizeof(TN));
        k->data = data1;
        creatree(k->left);
        creatree(k->right);
    }
}
void preorder(TT root)          //前序遍历
{
    if (root){
        printf("%d ", root->data);
        preorder(root->left);
        preorder(root->right);
    }
}
四种非递归遍历方法

void preorder2(TT root)                        //前序遍历
{
    stack<TT> sk;
    TT wk = root;
    while (wk || !sk.empty())
    {
        while (wk)                         //直到左子树为空的时候
        {
            sk.push(wk);
            cout << wk->data << " ";
            wk = wk->left;                
        }
        if (!sk.empty())
        wk= sk.top()->right; sk.pop();        //为空时栈顶为最短父节点,遍历该父节点的右子树
    }
}
void midorder(TT root)                            //中序遍历
{
    TT wk = root;
    stack<TT> sk;
    while (wk||!sk.empty())
    {
        while (wk)
        {
            sk.push(wk);
            wk = wk->left;
        }
        if (!sk.empty())
        {
            wk = sk.top(); sk.pop();
            cout << wk->data<<" ";
            wk = wk->right;
        }
    }
}
void postorder(TT root)                //后序遍历
{
    stack<TT> sk;
    TT wk = root;                         //工作指针
    TT r = NULL;                        //标志指针
    while (wk || !sk.empty())
    {
        if (wk){                         //wk非空
            sk.push(wk);
            wk = wk->left;
        }
        else
        {
            wk = sk.top();
            if (wk->right&&wk->right != r)      //右子树存在且没有被访问过
            {
                wk = wk->right;
            }
            else
            {
                cout << wk->data << " ";
                sk.pop();
                r = wk;                              //表示该节点已经访问
                wk = NULL;                           //下一步要么结束要么wk等于栈顶元素继续访问左子树或者右子树---所以令其为空
            }
        }
    }
}

void levelorder(TT root)                                //层次遍历
{
    queue<TT> sk;                                   //由层次遍历的特点:左子树先访问,而且左子树的孩子也先访问
    TT wk = root;
    sk.push(wk);
    while (!sk.empty())
    {
        wk = sk.front();
        cout <<wk->data << " ";
        if (wk->left != NULL)sk.push(wk->left);
        if (wk->right != NULL)sk.push(wk->right);
        sk.pop();
    }
}
int main()
{
    TT st=NULL;
    cout << "前序遍历建立二叉树:"<<endl;
    creatree(st);
    cout << "前序遍历1:" << endl;
    preorder(st);
    cout << endl;
    cout << "前序遍历2:" << endl;
    preorder2(st);
    cout << endl;
    cout << "中序遍历:" << endl;
    midorder(st);
    cout << endl;
    cout << "后序遍历:" << endl;
    postorder(st);
    cout << endl;
    cout << "层次遍历:" << endl;
    levelorder(st);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值