树的打印方式

本文详细介绍了三种打印二叉树的方法:Z形打印、多行打印、按层打印第k层节点,并提供了相应的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///////////////////////////////////////////////////////////////////////////////////////////////////////
Z行打印二叉树
            5
           / \
          1   8
         /\   /\
        0  2  7 9
            \
             4
输出格式为 :
       5
       8 1
       0 2 7 9
       4
void Z_Print_Tree(Tree *root)
{
    if(root == NULL)
    {
        return ;
    }
    stack<Tree*> sta[2];   //两个栈   sta[0]是放 奇数层的 数字   sta[1]  放偶数层的数字
    sta[0].push(root);
    int ceng = 1;//当前打印的层数
    while(!sta[0].empty() || !sta[1].empty())
    {
        Tree *temp = NULL;
        if(ceng % 2 != 0)        //奇数层
        {
            temp = sta[0].top();    //弹出一个元素
            sta[0].pop();
            cout<<temp->nValue<<" ";
            if(temp->pLeft != NULL)
            {
                sta[1].push(temp->pLeft);   //下一层  为 偶数层  要放到  第二个栈  中
            }
            if(temp->pRight != NULL)
            {
                sta[1].push(temp->pRight);    //下一层  为 偶数层  要放到  第二个栈  中
            } 
        }
        else
        {
            temp = sta[1].top();
            sta[1].pop();
            cout<<temp->nValue<<" ";
            if(temp->pRight != NULL)
            {
                sta[0].push(temp->pRight);
            }
            if(temp->pLeft != NULL)
            {
                sta[0].push(temp->pLeft);
            }
        }
        if(ceng % 2 != 0)     //只有  当前的  层数  打印完  层数  ceng  才会 ++
        {
            if(sta[0].empty())   //奇数层  看  sta[0]  栈  是否  打印完   ,栈 为 空 才会  ceng++
            {
                cout<<endl;
                ceng++;
            }
        }
        if(ceng % 2 == 0)
        {
            if(sta[1].empty())
            {
                cout<<endl;
                ceng++;
            }
        }
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
把二叉树打印成多行
            5
           / \
          1   8
         /\   /\
        0  2  7 9
            \
             4
输出格式为 :
       5
       1 8
       0 2 7 9
       4
void Ceng_Print(Tree *root)
{
    int Next_Node_Sum = 0;//下一层结点的个数
    int To_Be_Print_Sum = 1;//当前层需要打印结点的个数
    queue<Tree*> que;
    que.push(root);
    while(!que.empty())
    {
        Tree *temp = que.front();
        que.pop();
        cout<<temp->nValue<<" ";
        To_Be_Print_Sum--;
        if(temp->pLeft != NULL)
        {
            que.push(temp->pLeft);
            Next_Node_Sum++;
        }
        if(temp->pRight != NULL)
        {
            que.push(temp->pRight);
            Next_Node_Sum++;
        }
        if(To_Be_Print_Sum == 0)
        {
            cout<<endl;
            To_Be_Print_Sum = Next_Node_Sum;
            Next_Node_Sum = 0;
        }
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
打印树的第k层结点
//打印第k层结点顺序的打印
            5
           / \
          1   8
         /\   /\
        0  2  7 9
            \
             4
输出格式为 :
       第0层 5
       第2层 0 2 7 9
void PrintKCeng(Tree *root,int k)
{
    if(root == NULL)
    {
        return;
    }
    if(k == 0)
    {
        cout<<root->nValue<<" ";
        return;
    }
    if(root->pLeft != NULL)
    {
        PrintKCeng(root->pLeft,k-1);
    }
    if(root->pRight != NULL)
    {
        PrintKCeng(root->pRight,k-1);
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

            5
           / \
          1   8
         /\   /\
        0  2  7 9
            \
             4
输出格式为 :
       4
       9 7 2 0
       8 1
       5

void ReversePrintCeng(Tree *root,stack<vector<int> > &sta/*因为当前需要从树的叶子节点输出,所以用栈*/)
{
    if(root == NULL)
    {
        return ;
    }
    vector<int>vec;    //储层当前层数的节点
    queue<Tree*> que;   //通过层遍历 树的 队列
    que.push(root);
    int To_Be_Print = 1;  //当前层需要打印 的 个数
    int Next_Ceng_Sum = 0;  //下一层 需要 打印 的 个数
    while(!que.empty()) // 栈不为空
    {
        Tree *temp = que.front();
        que.pop();
        vec.push_back(temp->nValue);
        To_Be_Print--;       // 需要打印的 个数 --
        if(temp->pRight != NULL)  //因为 当前  需要  从 每一层 的 右边 开始 存入节点 
        {
            que.push(temp->pRight);   // 所以 需要  先判断 右边  是否 有节点  
            Next_Ceng_Sum++;          // 下一层数  的 结点 个数 ++
        }
        if(temp->pLeft != NULL)
        {
            que.push(temp->pLeft);
            Next_Ceng_Sum++;
        }
        if(To_Be_Print == 0)        //如果 当前层数 需要打印的 结点  个数 没有 了 0 个 
        {
            sta.push(vec);            // 将当前  层数 的节点   放到  栈中 
            To_Be_Print = Next_Ceng_Sum;       //更改  需要打印结点的 个数 
            Next_Ceng_Sum = 0;     //重新 置 0
            vec.clear();     //vector  清空
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值