一颗完全二叉树,求其结点个数

来源:互联网 发布:显微镜图像测量软件 编辑:程序博客网 时间:2024/05/22 03:36

http://blog.csdn.net/bobten2008/article/details/9734979


网上看了很多人的答案,都说最优解是o(n),想到了一种更快的算法,复杂度是O(lgn的平方),就是对左右子树进行二分,找最后一层的最右边那个结点即可:


[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <cmath>  
  3. #include <stdlib.h>  
  4.   
  5. using namespace std;  
  6.   
  7. struct Node {  
  8.   Node* left;  
  9.   Node* right;  
  10.   ~Node() {  
  11.     if (left) {  
  12.       delete left;  
  13.       left = NULL;  
  14.     }  
  15.     if (right) {  
  16.       delete right;  
  17.       right = NULL;  
  18.     }  
  19.   }  
  20. };  
  21.   
  22. int GetDepth(Node* root) {  
  23.   int depth = 0;  
  24.   while (root) {  
  25.     depth++;  
  26.     root = root->left;  
  27.   }  
  28.   return depth;  
  29. }  
  30.   
  31. void GetMostRightDepthAndIndex(Node* root, int* depth, int* index) {  
  32.   while (root) {  
  33.     if (root->right) {  
  34.       root = root->right;  
  35.       *index = (*index - 1) * 2;  
  36.       *index += 2;  
  37.       *depth += 1;  
  38.     } else if (root->left) {  
  39.       root = root->left;  
  40.       *index = (*index - 1) * 2;  
  41.       *index += 1;  
  42.       *depth += 1;  
  43.     } else {  
  44.       return;  
  45.     }  
  46.   }  
  47. }  
  48.   
  49. void GetMostLeftDepthAndIndex(Node* root, int* depth, int* index) {  
  50.   while (root) {  
  51.     if (root->left) {  
  52.       root = root->left;  
  53.       *index = (*index - 1) * 2;  
  54.       *index += 1;  
  55.       *depth += 1;  
  56.     }  
  57.     else if (root->left) {  
  58.       root = root->right;  
  59.       *index = (*index - 1) * 2;  
  60.       *index += 2;  
  61.       *depth += 1;  
  62.     } else {  
  63.       return;  
  64.     }  
  65.   }  
  66. }  
  67.   
  68. void GetNodeNum(Node* root, int cur_depth, int cur_index,  
  69.     int* node_num, int max_depth) {  
  70.   if (!root->left) {  
  71.     if (cur_depth - 1 >= 1) {  
  72.       *node_num += pow(2, cur_depth - 1) - 1;  
  73.     }  
  74.     *node_num += cur_index;  
  75.     return;  
  76.   }  
  77.   int ml_depth = root->left ? cur_depth + 1 : cur_depth;  
  78.   int ml_index = (cur_index - 1) * 2 + 1;  
  79.   int mr_depth = root->right ? cur_depth + 1 : cur_depth;  
  80.   int mr_index = (cur_index - 1) * 2 + 2;  
  81.   GetMostRightDepthAndIndex(root->left, &ml_depth, &ml_index);  
  82.   GetMostLeftDepthAndIndex(root->right, &mr_depth, &mr_index);  
  83.   if (ml_depth == mr_depth) {  
  84.     if (ml_depth == max_depth) {  
  85.       GetNodeNum(root->right, cur_depth + 1, (cur_index - 1) * 2 + 2,  
  86.           node_num, max_depth);  
  87.     } else if (ml_depth < max_depth) {  
  88.       GetNodeNum(root->left, cur_depth + 1, (cur_index - 1) * 2 + 1,  
  89.           node_num, max_depth);  
  90.     } else {  
  91.       std::cout << "illegal tree";  
  92.       exit(1);  
  93.     }  
  94.   } else if (ml_depth > mr_depth) {  
  95.     if (ml_depth == max_depth && mr_depth == max_depth - 1) {  
  96.       *node_num = pow(2, ml_depth - 1) - 1 + ml_index;  
  97.     }  
  98.   } else {  
  99.     std::cout << "illegal tree";  
  100.     exit(1);  
  101.   }  
  102. }  
  103.   
  104. int main() {  
  105.   /* Input: create a  */  
  106.   // depth 1  
  107.   Node* root = new Node();  
  108.   // depth 2  
  109.   root->left = new Node();  
  110.   root->right = new Node();  
  111.   // depth 3  
  112.   root->left->left = new Node();  
  113.   root->left->right = new Node();  
  114.   root->right->left = new Node();  
  115.   root->right->right = new Node();  
  116.   // depth 4  
  117.   root->left->left->left = new Node();  
  118.   root->left->left->right= new Node();  
  119.   root->left->right->left = new Node();  
  120.   root->left->right->right = new Node();  
  121.   root->right->left->left = new Node();  
  122.   root->right->left->right = new Node();  
  123.   root->right->right->left = new Node();  
  124.   root->right->right->right = new Node();  
  125.   // depth 5  
  126.   root->left->left->left->left = new Node();  
  127.   
  128.   int root_depth = 1;  
  129.   int root_index = 1;  
  130.   int max_depth = GetDepth(root);  
  131.   int node_num = 0;  
  132.   GetNodeNum(root, root_depth, root_index, &node_num, max_depth);  
  133.   std::cout << "node_num = " << node_num << endl;  
  134.   delete root;  
  135. }  




版权声明:本文为博主原创文章,未经博主允许不得转载。


0 0
原创粉丝点击