Advertisement
Loesome

main.cpp

Apr 4th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include "BinNode.h"
  2. #include <iostream>
  3. #include <QString>
  4.  
  5. using namespace std;
  6. template<typename T>
  7.  
  8. void printTree(BinNode<T> *node, int level = 0)
  9. {
  10. if(node != 0)
  11. {
  12. printTree(node->left, level +1);
  13. cout << qPrintable(QString("\t").repeated(level)) << node->element <<endl;
  14. printTree(node->right,level +1);
  15. }
  16. }
  17. template<typename T>
  18. int HeightTree(BinNode<T> *node)
  19. {
  20. if(node != 0)
  21. {
  22. return 1 + qMax(HeightTree(node->left),HeightTree(node->right));
  23. }
  24. else
  25. {
  26. return 0;
  27. }
  28. }
  29. template<typename T>
  30. int leafsTree(BinNode<T> *node)
  31. {if(node != 0)
  32. {
  33. if(node->right == 0 && node->left == 0)
  34. {
  35. return 1;
  36. }
  37. else
  38. {
  39. return leafsTree(node->left) + leafsTree(node->right);
  40. }}
  41. else{
  42. return 0;
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48. // 1
  49. // 2 3
  50. //4 5 6 7
  51. BinNode<int> * root = new BinNode<int>(1,
  52. new BinNode<int>(2,
  53. new BinNode<int>(4),
  54. new BinNode<int>(5)),
  55. new BinNode<int>(3,
  56. new BinNode<int>(6),
  57. new BinNode<int>(7)));
  58.  
  59. printTree(root);
  60. cout << "Numero de Leveis: "<< HeightTree(root) << endl;
  61. cout << "Numero de Folhas: "<< leafsTree(root) << endl;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement