Advertisement
Guest User

asdfasdf

a guest
Mar 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include "bintree.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <queue>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8. using namespace main_savitch_10;
  9.  
  10. int h;
  11.  
  12.  
  13. binary_tree_node<char>* create_tree ();
  14. // return: pointer root of binary search tree of integers
  15.  
  16. template <typename T>
  17. int height(const binary_tree_node<T>* t){
  18.  
  19. if (t == NULL)
  20. return 0;
  21. else
  22. {
  23. /* compute the height of each subtree */
  24. int lheight = height(t->left( ));
  25. int rheight = height(t->right( ));
  26.  
  27. /* use the larger one */
  28. if (lheight > rheight)
  29. return(lheight + 1);
  30. else return(rheight + 1);
  31. }
  32. }
  33.  
  34. template <typename T>
  35. void printGivenLevel(const binary_tree_node<T>* t, int level){
  36. if (t == NULL)
  37. return;
  38. if (level == 1)
  39. cout << t->data( ) << " ";
  40. else if (level > 1)
  41. {
  42. printGivenLevel(t->left( ), level-1);
  43. printGivenLevel(t->right( ), level-1);
  44. }
  45. }
  46.  
  47. template <typename T>
  48. void display_complete_tree (const binary_tree_node<T>* t){
  49. h = height(t);
  50. int i = 0;
  51. for (i = 1; i <= h; i++){
  52. printGivenLevel(t, i);
  53. cout<<endl;
  54. }
  55.  
  56. }
  57.  
  58.  
  59. int main ()
  60. {
  61. binary_tree_node<char>* t = create_tree ();
  62. display_complete_tree(t);
  63.  
  64.  
  65.  
  66.  
  67. return EXIT_SUCCESS;
  68. }
  69.  
  70. binary_tree_node<char>* create_tree()
  71. {
  72. binary_tree_node<char>* t1 = new binary_tree_node<char>('A');
  73. binary_tree_node<char>* t2 = new binary_tree_node<char>('Q');
  74. binary_tree_node<char>* t3 = new binary_tree_node<char>('W');
  75. binary_tree_node<char>* t4 = new binary_tree_node<char>('E',t1,NULL);
  76. binary_tree_node<char>* t5 = new binary_tree_node<char>('H',t4,NULL);
  77. binary_tree_node<char>* t6 = new binary_tree_node<char>('P',NULL,t2);
  78. binary_tree_node<char>* t7 = new binary_tree_node<char>('T',t6,t3);
  79. binary_tree_node<char>* t8 = new binary_tree_node<char>('M',t5,t7);
  80. return t8;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement