剑指offer第二十三题:从上往下打印二叉树
1 //============================================================================ 2 // Name : JZ-C-23.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 从上往下打印二叉树 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 #include <deque> 12 #include "BinaryTree.h" 13 using namespace std; 14 /** 15 *层次遍历二叉树,相当于广度优先遍历,都需要利用队列 16 */ 17 void PrintFromTopToBottom(BinaryTreeNode* pTreeRoot){ 18 if(pTreeRoot == NULL){ 19 return; 20 } 21 std::deque<BinaryTreeNode*> dequeTreeNode;//这是一个两端都可以进出的队列★ 22 dequeTreeNode.push_back(pTreeRoot);//从后进队列 23 while(!dequeTreeNode.empty()){ 24 BinaryTreeNode* Node = dequeTreeNode.front();//从前出队列 25 dequeTreeNode.pop_front(); 26 cout<<Node->m_nValue<<endl;//打印 27 if(Node->m_pLeft){ 28 dequeTreeNode.push_back(Node->m_pLeft); 29 } 30 if(Node->m_pRight){ 31 dequeTreeNode.push_back(Node->m_pRight); 32 } 33 } 34 } 35 36 // ====================测试代码==================== 37 void Test(char* testName, BinaryTreeNode* pRoot) 38 { 39 if(testName != NULL) 40 printf("%s begins: ", testName); 41 42 PrintTree(pRoot); 43 44 printf("The nodes from top to bottom, from left to right are: "); 45 PrintFromTopToBottom(pRoot); 46 47 printf(" "); 48 } 49 50 // 10 51 // / 52 // 6 14 53 // / / 54 // 4 8 12 16 55 void Test1() 56 { 57 BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 58 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 59 BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14); 60 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 61 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 62 BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); 63 BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16); 64 65 ConnectTreeNodes(pNode10, pNode6, pNode14); 66 ConnectTreeNodes(pNode6, pNode4, pNode8); 67 ConnectTreeNodes(pNode14, pNode12, pNode16); 68 69 Test("Test1", pNode10); 70 71 DestroyTree(pNode10); 72 } 73 74 // 5 75 // / 76 // 4 77 // / 78 // 3 79 // / 80 // 2 81 // / 82 // 1 83 void Test2() 84 { 85 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 86 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 87 BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 88 BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 89 BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 90 91 ConnectTreeNodes(pNode5, pNode4, NULL); 92 ConnectTreeNodes(pNode4, pNode3, NULL); 93 ConnectTreeNodes(pNode3, pNode2, NULL); 94 ConnectTreeNodes(pNode2, pNode1, NULL); 95 96 Test("Test2", pNode5); 97 98 DestroyTree(pNode5); 99 } 100 101 // 1 102 // 103 // 2 104 // 105 // 3 106 // 107 // 4 108 // 109 // 5 110 void Test3() 111 { 112 BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 113 BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 114 BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 115 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 116 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 117 118 ConnectTreeNodes(pNode1, NULL, pNode2); 119 ConnectTreeNodes(pNode2, NULL, pNode3); 120 ConnectTreeNodes(pNode3, NULL, pNode4); 121 ConnectTreeNodes(pNode4, NULL, pNode5); 122 123 Test("Test3", pNode1); 124 125 DestroyTree(pNode1); 126 } 127 128 // 树中只有1个结点 129 void Test4() 130 { 131 BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 132 Test("Test4", pNode1); 133 134 DestroyTree(pNode1); 135 } 136 137 // 树中没有结点 138 void Test5() 139 { 140 Test("Test5", NULL); 141 } 142 143 int main(int argc, char** argv) 144 { 145 Test1(); 146 Test2(); 147 Test3(); 148 Test4(); 149 Test5(); 150 151 return 0; 152 }