Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <string.h>
- #include <cstring>
- #include <iomanip>
- #include "Header.h"
- int main() {
- NAPIS* root = nullptr;
- char* tab[] = { "Basia", "Ala", "Ola", "Zosia", "Kasia", "Helga", "Magda", "Grazyna", "Natalia", "Partycja", "Wiktoria"};
- for (auto i : tab) {
- dodaj(root, i);
- }
- wypisz(root, 0);
- usun(root);
- wypisz(root, 0);
- usun(root);
- std::cin.get();
- }
- //HEADER
- #pragma once
- struct NAPIS {
- char* nap;
- NAPIS* left;
- NAPIS* right;
- };
- void dodaj(NAPIS* & root, char* napis);
- void wypisz(NAPIS* root, int wciecie);
- void usun(NAPIS* & root);
- //FUNKCJE
- #include <iostream>
- #include <iomanip>
- #include "Header.h"
- void dodaj(NAPIS* & root, char* napis) {
- if (root == nullptr) {
- root = new NAPIS{ nullptr, nullptr, nullptr };
- root->nap = new char[strlen(napis) + 1];
- strcpy_s(root->nap, strlen(napis) + 1, napis);
- }
- else {
- if (strcmp(napis, root->nap) <= 0) {
- dodaj(root->left, napis);
- }
- else {
- dodaj(root->right, napis);
- }
- }
- }
- void wypisz(NAPIS* root, int wciecie) {
- if (root) {
- wypisz(root->left, wciecie + 10);
- std::cout << std::setw(wciecie) << root->nap << std::endl;
- wypisz(root->right, wciecie + 10);
- }
- }
- void usun(NAPIS* & root) {
- if (root != nullptr) {
- usun(root->left);
- usun(root->right);
- delete root->nap;
- delete root;
- root = nullptr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement