fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define ADD_LENGTH 50
  4. struct node
  5. {
  6. int id;
  7. char agent[12];
  8. int price;
  9. int size; //In square feet
  10. int numBeds;
  11. double numBaths;
  12. int yearBuilt;
  13. char address[ADD_LENGTH];
  14. struct node *left;
  15. struct node *right;
  16. };
  17. struct node * newNode(int id, char str[], int price, int size, int numBeds, double numBaths, int yearBuilt) {
  18. struct node *temp = (struct node*)malloc(sizeof(struct node));
  19. temp->id = id;
  20. temp->price = price;
  21. temp->numBeds = numBeds;
  22. temp->numBaths = numBaths;
  23. temp->yearBuilt = yearBuilt;
  24. temp->left = temp->right = NULL;
  25. return temp;
  26. }
  27. struct node* insert(struct node* node, int id, char *str, int key, int size, int numBeds, double numBaths, int yearBuilt)
  28. {
  29. /* If the tree is empty, return a new node */
  30. if (node == NULL) return newNode(id, str, key, size, numBeds, numBaths, yearBuilt);
  31.  
  32. /* Otherwise, recur down the tree */
  33. if (key <= node->price)
  34. node->left = insert(node->left, id, str, key, size, numBeds, numBaths, yearBuilt);
  35. else if (key > node->price)
  36. node->right = insert(node->right, id, str, key, size, numBeds, numBaths, yearBuilt);
  37.  
  38. /* return the (unchanged) node pointer */
  39. return node;
  40. }
  41. void print(struct node *node) {
  42. printf("%d\n", node->id);
  43. printf("%d\n", node->price);
  44. printf("%d\n", node->numBeds);
  45. printf("%lf\n", node->numBaths);
  46. printf("%d\n", node->yearBuilt);
  47. }
  48. void findListingByPrice(struct node *node, int maxPrice)
  49. {
  50. if(node == NULL)
  51. {
  52. return;
  53. }else {
  54. findListingByPrice(node->left, maxPrice);
  55. if(node->price <=maxPrice) {
  56. printf("Found 1\n");
  57. print(node);
  58. }
  59. findListingByPrice(node->right, maxPrice);
  60. }
  61.  
  62.  
  63. }
  64. void inorder(struct node *node) {
  65. if(node != NULL) {
  66. inorder(node->left);
  67. printf("%d ", node->price);
  68. inorder(node->right);
  69. }
  70. }
  71. int main()
  72. {
  73. /* Let us create following BST
  74.   50
  75.   / \
  76.   30 70
  77.   / \ / \
  78.   20 40 60 80 */
  79. struct node *root = NULL;
  80. root = insert(root, 1, "hello 1", 50, 5, 5, 4.5, 2010);
  81. insert(root, 2, "hello 2", 30, 5, 6, 4.5, 2010);;
  82. insert(root, 3, "hello 3", 20, 5, 7, 4.5, 2010);
  83. insert(root, 4, "hello 4", 40, 5, 8, 4.5, 2010);
  84. insert(root, 5, "hello 5", 70, 5, 9, 4.5, 2010);
  85. insert(root, 6, "hello 6", 60, 5, 10, 4.5, 2010);
  86. insert(root, 7, "hello 7", 80, 5, 11, 4.5, 2010);
  87.  
  88. // print inoder traversal of the BST
  89. inorder(root);
  90. findListingByPrice(root, 65);
  91.  
  92. return 0;
  93. }
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
20 30 40 50 60 70 80 Found 1
3
20
7
4.500000
2010
Found 1
2
30
6
4.500000
2010
Found 1
4
40
8
4.500000
2010
Found 1
1
50
5
4.500000
2010
Found 1
6
60
10
4.500000
2010