Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_DEPRECATE
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int key;
- struct node* left;
- struct node* right;
- };
- struct node* createnode(int n){
- struct node* newnode = (struct node*)malloc(sizeof(struct node));
- newnode->key = n;
- newnode->left = NULL;
- newnode->right = NULL;
- return newnode;
- }
- struct node* add(struct node* node, int n) {
- if (node == NULL) return;
- if (n < node->key) {
- if (!node->left) node->left = createnode(n);
- else add(node->left, n);
- }
- if (n > node->key) {
- if (!node->right) node->right = createnode(n);
- else add(node->right, n);
- }
- }
- int height(struct node* node) {
- if (node == NULL) return 0;
- else {
- int lheight = height(node->left);
- int rheight = height(node->right);
- if (lheight > rheight) return(lheight + 1);
- else return(rheight + 1);
- }
- }
- int size(struct node* node) {
- if (!node) return 0;
- return size(node->left) + size(node->right) + 1;
- }
- int count(struct node* node) {
- if (!node) return 0;
- if ((node->left && !node->right) || (!node->left && node->right)) return count(node->left) + count(node->right) + 1;
- return count(node->left) + count(node->right);
- }
- int LeafCount(struct node* node) {
- if (node == NULL) return 0;
- if (node->left == NULL && node->right == NULL) return 1;
- else return LeafCount(node->left) + LeafCount(node->right);
- }
- void printTree(struct node* node, int odstep, int poziom) {
- if (node == NULL) return;
- printTree(node->right, odstep, poziom + 1);
- printf("\n");
- for (int i = 0; i < poziom * odstep; i++)
- printf(" ");
- printf("%d\n", node->key);
- printTree(node->left, odstep, poziom + 1);
- }
- void deltree(struct node* node) {
- if (node != NULL) {
- deltree(node->left);
- deltree(node->right);
- free(node);
- }
- }
- int main() {
- struct node* root = NULL;
- int k;
- FILE* plik = fopen("test.txt", "r");
- if (plik == NULL)
- {
- printf("File does not exist");
- exit(1);
- }
- int counter = 0;
- while (!feof(plik)) {
- if (fscanf(plik, "%d", &k)) {
- if (root == NULL) root = createnode(k);
- else add(root, k);
- counter++;
- }
- else {
- char c;
- fscanf(plik, "%c", &c);
- }
- }
- if (counter >= 1000) {
- printf("Liczba liczb pobierana z pliku przekroczyla zakres n<=1000");
- deltree(root);
- root = NULL;
- exit(1);
- }
- fclose(plik);
- int a = 0;
- while (a != 7) {
- printf("\n\n");
- printf("Nacisnij:\n");
- printf("1: Jezeli chcesz wyswietlic drzewo z pliku.\n");
- printf("2: Jezeli chcesz wyswietlic wysokosc drzewa.\n");
- printf("3: Jezeli chcesz wyswietlic ilosc lisci drzewa.\n");
- printf("4: Jezeli chcesz wyswietlic ilosc wezlow z jednym potomkiem.\n");
- printf("5: Jezeli chcesz wyswietlic wielkosc drzewa.\n");
- printf("6: Jezeli chcesz usunac drzewo.\n");
- printf("7: Jezeli chcesz zamknac program.\n");
- scanf("%d", &a);
- if (a == 1 || a == 2 || a == 3 || a == 4 || a == 5 || a == 6 || a == 7) {
- if (a == 1) {
- system("cls");
- printTree(root, 5, 2);
- }
- if (a == 2) {
- system("cls");
- printf("Wysokosc drzewa: %d\n", height(root));
- }
- if (a == 3) {
- system("cls");
- printf("Ilosc lisci w drzewie:%d\n", LeafCount(root));
- }
- if (a == 4) {
- system("cls");
- printf("Ilosc wezlow z jednym potomkiem:%d\n", count(root));
- }
- if (a == 5) {
- system("cls");
- printf("Wielkosc drzewa:%d\n", size(root));
- }
- if (a == 6) {
- system("cls");
- printf("Drzewo usuniete\n");
- deltree(root);
- root = NULL;
- }
- }
- else {
- system("cls");
- printf("Wybrana zostala zla cyfra:");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement