Advertisement
Guest User

largestBST

a guest
Dec 14th, 2010
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. /*
  5.                ___________________15___________________
  6.               /                                        \
  7.   ___________10___________                             20
  8.  /                        \
  9.  5                   _____7____
  10.                     /          \
  11.                   __2__       __5
  12.                  /     \     /
  13.                  0      8    3
  14. */
  15.  
  16. /*
  17.          _________11_________
  18.         /                    \
  19.        12      ______________15______________
  20.               /                              \
  21.   ___________10___________                   20
  22.  /                        \
  23.  5                   _____7____
  24.                     /          \
  25.                   __2__       __5
  26.                  /     \     /
  27.                  0      8    3
  28. */
  29.  
  30. class BinaryTree
  31. {
  32.     public:
  33.     int data;
  34.     // Store current tree size to avoid computing it over and over again...
  35.     int size;
  36.     BinaryTree *left, *right;
  37.  
  38.     BinaryTree(int d, BinaryTree *l=NULL, BinaryTree *r=NULL){
  39.         data = d;
  40.         left = l;
  41.         right = r;
  42.         updateSize();
  43.     }
  44.  
  45.     ~BinaryTree(){
  46.         if (left != NULL) delete(left);
  47.         if (right != NULL) delete(right);
  48.     }
  49.  
  50.     void print(){
  51.         if (left || right){
  52.             printf("(%i, ", data);
  53.             if (left)  left->print(); else printf("NULL");
  54.             printf(", ");
  55.             if (right) right->print(); else printf("NULL");
  56.             printf(")");
  57.         } else printf("(%i)", data);
  58.     }
  59.  
  60.     void updateSize(){
  61.         size = 1;
  62.         if (left != NULL){
  63.             size += left->size;
  64.         }
  65.         if (right != NULL){
  66.             size += right->size;
  67.         }
  68.     }
  69. } ;
  70.  
  71. #define B new BinaryTree
  72.  
  73. // Limit lower bound
  74. // O(log n)
  75. BinaryTree *boundMin(BinaryTree *t, int min){
  76.     if ((t == NULL) || (t->data < min)) return NULL;
  77.  
  78.     t->left = boundMin(t->left, min);
  79.     t->updateSize();
  80.  
  81.     return t;
  82. }
  83.  
  84. // Limit upper bound
  85. // O(log n)
  86. BinaryTree *boundMax(BinaryTree *t, int max){
  87.     if ((t == NULL) || (t->data > max)) return NULL;
  88.  
  89.     t->right = boundMax(t->right, max);
  90.     t->updateSize();
  91.  
  92.     return t;
  93. }
  94.  
  95. // Find the largest BST : bottom up approach. Start down, and when moving up,
  96. // recreate a tree from which we can cut off the branches that don't
  97. // respect the BST...
  98. // n nodes visisted, each step is 2 * O(log n) => O(n log n)
  99. BinaryTree *findLargestBST(BinaryTree *p, int &maxNodes, BinaryTree *&largestBST) {
  100.   if (!p) return NULL;
  101.  
  102.   // Bottom up approach... Check out the children first...
  103.   BinaryTree *left = findLargestBST(p->left, maxNodes, largestBST);
  104.   BinaryTree *right = findLargestBST(p->right, maxNodes, largestBST);
  105.  
  106.   // Limit the max value on the left side...
  107.   left = boundMax(left, p->data);
  108.  
  109.   // Limit the min value on the right side
  110.   right = boundMin(right, p->data);
  111.  
  112.   BinaryTree *t = new BinaryTree(p->data, left, right);
  113.  
  114.   // New tree is actually better than the other ones !
  115.   if(t->size > maxNodes){
  116.       maxNodes = t->size;
  117.       largestBST = p;   // Store root node from ORIGINAL tree
  118.   }
  119.  
  120.   return t;
  121. }
  122.  
  123. // Turn a tree into a BST by cutting off the nodes that
  124. // don't respect the BST constraints...
  125. // Maximum n nodes => O(n)
  126. BinaryTree *tree2BST(BinaryTree *t, int min, int max){
  127.     // End case
  128.     if (t == NULL) return NULL;
  129.  
  130.     // No node
  131.     if ((t->data < min) || (t->data > max)) return NULL;
  132.  
  133.     t->left = tree2BST(t->left, min, t->data);
  134.     t->right = tree2BST(t->right, t->data, max);
  135.     return t;
  136. }
  137.  
  138. // Fint the largestBST's root node, then create the BST from it
  139. // O(n log n) + O(n) => O(n log n)
  140. BinaryTree* findLargestBST(BinaryTree *root) {
  141.   BinaryTree *largestBST = NULL;
  142.   int maxNodes = INT_MIN;
  143.  
  144.   // Find root...
  145.   findLargestBST(root, maxNodes, largestBST);
  146.   printf("Size : %i\n", maxNodes);
  147.  
  148.   // Create BST from the largestBST's root
  149.   largestBST = tree2BST(largestBST, INT_MIN, INT_MAX);
  150.   largestBST->print();
  151.   printf("\n");
  152.  
  153.   return largestBST;
  154. }
  155.  
  156. int main()
  157. {
  158.     //BinaryTree *b = B( 15, B(10, B(5), B(7, B(2, B(0), B(8)), B(5, B(3), NULL))), B(20));
  159.     BinaryTree *b = B(11, B(12), B( 15, B(10, B(5), B(7, B(2, B(0), B(8)), B(5, B(3), NULL))), B(20)));
  160.  
  161.     b->print();
  162.     printf("\n");
  163.     findLargestBST(b);
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement