1
0

tree.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "tree.h"
  4. /*
  5. * In this program, you must NOT use while, for, goto. If you need to
  6. * traverse the tree, you must use recursion.
  7. *
  8. * You cannot use global or static variables. You will lose 1 point
  9. * (25% of the full score) for each global or static variable.
  10. *
  11. * You cannot use "mysterious" constants in the program. If you need
  12. * a constant, you have to define a symbol at the top of this file.
  13. *
  14. * You will probably write many "helper" functions in order to
  15. * simplify the code in readTree and writeTree.
  16. */
  17. Tree * Tree_insert(Tree * root, int val)
  18. /*
  19. * Hint: read 10.2.2 of the course handout.
  20. */
  21. {
  22. return root;
  23. }
  24. Tree * Tree_delete(Tree * root, int val)
  25. /*
  26. * Hint: Check the last question in the final exam of Fall
  27. * 2012. Blackboard - Past Exams - FinalFall2012.pdf.
  28. *
  29. * When deleting a node, we have to consider different scenarios:
  30. *
  31. * 1. If this node has no child, release the memory occupied by this
  32. * node.
  33. *
  34. * 2. If this node has only one child, make this node's parent * point
  35. * to this node's child and release the memory occupied by this * node.
  36. *
  37. * 3. If this node has two children, find this node's successor. The
  38. * successor is the node that appears immediately after this node in
  39. * in-order traversal. The successor must be on the right side of this
  40. * node. Exchange the values of this node and the successor. Delete
  41. * the successor.
  42. *
  43. */
  44. {
  45. return root;
  46. }
  47. Tree * readTree(char * infile)
  48. /*
  49. * The function opens a file whose name is given by the argument.
  50. * If fopen fails, this function returns NULL.
  51. *
  52. * The input file's format:
  53. * Each line contains a command an an integer.
  54. * c n
  55. * c is a command, either i (insert) or r (remove)
  56. * n is an integer
  57. *
  58. * If the input file contains an empty line (i.e., does not have a
  59. * command and an integer), the tree is not changed.
  60. *
  61. * You do not need to worry about the possibility of an invalid
  62. * command. For example,
  63. *
  64. * m 34
  65. *
  66. * will not occur in the input file.
  67. *
  68. * The function inserts or deletes the numbers based on the commands
  69. * in the input file. The function returns the binary search tree
  70. * built based on these commands.
  71. *
  72. * Remember to close the file.
  73. *
  74. * Hint: The file may be empty. In this case, the function should
  75. * return NULL. It is possible that the first command is r. Your
  76. * program must not crash when deleting a number from an empty tree.
  77. *
  78. */
  79. {
  80. return NULL;
  81. }
  82. int preOrder(char * outfile, Tree * root)
  83. {
  84. return EXIT_SUCCESS;
  85. }
  86. int inOrder(char * outfile, Tree * root)
  87. {
  88. return EXIT_SUCCESS;
  89. }
  90. int postOrder(char * outfile, Tree * root)
  91. {
  92. return EXIT_SUCCESS;
  93. }
  94. int writeTree(char * outfile, Tree * root)
  95. /*
  96. * If the tree is empty, the function creates an empty file
  97. * and returns EXIT_SUCCESS.
  98. *
  99. * The function opens a file whose name is given by the argument.
  100. *
  101. * If fopen fails, this function returns EXIT_FAILURE;
  102. *
  103. * The function writes the root in three traverse orders:
  104. * pre-order, in-order, and post-order.
  105. *
  106. * The function closes the file and returns EXIT_SUCCESS.
  107. *
  108. * Hint: You may open the output file multiple times. The first fopen
  109. * needs to use "w" and the following fopen should use "a" so that the
  110. * previously written content is not erased.
  111. *
  112. */
  113. {
  114. return EXIT_SUCCESS;
  115. }