程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 從上往下打印二叉樹,往下打印二叉樹

從上往下打印二叉樹,往下打印二叉樹

編輯:C++入門知識

從上往下打印二叉樹,往下打印二叉樹


題目:從上往下打印出二叉樹的每個結點,同一層的結點按照從左到右的順序打印。

思路:每一次打印一個結點的時候,如果該結點有子結點,則把該結點的子結點放到一個隊列的末尾。接下來到隊列的頭部取出最早進入隊列的結點,重復前面的打印操作,直至隊列中所有的結點都被打印出來為止。

  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\n", pNode->m_nValue);
 36         
 37         if(pNode->m_pLeft != NULL)
 38             printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
 39         else
 40             printf("left child is null.\n");
 41         
 42         if(pNode->m_pRight != NULL)
 43             printf("value of its right child is: %d.\n",pNode->m_pRight->m_nValue);
 44         else
 45             printf("right child is null.\n");
 46     }
 47     else
 48     {
 49         printf("this node is null.\n");
 50     }
 51     printf("\n");
 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: \n");
131     PrintFromTopToBottom(pNode10);
132     
133     printf("\n\n");
134 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved