打印二叉树——顺序、分层、之字形

本文介绍了三种不同的二叉树打印方法:顺序打印、分层打印和之字形打印。通过示例代码详细展示了如何实现这三种方法,并提供了测试用例来验证打印效果。

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

#include <iostream>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>

using namespace std;

struct BinaryTreeNode{
    int val;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
    BinaryTreeNode(int x):val(x),left(NULL),right(NULL){}
};
///      8
///    /   \
///   6    10
///  / \   / \
/// 5  7  9  11

void sequencePrint(BinaryTreeNode* pRoot);
void tabPrint(BinaryTreeNode* pRoot);
void zhiPrint(BinaryTreeNode* pRoot);
void testBinaryPrint();

///前序顺序打印二叉树
/// 8 6 10 5 7  9 11
void sequencePrint(BinaryTreeNode* pRoot)
{
    if(NULL == pRoot)
        return;


    queue<BinaryTreeNode*> nodes;
    nodes.push(pRoot);
    while(!nodes.empty())
    {
        BinaryTreeNode* pNode = nodes.front();
        cout<<pNode->val<<" ";
        if(NULL != pNode->left)
            nodes.push(pNode->left);
        if(NULL != pNode->right)
            nodes.push(pNode->right);


        nodes.pop();
    }
    cout<<endl;
}
///分行打印二叉树
/// 8
/// 6 10
/// 5 7  9 11
void tabPrint(BinaryTreeNode* pRoot)
{
    if(NULL == pRoot)
        return;
    queue<BinaryTreeNode*> node;
    node.push(pRoot);
    int nextLevel=0,toBeprinted=1;
    while(!node.empty())
    {
        BinaryTreeNode* pNode = node.front();
        cout<<pNode->val<<" ";
        if(NULL != pNode->left)
        {
            node.push(pNode->left);
            nextLevel++;
        }
        if(NULL != pNode->right)
        {
            node.push(pNode->right);
            nextLevel++;
        }
        node.pop();
        toBeprinted--;
        if(0 == toBeprinted)
        {
            cout<<endl;
            toBeprinted = nextLevel;
            nextLevel = 0;
        }
    }
    cout<<endl;
}
/// 之字型打印二叉树
/// 8
/// 10 6
/// 5  7  9 11
void zhiPrint(BinaryTreeNode* pRoot)
{
    stack<BinaryTreeNode*> nodes[2];
    int current=0;///current = 0/1——>既可以控制栈,也可以控制行数的切换
    nodes[current].push(pRoot);
    while(!nodes[0].empty() || !nodes[1].empty())
    {
        BinaryTreeNode* pNode = nodes[current].top();
        cout<<pNode->val<<" ";
        nodes[current].pop();
        if(0==current)
        {
            if(NULL != pNode->left)
                nodes[1-current].push(pNode->left);
            if(NULL != pNode->right)
                nodes[1-current].push(pNode->right);
        }
        else
        {
            if(NULL != pNode->right)
                nodes[1-current].push(pNode->right);
            if(NULL != pNode->left)
                nodes[1-current].push(pNode->left);
        }
        if(nodes[current].empty())
        {
            cout<<endl;
            current = 1-current;
        }
    }
    cout<<endl;
}
void testBinaryPrint()
{
    BinaryTreeNode* pNode8 = new BinaryTreeNode(8);
    BinaryTreeNode* pNode6 = new BinaryTreeNode(6);
    BinaryTreeNode* pNode10 = new BinaryTreeNode(10);
    BinaryTreeNode* pNode5 = new BinaryTreeNode(5);
    BinaryTreeNode* pNode7 = new BinaryTreeNode(7);
    BinaryTreeNode* pNode9 = new BinaryTreeNode(9);
    BinaryTreeNode* pNode11 = new BinaryTreeNode(11);

    pNode10->left = pNode9;
    pNode10->right = pNode11;
    pNode6->left = pNode5;
    pNode6->right = pNode7;
    pNode8->left = pNode6;
    pNode8->right = pNode10;

    cout<<"需要打印的二叉树:"<<endl;
    cout<<"     8"<<endl;
    cout<<"   /   \\"<<endl;
    cout<<"  6    10"<<endl;
    cout<<" / \\   / \\"<<endl;
    cout<<"5  7  9  11"<<endl;
    cout<<"前序顺序打印二叉树"<<endl;
    sequencePrint(pNode8);
    cout<<"分层分行打印二叉树"<<endl;
    tabPrint(pNode8);
    cout<<"之字形打印二叉树"<<endl;
    zhiPrint(pNode8);
}
int main()
{
    ///测试打印二叉树
    testBinaryPrint();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值