zoukankan      html  css  js  c++  java
  • 从上往下打印二叉树

    题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印。

    思路:每一次打印一个结点的时候,如果该结点有子结点,则把该结点的子结点放到一个队列的末尾。接下来到队列的头部取出最早进入队列的结点,重复前面的打印操作,直至队列中所有的结点都被打印出来为止。

      1 #include "stdafx.h"
      2 #include<stdio.h>
      3 #include<deque>
      4 #include<tchar.h>
      5 
      6 struct BinaryTreeNode
      7 {
      8     int              m_nValue;
      9     BinaryTreeNode*  m_pLeft;
     10     BinaryTreeNode*  m_pRight;
     11 };
     12 
     13 BinaryTreeNode* CreateBinaryTreeNode(int value)
     14 {
     15     BinaryTreeNode* pNode = new BinaryTreeNode();
     16     pNode->m_nValue = value;
     17     pNode->m_pLeft = NULL;
     18     pNode->m_pRight = NULL;
     19 }
     20 
     21 void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, 
     22                     BinaryTreeNode* pRight)
     23 {
     24     if(pParent != NULL)
     25     {
     26         pParent->m_pLeft = pLeft;
     27         pParent->m_pRight = pRight;
     28     }
     29 }
     30 
     31 void PrintTreeNode(BinaryTreeNode* pNode)
     32 {
     33     if(pNode != NULL)
     34     {
     35         printf("value of this node is: %d
    ", pNode->m_nValue);
     36         
     37         if(pNode->m_pLeft != NULL)
     38             printf("value of its left child is: %d.
    ", pNode->m_pLeft->m_nValue);
     39         else
     40             printf("left child is null.
    ");
     41         
     42         if(pNode->m_pRight != NULL)
     43             printf("value of its right child is: %d.
    ",pNode->m_pRight->m_nValue);
     44         else
     45             printf("right child is null.
    ");
     46     }
     47     else
     48     {
     49         printf("this node is null.
    ");
     50     }
     51     printf("
    ");
     52 }
     53 
     54 void PrintTree(BinaryTreeNode* pRoot)
     55 {
     56     PrintTreeNode(pRoot);
     57     
     58     if(pRoot != NULL)
     59     {
     60         if(pRoot->m_pLeft != NULL)
     61             PrintTree(pRoot->m_pLeft);
     62         
     63         if(pRoot->m_pRight != NULL) 
     64             PrintTree(pRoot->m_pRight);
     65     }
     66 }
     67 
     68 void DestroyTree(BinaryTreeNode* pRoot)
     69 {
     70     if(pRoot != NULL)
     71     {
     72         BinaryTreeNode* pLeft = pRoot->m_pLeft;
     73         BinaryTreeNode* pRight = pRoot->m_pRight;
     74         
     75         delete pRoot;
     76         pRoot = NULL;
     77         
     78         DestroyTree(pLeft);
     79         DestroyTree(pRight);
     80     }
     81 }
     82 
     83 
     84 void PrintFromTopToBottom(BinaryTreeNode* pRoot)
     85 {
     86     if(pRoot == NULL)
     87         return;
     88         
     89     std::deque<BinaryTreeNode *> dequeTreeNode;
     90     
     91     dequeTreeNode.push_back(pRoot);
     92     
     93     while(dequeTreeNode.size())
     94     {
     95         BinaryTreeNode *pNode = dequeTreeNode.front();
     96         dequeTreeNode.pop_front();
     97         
     98         printf("%d ", pNode->m_nValue);
     99         
    100         if(pNode->m_pLeft)
    101             dequeTreeNode.push_back(pNode->m_pLeft);
    102         
    103         if(pNode->m_pRight)
    104             dequeTreeNode.push_back(pNode->m_pRight);
    105     }
    106 }
    107 
    108 //            10
    109 //         /      
    110 //        6        14
    111 //       /        /
    112 //      4  8     12  16
    113 
    114 int main()
    115 {
    116     BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    117     BinaryTreeNode* pNode6  = CreateBinaryTreeNode(6);
    118     BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
    119     BinaryTreeNode* pNode4  = CreateBinaryTreeNode(4);
    120     BinaryTreeNode* pNode8  = CreateBinaryTreeNode(8);
    121     BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
    122     BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16);
    123     
    124     ConnectTreeNodes(pNode10, pNode6, pNode14);
    125     ConnectTreeNodes(pNode6, pNode4, pNode8);
    126     ConnectTreeNodes(pNode14, pNode12, pNode16);
    127     
    128     PrintTree(pNode10);
    129     
    130     printf("The nodes from top to bottom, from left to right are: 
    ");
    131     PrintFromTopToBottom(pNode10);
    132     
    133     printf("
    
    ");
    134 }

  • 相关阅读:
    深入理解计算机系统 第六章 存储器层次结构 第二遍
    深入理解计算机系统 第六章 存储器层次结构
    深入理解计算机系统 第八章 异常控制流 Part2 第二遍
    深入理解计算机系统 第八章 异常控制流 part2
    深入理解计算机系统 第八章 异常控制流 Part1 第二遍
    深入理解计算机系统 第八章 异常控制流 part1
    深入理解计算机系统 第三章 程序的机器级表示 Part2 第二遍
    深入理解计算机系统 第三章 程序的机器级表示 part2
    深入理解计算机系统 第三章 程序的机器级表示 Part1 第二遍
    深入理解计算机系统 第三章 程序的机器级表示 part1
  • 原文地址:https://www.cnblogs.com/sankexin/p/5620441.html
Copyright © 2011-2022 走看看