Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #define COUNT 10
- struct tree_node
- {
- int key;
- char value;
- struct tree_node *left_child, *right_child;
- } *root;
- void add_node(struct tree_node **root, int key, char value)
- {
- if(*root==NULL)
- {
- *root = (struct tree_node *)malloc(sizeof(struct tree_node));
- if(*root)
- {
- (*root)->key = key;
- (*root)->value = value;
- (*root)->left_child = (*root)->right_child = NULL;
- }
- }
- else if ((*root)->key >= key)
- add_node(&(*root)->left_child,key,value);
- else
- add_node(&(*root)->right_child,key,value);
- }
- void add_node_iteratively(struct tree_node **root, int key, char value)
- {
- while(*root!=NULL)
- {
- if((*root)->key>=key)
- root = &(*root)->left_child;
- else
- root = &(*root)->right_child;
- }
- *root = (struct tree_node *)
- malloc(sizeof(struct tree_node));
- if(*root)
- {
- (*root)->key = key;
- (*root)->value = value;
- (*root)->left_child = (*root)->right_child = NULL;
- }
- }
- void print_inorder(struct tree_node *root)
- {
- if(root)
- {
- print_inorder(root->left_child);
- printf("klucz: %d, wartosc: %c\n",root->key, root->value);
- print_inorder(root->right_child);
- }
- }
- void print_inorder_reverse(struct tree_node *root)
- {
- if(root)
- {
- print_inorder_reverse(root->right_child);
- printf("klucz: %d, wartosc: %c\n",root->key, root->value);
- print_inorder_reverse(root->left_child);
- }
- }
- void print_preorder(struct tree_node *root)
- {
- if(root)
- {
- printf("klucz: %d, wartosc: %c\n",root->key, root->value);
- print_preorder(root->left_child);
- print_preorder(root->right_child);
- }
- }
- void print_postorder(struct tree_node *root)
- {
- if(root)
- {
- print_postorder(root->left_child);
- print_postorder(root->right_child);
- printf("klucz: %d, wartosc: %c\n",root->key, root->value);
- }
- }
- int height(struct tree_node* root)
- {
- if (root==NULL)
- return 0;
- else
- {
- int lheight = height(root->left_child);
- int rheight = height(root->right_child);
- if (lheight > rheight)
- return(lheight+1);
- else
- return(rheight+1);
- }
- }
- struct tree_node *find_node(struct tree_node *root, int key)
- {
- if(root && root->key > key)
- return find_node(root->left_child,key);
- else if(root && root->key < key)
- return find_node(root->right_child,key);
- else
- return root;
- }
- // wyswietlanie danego poziomu
- void print_level(struct tree_node* root, int level)
- {
- if (root == NULL)
- return;
- if (level == 1)
- printf("%d ", root->key);
- else if (level > 1)
- {
- print_level(root->left_child, level-1);
- print_level(root->right_child, level-1);
- }
- }
- // wyswietlanie wszystkich poziomow
- void print_levelorder(struct tree_node* root)
- {
- int h = height(root);
- int i;
- for (i=1; i<=h; i++){
- print_level(root, i);
- puts("");
- }
- }
- void print_graphic(struct tree_node *root, int space)
- {
- int i;
- // Base case
- if (root == NULL)
- return;
- // Increase distance between levels
- space += COUNT;
- // Process right child first
- print_graphic(root->right_child, space);
- // Print current node after space
- // count
- printf("\n");
- for (i = COUNT; i < space; i++)
- printf(" ");
- printf("%d\n", root->key);
- // Process left child
- print_graphic(root->left_child, space);
- }
- void menu()
- {
- int key;
- int pick;
- puts("\n1. Wyswietlanie drzewa inorder");
- puts("2. Wyswietlanie drzewa inorder od tylu");
- puts("3. Wyswietlanie drzewa preorder");
- puts("4. Wyswietlanie drzewa postorder");
- puts("5. Wysokosc drzewa");
- puts("6. Wyszukiwanie danego elementu");
- puts("7. Wyswietlanie levelorder");
- puts("8. Wyswietlanie drzewa w postaci semi-graficznej");
- puts("0. Koniec");
- scanf("%d", &pick);
- system("cls");
- switch(pick)
- {
- case 1:
- print_inorder(root);
- puts("\n");
- menu();
- system("cls");
- break;
- case 2:
- print_inorder_reverse(root);
- puts("\n");
- menu();
- system("cls");
- break;
- case 3:
- print_preorder(root);
- puts("\n");
- menu();
- system("cls");
- break;
- case 4:
- print_postorder(root);
- puts("\n");
- menu();
- system("cls");
- break;
- case 5:
- printf("Wysokosc drzewa: %d\n", height(root));
- puts("");
- menu();
- system("cls");
- break;
- case 6:
- printf("Podaj klucz: ");
- scanf("%d", &key);
- struct tree_node *result = find_node(root,key);
- if(result){
- printf("\nSzukana wartosc dla klucza %d wynosi %c", result->key, result->value);
- }
- menu();
- case 7:
- print_levelorder(root);
- menu();
- case 8:
- print_graphic(root,0);
- menu();
- case 0:
- return;
- default:
- puts("Niepoprawny wybor");
- menu();
- break;
- }
- }
- int main()
- {
- int i;
- char value;
- int key;
- srand(time(0));
- for(i=0; i<10; i++)
- {
- key = rand()%100;
- value = 'a' + rand() % ('z'-'a'+1);
- add_node(&root, key, value);
- }
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement