|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <limits.h> |
| 4 | + |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <memory.h> |
| 8 | +#include <limits.h> |
| 9 | + |
| 10 | +#include <sstream> |
| 11 | +#include <iostream> |
| 12 | +#include <stack> |
| 13 | +#include <vector> |
| 14 | +#include <iterator> |
| 15 | +#include <numeric> |
| 16 | +#include <algorithm> |
| 17 | + |
| 18 | +/* A binary tree node has data, pointer to left child |
| 19 | + and a pointer to right child */ |
| 20 | +struct node |
| 21 | +{ |
| 22 | + int data; |
| 23 | + struct node* left; |
| 24 | + struct node* right; |
| 25 | +}; |
| 26 | + |
| 27 | +/* Helper function that allocates a new node with the |
| 28 | + given data and NULL left and right pointers. */ |
| 29 | +struct node* newNode(int data) |
| 30 | +{ |
| 31 | + struct node* node = (struct node*) |
| 32 | + malloc(sizeof(struct node)); |
| 33 | + node->data = data; |
| 34 | + node->left = NULL; |
| 35 | + node->right = NULL; |
| 36 | + |
| 37 | + return(node); |
| 38 | +} |
| 39 | + |
| 40 | +int largestBSTUtil(struct node* node, int *min_ref, int *max_ref, |
| 41 | + int *max_size_ref, bool *is_bst_ref); |
| 42 | + |
| 43 | +/* Returns size of the largest BST subtree in a Binary Tree |
| 44 | + (efficient version). */ |
| 45 | +int largestBST(struct node* node) |
| 46 | +{ |
| 47 | + // Set the initial values for calling largestBSTUtil() |
| 48 | + int min = INT_MAX; // For minimum value in right subtree |
| 49 | + int max = INT_MIN; // For maximum value in left subtree |
| 50 | + |
| 51 | + int max_size = 0; // For size of the largest BST |
| 52 | + bool is_bst = 0; |
| 53 | + |
| 54 | + largestBSTUtil(node, &min, &max, &max_size, &is_bst); |
| 55 | + |
| 56 | + return max_size; |
| 57 | +} |
| 58 | + |
| 59 | +/* largestBSTUtil() updates *max_size_ref for the size of the largest BST |
| 60 | + subtree. Also, if the tree rooted with node is non-empty and a BST, |
| 61 | + then returns size of the tree. Otherwise returns 0.*/ |
| 62 | +int largestBSTUtil(struct node* node, int *min_ref, int *max_ref, |
| 63 | + int *max_size_ref, bool *is_bst_ref) |
| 64 | +{ |
| 65 | + |
| 66 | + /* Base Case */ |
| 67 | + if (node == NULL) |
| 68 | + { |
| 69 | + *is_bst_ref = 1; // An empty tree is BST |
| 70 | + return 0; // Size of the BST is 0 |
| 71 | + } |
| 72 | + |
| 73 | + int min = INT_MAX; |
| 74 | + |
| 75 | + /* A flag variable for left subtree property |
| 76 | + i.e., max(root->left) < root->data */ |
| 77 | + bool left_flag = false; |
| 78 | + |
| 79 | + /* A flag variable for right subtree property |
| 80 | + i.e., min(root->right) > root->data */ |
| 81 | + bool right_flag = false; |
| 82 | + |
| 83 | + int ls, rs; // To store sizes of left and right subtrees |
| 84 | + |
| 85 | + /* Following tasks are done by recursive call for left subtree |
| 86 | + a) Get the maximum value in left subtree (Stored in *max_ref) |
| 87 | + b) Check whether Left Subtree is BST or not (Stored in *is_bst_ref) |
| 88 | + c) Get the size of maximum size BST in left subtree (updates *max_size) */ |
| 89 | + *max_ref = INT_MIN; |
| 90 | + ls = largestBSTUtil(node->left, min_ref, max_ref, max_size_ref, is_bst_ref); |
| 91 | + if (*is_bst_ref == 1 && node->data > *max_ref) |
| 92 | + left_flag = true; |
| 93 | + |
| 94 | + /* Before updating *min_ref, store the min value in left subtree. So that we |
| 95 | + have the correct minimum value for this subtree */ |
| 96 | + min = *min_ref; |
| 97 | + |
| 98 | + /* The following recursive call does similar (similar to left subtree) |
| 99 | + task for right subtree */ |
| 100 | + *min_ref = INT_MAX; |
| 101 | + rs = largestBSTUtil(node->right, min_ref, max_ref, max_size_ref, is_bst_ref); |
| 102 | + if (*is_bst_ref == 1 && node->data < *min_ref) |
| 103 | + right_flag = true; |
| 104 | + |
| 105 | + // Update min and max values for the parent recursive calls |
| 106 | + if (min < *min_ref) *min_ref = min; |
| 107 | + if (node->data < *min_ref) // For leaf nodes |
| 108 | + *min_ref = node->data; |
| 109 | + if (node->data > *max_ref) |
| 110 | + *max_ref = node->data; |
| 111 | + |
| 112 | + /* If both left and right subtrees are BST. And left and right |
| 113 | + subtree properties hold for this node, then this tree is BST. |
| 114 | + So return the size of this tree */ |
| 115 | + if(left_flag && right_flag) |
| 116 | + { |
| 117 | + if (ls + rs + 1 > *max_size_ref) |
| 118 | + *max_size_ref = ls + rs + 1; |
| 119 | + return ls + rs + 1; |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + //Since this subtree is not BST, set is_bst flag for parent calls |
| 124 | + *is_bst_ref = 0; |
| 125 | + return 0; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/* Driver program to test above functions*/ |
| 130 | +int main() |
| 131 | +{ |
| 132 | + /* Let us construct the following Tree |
| 133 | + 50 |
| 134 | + / \ |
| 135 | + 10 60 |
| 136 | + / \ / \ |
| 137 | + 5 20 55 70 |
| 138 | + / / \ |
| 139 | + 45 65 80 |
| 140 | + */ |
| 141 | + |
| 142 | + struct node *root = newNode(50); |
| 143 | + root->left = newNode(10); |
| 144 | + root->right = newNode(60); |
| 145 | + root->left->left = newNode(5); |
| 146 | + root->left->right = newNode(20); |
| 147 | + root->right->left = newNode(55); |
| 148 | + root->right->left->left = newNode(45); |
| 149 | + root->right->right = newNode(70); |
| 150 | + root->right->right->left = newNode(65); |
| 151 | + root->right->right->right = newNode(80); |
| 152 | + |
| 153 | + /* The complete tree is not BST as 45 is in right subtree of 50. |
| 154 | + The following subtree is the largest BST |
| 155 | + 60 |
| 156 | + / \ |
| 157 | + 55 70 |
| 158 | + / / \ |
| 159 | + 45 65 80 |
| 160 | + */ |
| 161 | + printf(" Size of the largest BST is %d", largestBST(root)); |
| 162 | + |
| 163 | + getchar(); |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
0 commit comments