Guest User

Untitled

a guest
Jan 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. struct book {
  6. char* name;
  7. int year;
  8. };
  9.  
  10. typedef struct tnode {
  11. struct book *aBook;
  12. struct tnode *left;
  13. struct tnode *right;
  14. } BTree;
  15.  
  16. BTree* addBook(BTree* nodeP, char* name, int year){
  17. if( nodeP == NULL )
  18. {
  19. nodeP = (struct tnode*) malloc( sizeof( struct tnode ) );
  20. (nodeP->aBook)->year = year;
  21. (nodeP->aBook)->name = name;
  22. /* initialize the children to null */
  23. (nodeP)->left = NULL;
  24. (nodeP)->right = NULL;
  25. }
  26. else if(year > (nodeP->aBook)->year)
  27. {
  28. addBook(&(nodeP)->left,name,year );
  29. }
  30. else if(year < (nodeP->aBook)->year)
  31. {
  32. addBook(&(nodeP)->right,name,year );
  33. }
  34. return nodeP;
  35. }
  36.  
  37. void freeBTree(BTree* books)
  38. {
  39. if( books != NULL )
  40. {
  41. freeBTree(books->left);
  42. freeBTree(books->right);
  43. //free( books );
  44. }
  45. }
  46.  
  47. void printBooks(BTree* books){
  48. if(books != NULL){
  49.  
  50. }
  51. }
  52.  
  53. int main(int argc, char** argv) {
  54. BTree *head;
  55. head = addBook(head,"The C Programming Language", 1990);
  56. /*addBook(head,"JavaScript, The Good Parts",2008);
  57. addBook(head,"Accelerated C++: Practical Programming by Example", 2000);
  58. addBook(head,"Scala for the impatient",2012);*/
  59. }
  60.  
  61. BTree *head;
  62.  
  63. BTree *head = NULL;
  64.  
  65. addBook(&(nodeP)->left,name,year );
  66.  
  67. addBook( nodeP->left,name,year );
  68.  
  69. BTree *makeNode(char *name, int year)
  70. {
  71. // NOTE: 3 frees required for every node
  72. BTree *nodeP = malloc( sizeof( struct tnode ) ); // 1
  73. nodeP->aBook = malloc( sizeof(struct book) ); // 2
  74. (nodeP->aBook)->year = year;
  75. (nodeP->aBook)->name = malloc(strlen(name) + 1); // 3
  76. strcpy((nodeP->aBook)->name,name);
  77. /* initialize the children to null */
  78. nodeP->left = NULL;
  79. nodeP->right = NULL;
  80. return nodeP;
  81. }
  82.  
  83. BTree* addBook(BTree* nodeP, char* name, int year)
  84. {
  85. if ( nodeP == NULL )
  86. {
  87. nodeP = makeNode(name,year);
  88. }
  89. else if (year > (nodeP->aBook)->year)
  90. {
  91. if ( nodeP->left == NULL )
  92. nodeP->left = makeNode(name,year);
  93. else
  94. addBook( nodeP->left,name,year );
  95. }
  96. else if(year < (nodeP->aBook)->year)
  97. {
  98. if ( nodeP->right == NULL )
  99. nodeP->right = makeNode(name,year);
  100. else
  101. addBook( nodeP->right,name,year );
  102. }
  103. return nodeP;
  104. }
  105.  
  106. void printBooks(BTree* books)
  107. {
  108. if (books != NULL) {
  109. printf("book: %s %dn",books->aBook->name,books->aBook->year);
  110. printBooks(books->right);
  111. printBooks(books->left);
  112. }
  113. }
  114.  
  115. nodeP = (struct tnode*) malloc( sizeof( struct tnode ) );
  116. (nodeP->aBook)->year = year;
Add Comment
Please, Sign In to add comment