Advertisement
Guest User

Untitled

a guest
Jan 14th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.37 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define bool int
  5.  
  6. /* A binary tree node has data, pointer to left child
  7.    and a pointer to right child */
  8. struct node
  9. {
  10.    int data;
  11.    struct node* left;
  12.    struct node* right;
  13. };
  14.  
  15. /*
  16.  Original example code.
  17.  
  18.  Given a tree and a sum, return true if there is a path from the root
  19.  down to a leaf, such that adding up all the values along the path
  20.  equals the given sum.
  21.  
  22.  Strategy: subtract the node value from the sum when recurring down,
  23.  and check to see if the sum is 0 when you run out of tree.
  24. */
  25. bool hasPathSum_original(struct node* node, int sum)
  26. {
  27.   /* return true if we run out of tree and sum==0 */
  28.   if (node == NULL)
  29.   {
  30.      return (sum == 0);
  31.   }
  32.  
  33.   else
  34.   {
  35.     bool ans = 0;
  36.  
  37.     /* otherwise check both subtrees */
  38.     int subSum = sum - node->data;
  39.  
  40.     /* If we reach a leaf node and sum becomes 0 then return true*/
  41.     if ( subSum == 0 && node->left == NULL && node->right == NULL )
  42.       return 1;
  43.  
  44.     if(node->left)
  45.       ans = ans || hasPathSum_original(node->left, subSum);
  46.     if(node->right)
  47.       ans = ans || hasPathSum_original(node->right, subSum);
  48.  
  49.     return ans;
  50.   }
  51. }
  52.  
  53. /*
  54. Here is the original example without optimization. You'll see that the algorithm is much cleaner now. You had a bug in your rewrite as it answered the question "Is there A path with the given sum?" when you had nodes which had a left node without a right one (or vice versa).
  55. */
  56. bool hasPathSum_revised(struct node* node, int sum)
  57. {
  58.   /* return true if we run out of tree and sum==0 */
  59.   if (node == NULL)
  60.      return (sum == 0);
  61.  
  62.   /* otherwise check both subtrees */
  63.   int subSum = sum - node->data;
  64.   return hasPathSum_revised(node->left, subSum) ||  hasPathSum_revised(node->right, subSum);
  65. }
  66.  
  67. /*
  68. Find the minimum in the binary tree - without making any assumptions.
  69. Call this with an "impossible" minimum at top level.
  70. */
  71. int findMin(struct node* node, int min) {
  72.     if (!node) return min;
  73.     if (node->data < min)
  74.        min = node->data;
  75.  
  76.     int leftMin = findMin(node->left, min);
  77.     if(leftMin < min)
  78.        min = leftMin;
  79.  
  80.     int rightMin = findMin(node->right, min);
  81.     if(rightMin < min)
  82.        min = rightMin;
  83.  
  84.     return min;
  85. }
  86.  
  87. /* UTILITY FUNCTIONS */
  88. /* Helper function that allocates a new node with the
  89.    given data and NULL left and right pointers. */
  90. struct node* newnode(int data)
  91. {
  92.   struct node* node = (struct node*)
  93.                        malloc(sizeof(struct node));
  94.   node->data = data;
  95.   node->left = NULL;
  96.   node->right = NULL;
  97.  
  98.   return(node);
  99. }
  100.  
  101. void freetree(struct node* node)
  102. {
  103.   if(!node)
  104.     return;
  105.   freetree(node->left);
  106.   freetree(node->right);
  107.   free(node);
  108. }
  109.  
  110. /* Generate a random full binary tree where every path to a leaf has the
  111.    totalsum given */
  112. struct node* generatetree(struct node* node, unsigned depth, int totalsum) {
  113.   if(depth == 0) {
  114.     node->data = totalsum;
  115.     return node;
  116.   }
  117.   node->data = (rand() % 100) - 50;
  118.   node->left = newnode(0);
  119.   node->right = newnode(0);
  120.   generatetree(node->left, depth-1, totalsum - node->data);
  121.   generatetree(node->right, depth-1, totalsum - node->data);
  122.   return node;
  123. }
  124.  
  125. void inspecttree(struct node* root, int expected_sum) {
  126.   printf("Original:\n");
  127.   if(hasPathSum_original(root, expected_sum))
  128.     printf("There is a root-to-leaf path with sum %d\n", expected_sum);
  129.   else
  130.     printf("There is no root-to-leaf path with sum %d\n", expected_sum);
  131.  
  132.   printf("Revised:\n");
  133.   if(hasPathSum_revised(root, expected_sum))
  134.     printf("There is a root-to-leaf path with sum %d\n", expected_sum);
  135.   else
  136.     printf("There is no root-to-leaf path with sum %d\n", expected_sum);
  137.   /* where was INT_MAX defined again :( */
  138.   printf("Minimum: %d\n", findMin(root, 60000));
  139. }
  140.  
  141. /* Driver program to test above functions*/
  142. int main()
  143. {
  144.   srand(time(NULL));
  145.   int sum = 21;
  146.  
  147.   /* Hand constructed binary tree is
  148.             10
  149.           /   \
  150.         8      2
  151.       /  \    /
  152.     3     5  2
  153.   */
  154.   struct node *root = newnode(10);
  155.   root->left        = newnode(8);
  156.   root->right       = newnode(2);
  157.   root->left->left  = newnode(3);
  158.   root->left->right = newnode(5);
  159.   root->right->left = newnode(2);
  160.   inspecttree(root, sum);
  161.   freetree(root);
  162.  
  163.   root = generatetree(newnode(0), 8, 1000);
  164.   inspecttree(root, 1000);
  165.   freetree(root);
  166.  
  167.   getchar();
  168.   return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement