Advertisement
Domy131097

[LV4] Algoritmi-Binarna Stabla

Apr 4th, 2019
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct cvor {
  7.     char x;
  8.     struct cvor *left, *right;
  9. }BS;
  10.  
  11. int pronadi(BS *r, char x) {
  12.     BS *p = r;
  13.     while (p != NULL) {
  14.         if (x > p->x) p = p->right;
  15.         else if (x < p->x) p = p->left;
  16.         else return 1;
  17.     }
  18.     return 0;
  19. }
  20.  
  21. void ubaci(BS *r, char p) {
  22.     BS *novi = (BS *)malloc(sizeof(BS)), *temp = r;
  23.     if (pronadi(r, p)) return;
  24.     novi->x = p;
  25.     novi->left = NULL;
  26.     novi->right = NULL;
  27.     if (r == NULL) r = novi;
  28.     else {
  29.         if ((temp->right == NULL) && (novi->x > temp->x)) temp->right = novi;
  30.         else if ((temp->right != NULL) && (novi->x > temp->x)) temp = temp->right;
  31.         if ((temp->left == NULL) && (novi->x < temp->x)) temp->left = novi;
  32.         else if ((temp->left != NULL) && (novi->x < temp->x)) temp = temp->left;
  33.        
  34.     }
  35. }
  36.  
  37. int main() {
  38.  
  39.     return 1;
  40. }
Advertisement
RAW Paste Data Copied
Advertisement