Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     struct Node* left, * right;
  9.     Node(int data)
  10.     {
  11.         this->data = data;
  12.         left = right = NULL;
  13.     }
  14. };
  15.  
  16. int src(Node *root,int idata) {
  17.     if (root == NULL)
  18.         return NULL;
  19.     if (idata == root->data) {
  20.         cout << "Pronaden: "<< root->data;
  21.         return root->data;
  22.     }
  23.     if (idata < root->data) {
  24.         return src(root->left, idata);
  25.     }
  26.     if (idata > root->data) {
  27.         return src(root->right, idata);
  28.     }
  29. }
  30.  
  31. int main() {
  32.     struct Node* root = new Node(50);
  33.     root->left = new Node(17);
  34.     root->right = new Node(72);
  35.     root->left->left = new Node(12);
  36.     root->left->right = new Node(23);
  37.     root->right->left = new Node(54);
  38.     root->right->right = new Node(76);
  39.     root->left->left->left = new Node(9);
  40.     root->left->left->right = new Node(14);
  41.     root->left->right->left = new Node(19);
  42.     root->right->left->right = new Node(67);
  43.  
  44.     int idata;
  45.     cout << "Trazena vrijednost: ";
  46.     cin >> idata;
  47.     src(root, idata);
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement