tree.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* tree.h -- binary search tree */
  2. /* no duplicate items are allowed in this tree */
  3. #ifndef _TREE_H_
  4. #define _TREE_H_
  5. #include <stdbool.h>
  6. /* redefine Item as appropriate */
  7. #define SLEN 20
  8. typedef struct item
  9. {
  10. char petname[SLEN];
  11. char petkind[SLEN];
  12. } Item;
  13. #define MAXITEMS 10
  14. typedef struct trnode
  15. {
  16. Item item;
  17. struct trnode * left; /* pointer to right branch */
  18. struct trnode * right; /* pointer to left branch */
  19. } Trnode;
  20. typedef struct tree
  21. {
  22. Trnode * root; /* pointer to root of tree */
  23. int size; /* number of items in tree */
  24. } Tree;
  25. /* function prototypes */
  26. /* operation: initialize a tree to empty */
  27. /* preconditions: ptree points to a tree */
  28. /* postconditions: the tree is initialized to empty */
  29. void InitializeTree(Tree * ptree);
  30. /* operation: determine if tree is empty */
  31. /* preconditions: ptree points to a tree */
  32. /* postconditions: function returns true if tree is */
  33. /* empty and returns false otherwise */
  34. bool TreeIsEmpty(const Tree * ptree);
  35. /* operation: determine if tree is full */
  36. /* preconditions: ptree points to a tree */
  37. /* postconditions: function returns true if tree is */
  38. /* full and returns false otherwise */
  39. bool TreeIsFull(const Tree * ptree);
  40. /* operation: determine number of items in tree */
  41. /* preconditions: ptree points to a tree */
  42. /* postconditions: function returns number of items in */
  43. /* tree */
  44. int TreeItemCount(const Tree * ptree);
  45. /* operation: add an item to a tree */
  46. /* preconditions: pi is address of item to be added */
  47. /* ptree points to an initialized tree */
  48. /* postconditions: if possible, function adds item to */
  49. /* tree and returns true; otherwise, */
  50. /* the function returns false */
  51. bool AddItem(const Item * pi, Tree * ptree);
  52. /* operation: find an item in a tree */
  53. /* preconditions: pi points to an item */
  54. /* ptree points to an initialized tree */
  55. /* postconditions: function returns true if item is in */
  56. /* tree and returns false otherwise */
  57. bool InTree(const Item * pi, const Tree * ptree);
  58. /* operation: delete an item from a tree */
  59. /* preconditions: pi is address of item to be deleted */
  60. /* ptree points to an initialized tree */
  61. /* postconditions: if possible, function deletes item */
  62. /* from tree and returns true; */
  63. /* otherwise the function returns false*/
  64. bool DeleteItem(const Item * pi, Tree * ptree);
  65. /* operation: apply a function to each item in */
  66. /* the tree */
  67. /* preconditions: ptree points to a tree */
  68. /* pfun points to a function that takes*/
  69. /* an Item argument and has no return */
  70. /* value */
  71. /* postcondition: the function pointed to by pfun is */
  72. /* executed once for each item in tree */
  73. void Traverse (const Tree * ptree, void (* pfun)(Item item));
  74. /* operation: delete everything from a tree */
  75. /* preconditions: ptree points to an initialized tree */
  76. /* postconditions: tree is empty */
  77. void DeleteAll(Tree * ptree);
  78. #endif