Testing program for my Binary tree implementation


/ Published in: C++
Save to your folder(s)

Here is test program using all functions in my headers.


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <cmath>
  3. #include "polje.h"
  4. #include "pokazivac.h"
  5.  
  6. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  7.  
  8. using namespace stablo_polje;
  9. using namespace std;
  10.  
  11.  
  12. template<typename labeltype>
  13. void napuni_stablo(int cvor, int dubina, bt<labeltype>&stablo)
  14. {
  15. if(dubina<=0) return;
  16. cout << "Cvor: " << cvor << ", labela: " << LabelB(cvor,stablo) << endl;
  17. CreateLeftB((float)dubina,cvor,stablo);
  18. CreateRightB((float)dubina,cvor,stablo);
  19. napuni_stablo(LeftChildB(cvor,stablo),dubina-1,stablo);
  20. napuni_stablo(RightChildB(cvor,stablo),dubina-1,stablo);
  21. }
  22.  
  23.  
  24. int main() {
  25.  
  26. bt <float>stablo;
  27.  
  28. InitB(50.0f,stablo);
  29. napuni_stablo(1,5,stablo);
  30. cout << "Stablo kreirano.\n";
  31.  
  32. int argnode=6;
  33. cout << "Parent (" << argnode << ")=" << ParentB(argnode,stablo) << endl;
  34. cout << "LeftChild (" << argnode << ")=" << LeftChildB(argnode,stablo) << endl;
  35. cout << "RightChild (" << argnode << ")=" << RightChildB(argnode,stablo) << endl;
  36. cout << "Labela (" << argnode << ")=" << LabelB(argnode,stablo) << endl;
  37. ChangeLabelB(11.0f,argnode,stablo);
  38. cout << "Promjenjena labela (" << argnode << ")=" << LabelB(argnode,stablo) << endl;
  39. cout << "Root: " << RootB(stablo) << endl;
  40. argnode=10;
  41. cout << "LeftChild (" << argnode << ")=" << LeftChildB(argnode,stablo) << endl;
  42. if(CreateLeftB(100.0f,argnode,stablo))
  43. cout << "Kreirano lijevo djete na " << argnode << ".\n";
  44. else cout << "Nije uspjelo kreiranje lijevog djeteta na " << argnode << ".\n";
  45. cout << "LeftChild (" << argnode << ")=" << LeftChildB(argnode,stablo) << endl;
  46.  
  47. cout << "RightChild (" << argnode << ")=" << RightChildB(argnode,stablo) << endl;
  48. if(CreateRightB(100.0f,argnode,stablo))
  49. cout << "Kreirano desno djete na " << argnode << ".\n";
  50. else cout << "Nije uspjelo kreiranje desnog djeteta na " << argnode << ".\n";
  51. cout << "RightChild (" << argnode << ")=" << RightChildB(argnode,stablo) << endl;
  52. cout << "Labela djece: " << LabelB(LeftChildB(argnode,stablo),stablo) << ", ";
  53. cout << LabelB(RightChildB(argnode,stablo),stablo) << ".\n";
  54.  
  55. cout << "Brisanje 4...\n";
  56. argnode=4;
  57. DeleteB(argnode,stablo);
  58. cout << "Labela djece cetvorke: " << LabelB(LeftChildB(argnode,stablo),stablo) << ", ";
  59. cout << LabelB(RightChildB(argnode,stablo),stablo) << ".\n";
  60.  
  61. cout << "Inicijalizacija..." << endl;
  62. InitB(50.0f,stablo);
  63. cout << "Root label: " << LabelB(RootB(stablo),stablo) << endl;
  64. argnode=1;
  65. cout << "Labela djece korjena: " << LabelB(LeftChildB(argnode,stablo),stablo) << ", ";
  66. cout << LabelB(RightChildB(argnode,stablo),stablo) << ".\n";
  67.  
  68.  
  69. system("PAUSE");
  70. return 0;
  71. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.