BiTree 二叉树 基础算法及常见题目总结

来源:互联网 发布:wpa2密码破解软件 编辑:程序博客网 时间:2024/05/30 04:17

1,对二叉树的几个相关概念的理清(概念内容均来自算法导论附录B的内容)

(1),树:是一个连通的,无回路的无向图,如果无向图是无回路但是非连通的,称为“森林“


 


 

(2),有根树和有序树

有根树是一颗自由树,它有一个与其他点不同的结点,为树的“根”,是唯一没有双亲的结点,没有子女的结点是叶结点或外部结点。有根树结点的子女数称为结点的“”,结点在树中的高度是结点向下到某个叶结点最长简单路径中边的条数。树的高度是根的高度,

一棵满二叉树,即每个内部结点都有两个子女的二叉树的结点个数为2^h - 1 , h为树的高度。


 


2,二叉树相关的算法题目:

[cpp] view plaincopyprint?
  1. // BiTree.cpp : Defines the entry point for the console application.  
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include "stdio.h"   
  6. #include "stdlib.h"   
  7. #include <stack>   
  8. using namespace std;  
  9. struct BiTree   
  10. {  
  11.     BiTree* m_pParent;  
  12.     BiTree* m_pLeft;  
  13.     BiTree* m_pRight;  
  14.     int key;  
  15.     int visited;//Only used in the function PostOrderNonRecursive()   
  16. };  
  17.   
  18. void Print(BiTree* root)  
  19. {  
  20.     printf("%d ", root->key);  
  21.   
  22. }  
  23.   
  24. void INORDER_TREE_WALK(BiTree* root)  
  25. {  
  26.     if (NULL != root)  
  27.     {  
  28.         INORDER_TREE_WALK(root->m_pLeft);  
  29.         Print(root);  
  30.         INORDER_TREE_WALK(root->m_pRight);  
  31.     }  
  32. }  
  33.   
  34. void PREORDER_TREE_WALK(BiTree* root)  
  35. {  
  36.     if (NULL != root)  
  37.     {  
  38.         Print(root);  
  39.         PREORDER_TREE_WALK(root->m_pLeft);  
  40.         PREORDER_TREE_WALK(root->m_pRight);  
  41.           
  42.     }  
  43. }  
  44.   
  45. void POSTORDER_TREE_WALK(BiTree* root)  
  46. {  
  47.     if (NULL != root)  
  48.     {  
  49.           
  50.         POSTORDER_TREE_WALK(root->m_pLeft);  
  51.         POSTORDER_TREE_WALK(root->m_pRight);  
  52.         Print(root);  
  53.     }  
  54. }  
  55.   
  56. int IS_POSTORDER(int A[],int len)  
  57. {  
  58.     int flag = 1;  
  59.     int right = len - 1;  
  60.     if (right > 0)  
  61.     {  
  62.         if (A[right] < A[len])  
  63.         {  
  64.             flag = 0;  
  65.         }  
  66.         else  
  67.         {  
  68.             IS_POSTORDER(A,right);  
  69.         }  
  70.   
  71.         int left = len - 2;  
  72.         if (left > 0)  
  73.         {  
  74.               
  75.             if (A[left] > A[len])  
  76.             {  
  77.                 flag = 0;  
  78.             }  
  79.             else  
  80.             {  
  81.                 IS_POSTORDER(A,left);  
  82.             }  
  83.         }  
  84.       
  85.     }  
  86.       
  87.     return flag;  
  88. }  
  89.   
  90.   
  91. void TREE_INSERT_RECURSION(BiTree* root, int key)  
  92. {  
  93.     if (root == NULL)  
  94.     {  
  95.         return;  
  96.     }  
  97.     else if (key < root->m_pLeft->key)  
  98.     {  
  99.         TREE_INSERT_RECURSION(root->m_pLeft,key);  
  100.     }  
  101.     else  
  102.     {  
  103.         TREE_INSERT_RECURSION(root->m_pRight,key);  
  104.     }  
  105. }  
  106.   
  107. BiTree* MakeNode(int key)  
  108. {  
  109.     BiTree* pRoot = (BiTree*)(malloc(sizeof(BiTree)));  
  110.   
  111.     pRoot->m_pLeft = NULL;  
  112.     pRoot->m_pRight = NULL;  
  113.     pRoot->m_pParent = NULL;  
  114.     pRoot->key = key;  
  115.     pRoot->visited = 0;  
  116.     return pRoot;  
  117. }  
  118.   
  119. void DelNode(BiTree* pNode)  
  120. {  
  121.     free(pNode);  
  122.   
  123. }  
  124.   
  125. //构建二叉查找树   
  126. void TREE_INSERT(BiTree* root, int key)  
  127. {  
  128.     BiTree* y = NULL;  
  129.     BiTree* x = root;  
  130.     while(x != NULL)  
  131.     {  
  132.         y = x;  
  133.         if (key < x->key)  
  134.         {  
  135.             x = x->m_pLeft;  
  136.         }  
  137.         else  
  138.         {  
  139.             x = x->m_pRight;  
  140.         }  
  141.     }  
  142.   
  143.     BiTree* z = MakeNode(key);  
  144.     z->m_pParent = y;  
  145.   
  146.     if (y == NULL)  
  147.     {  
  148.         root = z;  
  149.     }  
  150.     else  
  151.     {  
  152.         if (key < y->key)  
  153.         {  
  154.             y->m_pLeft = z;  
  155.         }  
  156.         else  
  157.         {  
  158.             y->m_pRight = z;  
  159.         }  
  160.     }  
  161. }  
  162.   
  163. //二叉查找树最小值   
  164. BiTree* TREE_MINIMUM(BiTree* root)  
  165. {  
  166.     while(root->m_pLeft != NULL)  
  167.     {  
  168.         root = root->m_pLeft;  
  169.     }  
  170.     return root;  
  171. }  
  172. //二叉查找树最大值   
  173. BiTree* TREE_MAXIMUM(BiTree* root)  
  174. {  
  175.     while(root->m_pRight != NULL)  
  176.     {  
  177.         root = root->m_pRight;  
  178.     }  
  179.     return root;  
  180. }  
  181.   
  182. BiTree* TREE_SUCCESSOR(BiTree* node)  
  183. {  
  184.     if (node->m_pRight != NULL)  
  185.     {  
  186.         return TREE_MINIMUM(node->m_pRight);  
  187.     }  
  188.       
  189.     BiTree* p = node->m_pParent;  
  190.     while ( p != NULL && p->m_pRight == node)  
  191.     {  
  192.         node = p;  
  193.         p = p->m_pParent;  
  194.     }  
  195.     return p;  
  196. }  
  197.   
  198. void TREE_DELETE(BiTree* root, BiTree* node)  
  199. {  
  200.     BiTree* y = NULL;  
  201.     BiTree* x = NULL;  
  202.     if (node->m_pLeft == NULL  || node->m_pRight == NULL)  
  203.     {  
  204.         y = node;  
  205.     }  
  206.     else  
  207.     {  
  208.         y = TREE_SUCCESSOR(node);  
  209.     }  
  210.     //开始删除   
  211.     if (y->m_pLeft != NULL)  
  212.     {  
  213.         x = y->m_pLeft;  
  214.     }  
  215.     else  
  216.     {  
  217.         x = y->m_pRight;  
  218.     }  
  219.   
  220.     if (x != NULL) //y是内节点  
  221.     {  
  222.         x->m_pParent = y->m_pParent;  
  223.     }  
  224.   
  225.     if (y->m_pParent == NULL) // 被删除的是根节点  
  226.     {  
  227.         root = x;  
  228.     }  
  229.     else if(y == y->m_pParent->m_pLeft)  
  230.     {  
  231.         y->m_pParent->m_pLeft = x;  
  232.     }  
  233.     else  
  234.     {  
  235.         y->m_pParent->m_pRight = x;  
  236.     }  
  237.   
  238.     if ( y->key != node->key )//情况3  
  239.     {  
  240.         node->key = y->key;  
  241.     }  
  242.   
  243. }  
  244.   
  245. void find_dis_helper(BiTree* node, int& max_dis, int& max_depth)  
  246. {  
  247.       
  248.     int left_max_depth = 0;  
  249.     int right_max_depth = 0;  
  250.   
  251.     int left_max_dis = 0;  
  252.     int right_max_dis = 0;  
  253.   
  254.     if (node->m_pLeft != NULL)  
  255.     {  
  256.         find_dis_helper(node->m_pLeft,left_max_dis,left_max_depth);  
  257.     }  
  258.     if (node->m_pRight != NULL)  
  259.     {  
  260.         find_dis_helper(node->m_pRight,right_max_dis, right_max_depth);  
  261.     }  
  262.     max_depth = (left_max_depth > right_max_depth ? left_max_depth : right_max_depth) + 1;  
  263.     int tmp_total = left_max_depth + right_max_depth + 2;  
  264.     int tmp_dis = left_max_dis > right_max_dis ? left_max_dis : right_max_dis;  
  265.     max_dis = max_dis > tmp_dis ? max_dis : tmp_dis;  
  266.     max_dis = max_dis > tmp_total ? max_dis : tmp_total;  
  267. }  
  268.   
  269. //寻找二叉树两个距离最远的结点   
  270. int Find_LONGEST_DISTANCE(BiTree* root)  
  271. {  
  272.     int max_dis = 0;  
  273.     int max_depth = 0;  
  274.     find_dis_helper(root, max_dis, max_depth);  
  275.     return max_dis;  
  276. }  
  277.   
  278. //反转二叉树   
  279. void MirrorBitree(BiTree* root)  
  280. {  
  281.     if (NULL == root)  
  282.     {  
  283.         return;  
  284.     }  
  285.   
  286.     MirrorBitree(root->m_pLeft);  
  287.     MirrorBitree(root->m_pRight);  
  288.   
  289.     BiTree* tmp = root->m_pLeft;  
  290.     root->m_pLeft = root->m_pRight;  
  291.     root->m_pRight = tmp;  
  292. }  
  293.   
  294.   
  295. void GetHeight_HELPER(BiTree* root, int& height, int& max_height)  
  296. {  
  297.     if (NULL == root)  
  298.     {  
  299.         height--;  
  300.         return;  
  301.     }  
  302.       
  303.     int tmp_height = height + 1;  
  304.   
  305.     GetHeight_HELPER(root->m_pLeft, tmp_height, max_height);  
  306.     GetHeight_HELPER(root->m_pRight, tmp_height, max_height);  
  307.   
  308.   
  309.     if (max_height < height)  
  310.     {  
  311.         max_height = height;  
  312.     }  
  313. }  
  314.   
  315. int GetHeight(BiTree* root)  
  316. {  
  317.     int height = 0;  
  318.     int max_height = -1;  
  319.     GetHeight_HELPER(root,height,max_height);  
  320.     return max_height;  
  321. }  
  322. //分层打印二叉树   
  323. void PrintBitreeByLevel(BiTree* root, int level)  
  324. {  
  325.     if (level == 0)  
  326.     {  
  327.         printf("%d ",root->key);  
  328.     }  
  329.   
  330.     if (root->m_pLeft != NULL)  
  331.     {  
  332.         PrintBitreeByLevel(root->m_pLeft, level - 1);  
  333.     }  
  334.     if (root->m_pRight != NULL)  
  335.     {  
  336.         PrintBitreeByLevel(root->m_pRight, level - 1);  
  337.     }  
  338.   
  339. }  
  340.   
  341.   
  342. void Visit(BiTree* node)  
  343. {  
  344.     if (node != NULL )  
  345.     {  
  346.         printf("%d ", node->key);  
  347.     }  
  348. }  
  349.   
  350. void PreOrderNonRecursive(BiTree* node)  
  351. {  
  352.     if(node == NULL)  
  353.         return;  
  354.   
  355.     stack<BiTree*> handywork;  
  356.     handywork.push(node);  
  357.     while(!handywork.empty())  
  358.     {  
  359.         BiTree* current = handywork.top();  
  360.         handywork.pop();  
  361.         Visit(current);  
  362.         if(current->m_pRight)  
  363.             handywork.push(current->m_pRight);  
  364.         if(current->m_pLeft)  
  365.             handywork.push(current->m_pLeft);  
  366.     }  
  367.   
  368. }  
  369.   
  370. void InOrderNonRecursive(BiTree* node)  
  371. {  
  372.     if(node == NULL)  
  373.         return;  
  374.     BiTree* current = node;  
  375.     stack<BiTree*> handywork;  
  376.     while(current != NULL || !handywork.empty())  
  377.     {  
  378.         if(current)  
  379.         {  
  380.             handywork.push(current);  
  381.             current = current->m_pLeft;  
  382.         }  
  383.         else  
  384.         {  
  385.             current = handywork.top();  
  386.             handywork.pop();  
  387.             Visit(current);  
  388.             current=current->m_pRight;  
  389.         }  
  390.           
  391.     }  
  392. }  
  393.   
  394.   
  395.   
  396. void PostOrderNonRecursive(BiTree* node)  
  397. {  
  398.     if(node == NULL)  
  399.         return;  
  400.   
  401.     BiTree* current = node;  
  402.     stack<BiTree*> handywork;  
  403.     while(current!= NULL )  
  404.     {  
  405.         handywork.push(current);  
  406.         current = current->m_pLeft;  
  407.     }  
  408.   
  409.     while(!handywork.empty())  
  410.     {  
  411.         BiTree* current = handywork.top();  
  412.         if( !current->m_pRight || current->m_pRight->visited )  
  413.         {  
  414.             handywork.pop();  
  415.             Visit(current);  
  416.         }  
  417.         else//若它的右孩子存在且rvisited为0,说明以前还没有动过它的右孩子,于是就去处理一下其右孩子。  
  418.         {   
  419.             //此时我们要从其右孩子结点开始一直往左下方走,直至走到尽头,将这条路径上的所有结点都入栈。  
  420.   
  421.             //当然,入栈之前要先将该结点的rvisited设成1,因为其右孩子的入栈意味着它的右孩子必将先于它被访问(这很好理解,因为我们总是从栈顶取出元素来进行visit)。由此可知,下一次该元素再处于栈顶时,其右孩子必然已被visit过了,所以此处可以将rvisited设置为1。  
  422.             current->m_pRight->visited = 1;  
  423.             current = current->m_pRight;  
  424.             while(current)  
  425.             {  
  426.                 handywork.push(current);  
  427.                 current = current->m_pLeft;  
  428.             }  
  429.         }  
  430.     }  
  431. }  
  432.   
  433.   
  434.   
  435.   
  436.   
  437.   
  438. int main(int argc, char* argv[])  
  439. {  
  440.     int a[] = {10,5,12,4,7};  
  441.     BiTree* root = MakeNode(a[0]);  
  442.     for (int i = 1; i < 5; i++)  
  443.     {  
  444.         TREE_INSERT(root,a[i]);  
  445.     }  
  446.   
  447.     printf("InOrder\r\n");  
  448.     INORDER_TREE_WALK(root);  
  449.     printf("\r\n");  
  450.     InOrderNonRecursive(root);  
  451.     printf("\r\n");  
  452.   
  453.     printf("PreOrder\r\n");  
  454.     PREORDER_TREE_WALK(root);  
  455.     printf("\r\n");  
  456.     PreOrderNonRecursive(root);  
  457.     printf("\r\n");  
  458.   
  459.     printf("PostOrder\r\n");  
  460.     POSTORDER_TREE_WALK(root);  
  461.     printf("\r\n");  
  462.     PostOrderNonRecursive(root);  
  463.     printf("\r\n");  
  464.   
  465.     int A[] = {4, 7, 5, 12, 10};  
  466.     printf("%d\r\n", IS_POSTORDER(A,4));  
  467.   
  468.     printf("%d\r\n",Find_LONGEST_DISTANCE(root));  
  469.   
  470.     PrintBitreeByLevel(root, 1);  
  471.   
  472.     MirrorBitree(root);  
  473.   
  474.     PrintBitreeByLevel(root, 1);  
  475.   
  476.     printf("\r\n height = %d", GetHeight(root));  
  477.     return 0;  
  478. }