把二叉树打印成多行
来源:互联网 发布:手机淘宝怎么联系客服 编辑:程序博客网 时间:2024/05/29 07:07
问题:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印到一行。例如:
打印结果:
8
6 10
5 7 9 11
分析:为了把二叉树的每一行单独打印到一行里,我们需要两个变量:一个变量表示在当前层中还没有打印的结点数,另一个变量表示下一层结点的数目。
//按照行打印二叉树void Print(BinaryTreeNode* pRoot){ if(pRoot == NULL) return; std::queue<BinaryTreeNode*> nodes; nodes.push(pRoot); int nextLevel = 0; int toBePrinted = 1; while(!nodes.empty()) { BinaryTreeNode* pNode = nodes.front(); printf("%d ", pNode->m_nValue); if(pNode->m_pLeft != NULL) { nodes.push(pNode->m_pLeft); ++nextLevel; } if(pNode->m_pRight != NULL) { nodes.push(pNode->m_pRight); ++nextLevel; } nodes.pop(); --toBePrinted; if(toBePrinted == 0) { printf("\n"); toBePrinted = nextLevel; nextLevel = 0; } }}// ==================== Test Code ====================// 8// 6 10// 5 7 9 11void Test1(){ BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); ConnectTreeNodes(pNode8, pNode6, pNode10); ConnectTreeNodes(pNode6, pNode5, pNode7); ConnectTreeNodes(pNode10, pNode9, pNode11); printf("====Test1 Begins: ====\n"); printf("Expected Result is:\n"); printf("8 \n"); printf("6 10 \n"); printf("5 7 9 11 \n\n"); printf("Actual Result is: \n"); Print(pNode8); printf("\n"); DestroyTree(pNode8);}// 5// 4// 3// 2void Test2(){ BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); ConnectTreeNodes(pNode5, pNode4, NULL); ConnectTreeNodes(pNode4, pNode3, NULL); ConnectTreeNodes(pNode3, pNode2, NULL); printf("====Test2 Begins: ====\n"); printf("Expected Result is:\n"); printf("5 \n"); printf("4 \n"); printf("3 \n"); printf("2 \n\n"); printf("Actual Result is: \n"); Print(pNode5); printf("\n"); DestroyTree(pNode5);}// 5// 4// 3// 2void Test3(){ BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); ConnectTreeNodes(pNode5, NULL, pNode4); ConnectTreeNodes(pNode4, NULL, pNode3); ConnectTreeNodes(pNode3, NULL, pNode2); printf("====Test3 Begins: ====\n"); printf("Expected Result is:\n"); printf("5 \n"); printf("4 \n"); printf("3 \n"); printf("2 \n\n"); printf("Actual Result is: \n"); Print(pNode5); printf("\n"); DestroyTree(pNode5);}void Test4(){ BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); printf("====Test4 Begins: ====\n"); printf("Expected Result is:\n"); printf("5 \n\n"); printf("Actual Result is: \n"); Print(pNode5); printf("\n"); DestroyTree(pNode5);}void Test5(){ printf("====Test5 Begins: ====\n"); printf("Expected Result is:\n"); printf("Actual Result is: \n"); Print(NULL); printf("\n");}// 100// /// 50 // \// 150void Test6(){ BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100); BinaryTreeNode* pNode50 = CreateBinaryTreeNode(50); BinaryTreeNode* pNode150 = CreateBinaryTreeNode(150); ConnectTreeNodes(pNode100, pNode50, NULL); ConnectTreeNodes(pNode50, NULL, pNode150); printf("====Test6 Begins: ====\n"); printf("Expected Result is:\n"); printf("100 \n"); printf("50 \n"); printf("150 \n\n"); printf("Actual Result is: \n"); Print(pNode100); printf("\n");}int main(int argc, char* argv[]){ Test1(); Test2(); Test3(); Test4(); Test5(); Test6();return 0;}
0 0
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- 把二叉树打印成多行
- C#数据库的使用
- Intellj Idea设置取消显示形参名提示
- 安鑫 23岁,我们第一次失业
- 【bzoj3224】普通平衡树 平衡树
- C#学习日记2016-12-23 Form窗口最大化时,控件相对位置变化的问题
- 把二叉树打印成多行
- 模拟设计存储管理的分配与回收实践说明
- ofbiz的form查询功能关键代码
- 【NOIP2016普及组】复赛——魔法阵
- 又一大动作,海致BDP似卷收购风波
- 工具总结
- linux下.swp文件是什么?
- vs2012+opencv,完成通用配置和测试离散傅里叶变换
- xcode使用过程中遇到的问题丨工具