Advertisement
Guest User

dżewa

a guest
Dec 9th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <string.h>
  4. #include <cstring>
  5. #include <iomanip>
  6.  
  7. #include "Header.h"
  8.  
  9. int main() {
  10.     NAPIS* root = nullptr;
  11.  
  12.     char* tab[] = { "Basia", "Ala", "Ola", "Zosia", "Kasia", "Helga", "Magda", "Grazyna", "Natalia", "Partycja", "Wiktoria"};
  13.     for (auto i : tab) {
  14.         dodaj(root, i);
  15.     }
  16.  
  17.     wypisz(root, 0);
  18.  
  19.     usun(root);
  20.  
  21.     wypisz(root, 0);
  22.  
  23.     usun(root);
  24.  
  25.     std::cin.get();
  26. }
  27.  
  28. //HEADER
  29.  
  30. #pragma once
  31.  
  32. struct NAPIS {
  33.     char* nap;
  34.     NAPIS* left;
  35.     NAPIS* right;
  36. };
  37.  
  38. void dodaj(NAPIS* & root, char* napis);
  39.  
  40. void wypisz(NAPIS* root, int wciecie);
  41.  
  42. void usun(NAPIS* & root);
  43.  
  44. //FUNKCJE
  45.  
  46. #include <iostream>
  47. #include <iomanip>
  48.  
  49. #include "Header.h"
  50.  
  51. void dodaj(NAPIS* & root, char* napis) {
  52.     if (root == nullptr) {
  53.         root = new NAPIS{ nullptr, nullptr, nullptr };
  54.         root->nap = new char[strlen(napis) + 1];
  55.         strcpy_s(root->nap, strlen(napis) + 1, napis);
  56.     }
  57.     else {
  58.         if (strcmp(napis, root->nap) <= 0) {
  59.             dodaj(root->left, napis);
  60.         }
  61.         else {
  62.             dodaj(root->right, napis);
  63.         }
  64.     }
  65. }
  66.  
  67. void wypisz(NAPIS* root, int wciecie) {
  68.     if (root) {
  69.         wypisz(root->left, wciecie + 10);
  70.         std::cout << std::setw(wciecie) << root->nap << std::endl;
  71.         wypisz(root->right, wciecie + 10);
  72.     }
  73. }
  74.  
  75. void usun(NAPIS* & root) {
  76.     if (root != nullptr) {
  77.         usun(root->left);
  78.         usun(root->right);
  79.         delete root->nap;
  80.         delete root;
  81.         root = nullptr;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement