Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BinNode.h"
- #include <iostream>
- #include <QString>
- using namespace std;
- template<typename T>
- void printTree(BinNode<T> *node, int level = 0)
- {
- if(node != 0)
- {
- printTree(node->left, level +1);
- cout << qPrintable(QString("\t").repeated(level)) << node->element <<endl;
- printTree(node->right,level +1);
- }
- }
- template<typename T>
- int HeightTree(BinNode<T> *node)
- {
- if(node != 0)
- {
- return 1 + qMax(HeightTree(node->left),HeightTree(node->right));
- }
- else
- {
- return 0;
- }
- }
- template<typename T>
- int leafsTree(BinNode<T> *node)
- {if(node != 0)
- {
- if(node->right == 0 && node->left == 0)
- {
- return 1;
- }
- else
- {
- return leafsTree(node->left) + leafsTree(node->right);
- }}
- else{
- return 0;
- }
- }
- int main()
- {
- // 1
- // 2 3
- //4 5 6 7
- BinNode<int> * root = new BinNode<int>(1,
- new BinNode<int>(2,
- new BinNode<int>(4),
- new BinNode<int>(5)),
- new BinNode<int>(3,
- new BinNode<int>(6),
- new BinNode<int>(7)));
- printTree(root);
- cout << "Numero de Leveis: "<< HeightTree(root) << endl;
- cout << "Numero de Folhas: "<< leafsTree(root) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement