Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1.  
  2. #define LINELEN 80 /* maximum length of any input line */
  3.  
  4. #define RIGHT 1 /* right traversal of tree */
  5. #define LEFT 0 /* left traversal of tree */
  6.  
  7. #define INSERT 'i' /* command to add a string */
  8. #define PRINT1 'p' /* command to do plain print */
  9. #define PRINT2 's' /* command to do snazzy print */
  10. #define ROTATE 'r' /* command to do a tree edge rotation */
  11. #define TABULA 't' /* command to tabulate statistics */
  12. #define FREEIT 'f' /* command to free all space */
  13. #define ALLOPS "irpstf" /* list of all valid operators */
  14. #define FILINP 1 /* indicates input coming from a file */
  15. #define PROMPT "> "
  16.  
  17.  
  18. /****************************************************************/
  19.  
  20. /* print out a tree, snazzy formatting */
  21. void
  22. do_print_s(tree_t *root) {
  23. /* Creates history array of max depth of tree */
  24. int maxdepth = do_t_maxdepth(root,0);
  25. int *history = (int*)malloc(maxdepth*sizeof(root->key));
  26. /* Snazzy print function part 2 */
  27. do_print_s_2(root, 0, history, -1);
  28. }
  29.  
  30. /****************************************************************/
  31.  
  32. /* Finds each word in reverse-alphabetic order,
  33. printing spaces according to it's level */
  34. void
  35. do_print_s_2(tree_t *root, int level, int *history, int lr) {
  36. int p_space = 0;
  37. /* Adds left or right traversal to history array */
  38. if (lr == RIGHT) history[level] = RIGHT;
  39. else history[level] = LEFT;
  40. /* Double recursive loop traversing down right
  41. leaves first - reverse alphabetic order */
  42. if (root != NULL){
  43. do_print_s_2(root->right, level + 1, history, RIGHT);
  44. /* Print space according to current level
  45. Five spaces for the rest of the str and prints
  46. '|' if needed */
  47. while(p_space<5*(level)-3){
  48. /* Checks if '|' is needed */
  49. if (do_print_s_3(history,p_space,level)){
  50. printf("|");
  51. p_space++;
  52. }
  53. printf(" ");
  54. p_space++;
  55. }
  56. /* Print string with +-- */
  57. if (level>0) printf("+--%s\n",root->key);
  58. else printf("%s\n",root->key);
  59. do_print_s_2(root->left,level + 1,history, LEFT);
  60. }
  61. else{
  62. /* Print NULL (empty) leaf with +-- */
  63. if (level>0){
  64. while(p_space<5*(level)-3){
  65. if (do_print_s_3(history,p_space,level)){
  66. printf("|");
  67. p_space++;
  68. }
  69. printf(" ");
  70. p_space++;
  71. }
  72. printf("+--\n");
  73. }
  74. }
  75. }
  76.  
  77. /****************************************************************/
  78.  
  79. /* Checks if | needs to be printed */
  80. int
  81. do_print_s_3(int *history,int p_space, int level){
  82. /* If tree traverses in alternating directions (i.e right then left
  83. or right then left) '|' is printed at corresponding level */
  84. if (level>0 && ((p_space+3) % 5*(level) == 0)
  85. && history[(p_space-2)/5+1] != history[(p_space-2)/5+2]){
  86. return 1;
  87. }
  88. return 0;
  89. }
  90.  
  91.  
  92. /* Find the maximum depth of tree */
  93. int
  94. do_t_maxdepth(tree_t *root,int level) {
  95. int lnum = 0, rnum = 0;
  96. /* Recursive loop adds one at end of tree */
  97. if (root != NULL) {
  98. lnum = do_t_maxdepth(root->right, level + 1);
  99. rnum = do_t_maxdepth(root->left, level + 1);
  100. }
  101. /* Returns max level, first root node is 0, however NULL
  102. nodes also add to total */
  103. if (lnum > rnum) {
  104. if (lnum > level) return lnum;
  105. }
  106. if (rnum > level) return rnum;
  107. return level;
  108. }
  109.  
  110.  
  111. /****************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement