博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oral_quiz->#二叉树的境像#
阅读量:6692 次
发布时间:2019-06-25

本文共 5720 字,大约阅读时间需要 19 分钟。

hot3.png

#include 
#include "Utilities/BinaryTree.h"#include
BinaryTreeNode* MyMirrorOfBinaryTree(BinaryTreeNode* root) { if(root == NULL) return NULL; BinaryTreeNode* temp = NULL; temp = MyMirrorOfBinaryTree(root->m_pLeft); root->m_pLeft = MyMirrorOfBinaryTree(root->m_pRight); root->m_pRight = temp; return root;}BinaryTreeNode* MyMirrorIteratively(BinaryTreeNode* root) { if(root == NULL) return NULL; std::stack
stack; stack.push(root); while(!stack.empty()) { BinaryTreeNode* curRoot = stack.top(); stack.pop(); BinaryTreeNode* pTemp = curRoot->m_pLeft; curRoot->m_pLeft = curRoot->m_pRight; curRoot->m_pRight = pTemp; if(curRoot->m_pLeft) stack.push(curRoot->m_pLeft); if(curRoot->m_pRight) stack.push(curRoot->m_pRight); } return root;}//==================================hht's===============================void MirrorRecursively(BinaryTreeNode* pNode) { if(pNode == NULL) return; if(pNode->m_pLeft == NULL && pNode->m_pRight == NULL) return; BinaryTreeNode* pTemp = pNode->m_pLeft; pNode->m_pLeft = pNode->m_pRight; pNode->m_pRight = pTemp; if(pNode->m_pLeft) MirrorRecursively(pNode->m_pLeft); if(pNode->m_pRight) MirrorRecursively(pNode->m_pRight);}// ====================测试代码====================// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点// 8// 6 10// 5 7 9 11void Test1(){ printf("=====Test1 starts:=====\n"); 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); PrintTree(pNode8); printf("=====Test1: MyMirrorRecursively=====\n"); PrintTree(MyMirrorOfBinaryTree(pNode8)); printf("=====Test1: MyMirrorIteratively=====\n"); PrintTree(MyMirrorIteratively(pNode8));// printf("=====Test1: MirrorRecursively=====\n");// MirrorRecursively(pNode8);// PrintTree(pNode8);//// printf("=====Test1: MirrorIteratively=====\n");// MirrorIteratively(pNode8);// PrintTree(pNode8); DestroyTree(pNode8);}// 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点// 8// 7// 6// 5// 4void Test2(){ printf("=====Test2 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); ConnectTreeNodes(pNode8, pNode7, NULL); ConnectTreeNodes(pNode7, pNode6, NULL); ConnectTreeNodes(pNode6, pNode5, NULL); ConnectTreeNodes(pNode5, pNode4, NULL); PrintTree(pNode8); printf("=====Test2: MyMirrorRecursively=====\n"); PrintTree(MyMirrorOfBinaryTree(pNode8)); printf("=====Test2: MyMirrorIteratively=====\n"); PrintTree(MyMirrorIteratively(pNode8));// printf("=====Test2: MirrorRecursively=====\n");// MirrorRecursively(pNode8);// PrintTree(pNode8);//// printf("=====Test2: MirrorIteratively=====\n");// MirrorIteratively(pNode8);// PrintTree(pNode8); DestroyTree(pNode8);}// 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点// 8// 7// 6// 5// 4void Test3(){ printf("=====Test3 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); ConnectTreeNodes(pNode8, NULL, pNode7); ConnectTreeNodes(pNode7, NULL, pNode6); ConnectTreeNodes(pNode6, NULL, pNode5); ConnectTreeNodes(pNode5, NULL, pNode4); PrintTree(pNode8); printf("=====Test3: MyMirrorRecursively=====\n"); PrintTree(MyMirrorOfBinaryTree(pNode8)); printf("=====Test3: MyMirrorIteratively=====\n"); PrintTree(MyMirrorIteratively(pNode8));// printf("=====Test3: MirrorRecursively=====\n");// MirrorRecursively(pNode8);// PrintTree(pNode8);//// printf("=====Test3: MirrorIteratively=====\n");// MirrorIteratively(pNode8);// PrintTree(pNode8); DestroyTree(pNode8);}// 测试空二叉树:根结点为空指针void Test4(){ printf("=====Test4 starts:=====\n"); BinaryTreeNode* pNode = NULL; PrintTree(pNode); printf("=====Test4: MyMirrorRecursively=====\n"); PrintTree(MyMirrorOfBinaryTree(pNode)); printf("=====Test4: MyMirrorIteratively=====\n"); PrintTree(MyMirrorIteratively(pNode));// printf("=====Test4: MirrorRecursively=====\n");// MirrorRecursively(pNode);// PrintTree(pNode);//// printf("=====Test4: MirrorIteratively=====\n");// MirrorIteratively(pNode);// PrintTree(pNode);}// 测试只有一个结点的二叉树void Test5(){ printf("=====Test5 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); PrintTree(pNode8); printf("=====Test5: MyMirrorRecursively=====\n"); PrintTree(MyMirrorOfBinaryTree(pNode8)); printf("=====Test5: MyMirrorIteratively=====\n"); PrintTree(MyMirrorIteratively(pNode8));// printf("=====Test5: MirrorRecursively=====\n");// MirrorRecursively(pNode8);// PrintTree(pNode8);// printf("=====Test5: MirrorIteratively=====\n");// MirrorIteratively(pNode8);// PrintTree(pNode8);}int main(int argc, char* argv[]){ Test1(); Test2(); Test3(); Test4(); Test5(); return 0;}

转载于:https://my.oschina.net/ITHaozi/blog/279794

你可能感兴趣的文章
为tomcat启用nio机制
查看>>
jquery select下拉框和 easy-ui combox 选定指定项区别
查看>>
尚学堂&浪曦视频学习推荐顺序
查看>>
hibernate 第二次深入接触
查看>>
测试开发题目
查看>>
[USACO3.2]Sweet Butter
查看>>
关于三角形的一个不等式
查看>>
Elementary Methods in Number Theory Theorem 1.1 Division algorithm
查看>>
<10>获取当前时间
查看>>
Jenkins的构建编号和一个有趣的bug
查看>>
EF添加关联的提示问题:映射从第 260 行开始的片段时有问题:
查看>>
【JDK1.8】JUC——AbstractQueuedSynchronizer
查看>>
2.可变与不可变
查看>>
PCI Express(三) - A story of packets, stack and network
查看>>
ThinkPHP中添加事件机制
查看>>
OO第一单元总结
查看>>
求1到n,n个整数的全排列
查看>>
PHP7 教程
查看>>
虚拟机VMBox的空间扩展和对加载进来资源的扩展
查看>>
《结对-结对编项目作业名称-需求分析》
查看>>