递归和循环两种方法完成树的镜像转换

来源:互联网 发布:天龙八部全套源码资源 编辑:程序博客网 时间:2024/04/30 12:54
  1. /* 
  2. copyright@nciaebupt 转载出处:http://blog.csdn.net/nciaebupt/article/details/8506038 
  3. 题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。 用递归和循环两种方法完成树的镜像转换。 
  4. 例如: 
  5.         8                     8 
  6.       /   \       转换       /   \ 
  7.      6     10     -->      10    6 
  8.     /  \   / \            /  \   / \ 
  9.    5    7 9   11         11   9 7   5 
  10. */  
  11. #include <cstdlib>  
  12. #include <iostream>  
  13. #include <stack>  
  14.   
  15. struct BSTreeNode  
  16. {  
  17.     int value;  
  18.     int flag;  
  19.     BSTreeNode * left;  
  20.     BSTreeNode * right;  
  21. };  
  22.   
  23. //创建二叉查找树  
  24. BSTreeNode * InsertBSTree(BSTreeNode * pRoot,int value)  
  25. {  
  26.     //创建节点并初始化  
  27.     BSTreeNode * pNode = new BSTreeNode();  
  28.     pNode->value = value;  
  29.     pNode->flag = 0;  
  30.     pNode->left = NULL;  
  31.     pNode->right = NULL;  
  32.   
  33.     BSTreeNode * pCur = pRoot;  
  34.     BSTreeNode * pParent = NULL;  
  35.   
  36.     if(pRoot == NULL)  
  37.     {  
  38.         pRoot = pNode;  
  39.     }  
  40.     else  
  41.     {  
  42.         while(pCur != NULL)  
  43.         {  
  44.             pParent = pCur;  
  45.             if(value < pCur->value)  
  46.                 pCur = pCur->left;  
  47.             else  
  48.                 pCur = pCur->right;  
  49.         }  
  50.         if(value < pParent->value)  
  51.             pParent->left = pNode;  
  52.         else  
  53.             pParent->right = pNode;  
  54.     }  
  55.     return pRoot;  
  56. }  
  57. //中序遍历二叉树  
  58. void InOrderBSTree1(BSTreeNode * pRoot)  
  59. {  
  60.     BSTreeNode * pCur = pRoot;  
  61.     if(pCur != NULL)  
  62.     {  
  63.         InOrderBSTree1(pCur->left);  
  64.         std::cout<<pCur->value<<std::endl;  
  65.         InOrderBSTree1(pCur->right);  
  66.     }  
  67. }  
  68. void InOrderBSTree(BSTreeNode * pRoot)  
  69. {  
  70.     BSTreeNode cur = *pRoot;  
  71.     std::stack<BSTreeNode> stack;  
  72.     stack.push(cur);  
  73.   
  74.     while(!stack.empty())  
  75.     {  
  76.         if(stack.top().flag == 0)  
  77.         {  
  78.             stack.top().flag = 1;  
  79.             if(stack.top().left != NULL)  
  80.             {  
  81.                 stack.push(*(stack.top().left));  
  82.             }  
  83.         }  
  84.         else  
  85.         {  
  86.             std::cout<<stack.top().value<<std::endl;  
  87.             BSTreeNode tmp = stack.top();  
  88.             stack.pop();  
  89.             if(tmp.right != NULL)  
  90.             {  
  91.                 stack.push(*(tmp.right));  
  92.             }  
  93.         }  
  94.     }  
  95. }  
  96. //将二叉查找树转换为它的镜像,递归实现  
  97. void BSTree2MirrorRcur(BSTreeNode * pRoot)  
  98. {  
  99.     if(pRoot == NULL)  
  100.     {  
  101.         return;  
  102.     }  
  103.     //交换指针  
  104.     BSTreeNode * tmp = pRoot->left;  
  105.     pRoot->left = pRoot->right;  
  106.     pRoot->right = tmp;  
  107.     //递归左子树  
  108.     if(pRoot->left != NULL)  
  109.     {  
  110.         BSTree2MirrorRcur(pRoot->left);  
  111.     }  
  112.     //递归右子树  
  113.     if(pRoot->right != NULL)  
  114.     {  
  115.         BSTree2MirrorRcur(pRoot->right);  
  116.     }  
  117. }  
  118. //将二叉查找树转换为它的镜像,循环实现  
  119. void BSTree2Mirror(BSTreeNode * pRoot)  
  120. {  
  121.     if(pRoot == NULL)  
  122.         return;  
  123.     std::stack<BSTreeNode*> pStack;  
  124.     BSTreeNode * pCur = pRoot;  
  125.     BSTreeNode * pTmp = NULL;  
  126.     pStack.push(pCur);  
  127.     while(!pStack.empty())  
  128.     {  
  129.         pCur = pStack.top();  
  130.         pStack.pop();  
  131.         pTmp = pCur->left;  
  132.         pCur->left = pCur->right;  
  133.         pCur->right = pTmp;  
  134.         if(pCur->left != NULL)  
  135.             pStack.push(pCur->left);  
  136.         if(pCur->right != NULL)  
  137.             pStack.push(pCur->right);  
  138.     }  
  139. }  
  140.   
  141. int main(int args,char **argv)  
  142. {  
  143.     int array[] = {8,6,10,5,7,9,11};  
  144.     int len = sizeof(array)/sizeof(int);  
  145.     //创建二叉查找树  
  146.     BSTreeNode * pRoot = NULL;  
  147.     for(int i = 0;i < len;++i)  
  148.     {  
  149.         pRoot = InsertBSTree(pRoot,array[i]);  
  150.     }  
  151.     //中序遍历二叉树  
  152.     InOrderBSTree(pRoot);  
  153.     //将二叉查找树转换为它的镜像,递归实现  
  154.     //BSTree2MirrorRcur(pRoot);  
  155.     //中序遍历二叉树  
  156.     //InOrderBSTree(pRoot);  
  157.     //将二叉查找树转换为它的镜像,循环实现  
  158.     BSTree2Mirror(pRoot);  
  159.     //中序遍历二叉树  
  160.     InOrderBSTree(pRoot);  
  161.   
  162.     system("pause");  
  163.     return 0;  
  164. }  
原创粉丝点击