Posted By


igotepava on 01/18/15

Tagged


Statistics


Viewed 921 times
Favorited by 0 user(s)

binarno_stablo_pretrazivanja.h


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

Implementacija algoritma Binarno stablo pretraživanja.


Copy this code and paste it in your HTML
  1. bool ExistsLeftChild(node *T){
  2. if(T->left) return true;
  3. else return false;
  4. }
  5.  
  6. bool ExistsRightChild(node *T){
  7. if(T->right) return true;
  8. else return false;
  9. }
  10.  
  11. void insert_BS(int m, node *T){
  12. bool dalje = true;
  13. node *t = T;
  14. do{
  15. if(m > t->label){
  16. if(ExistsRightChild(t)) t = t->right;
  17. else {
  18. CreateRightB(m, t);
  19. dalje = false;
  20. }
  21. }
  22. else if(m < t->label){
  23. if(ExistsLeftChild(t)) t = t->left;
  24. else {
  25. CreateLeftB(m, t);
  26. dalje = false;
  27. }
  28. }
  29. else dalje = false;
  30. }while(dalje);
  31. }
  32.  
  33. void bin_search(int k, node *T){
  34. if(T->label == k){
  35. cout << "Trazeni element je pronaden!\n\n";
  36. return;
  37. }
  38. if(k > T->label){
  39. if(ExistsRightChild(T)) bin_search(k, T->right);
  40. else cout << "Trazeni element ne postoji!\n";
  41. }
  42. if(k < T->label){
  43. if(ExistsLeftChild(T)) bin_search(k, T->left);
  44. else cout << "Trazeni element ne postoji!\n";
  45. }
  46. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.