Advertisement
eniodordan

[AISP] LV4

Dec 16th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Maximum stack size
  5. #define MAX_SIZE 100
  6.  
  7. struct cvor{ char x; struct cvor *left, *right; }; //Deklaracija strukture
  8. struct Stack
  9.     {
  10.         int size;
  11.         int top;
  12.         struct cvor* *array;
  13.     };
  14. //struct cvor{ char x; struct cvor *left, *right; }; //Deklaracija strukture
  15.     void ubaci(struct cvor *r, struct cvor *p){
  16.     if ((r->right == NULL) && (p->x > r->x)){ r->right = p; }
  17.     //Funkcija za dodavanje èvora u stablo
  18.     else if ((r->right != NULL) && (p->x > r->x)){ ubaci(r->right, p); }
  19.     //Sluèaj kada je vrijednost novog èvora veæa od nadreðenog.
  20.         //Sluèaj kada je vrijednost novog èvora veæa od nadreðenog.
  21.         if ((r->left == NULL) && (p->x < r->x)){ r->left = p; }
  22.     //Sluèaj kada je vrijednost novog èvora manja od nadreðenog.
  23.         else if ((r->left != NULL) && (p->x < r->x)){ ubaci(r->left, p); }
  24.     //Sluèaj kada je vrijednost novog èvora manja od nadreðenog.
  25. } //zavrsetak funkcije ubaci()
  26.  
  27.     int isFull(struct Stack* stack)
  28.     {
  29.         return stack->top - 1 == stack->size;
  30.     }
  31.    
  32.     int isEmpty(struct Stack* stack)
  33.     {
  34.         return stack->top == -1;
  35.     }
  36.    
  37.     void push(struct Stack* stack, struct cvor* node)
  38.     {
  39.         if (isFull(stack))
  40.             return;
  41.         stack->array[++stack->top] = node;
  42.     }
  43.    
  44.     struct cvor* pop(struct Stack* stack)
  45.     {
  46.         if (isEmpty(stack))
  47.             return NULL;
  48.         return stack->array[stack->top--];
  49.     }
  50.    
  51.     struct cvor* peek(struct Stack* stack)
  52.     {
  53.         if (isEmpty(stack))
  54.             return NULL;
  55.         return stack->array[stack->top];
  56.     }
  57.    
  58.     struct Stack* createStack(int size)
  59.     {
  60.         struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
  61.         stack->size = size;
  62.         stack->top = -1;
  63.         stack->array = (struct cvor**) malloc(stack->size * sizeof(struct cvor*));
  64.         return stack;
  65.     }
  66.  
  67.     void preOrder(struct cvor* root){ if (root == NULL) return;   else{ printf("%c ", root->x);   preOrder(root->left);   preOrder(root->right); } }
  68.  
  69.  
  70.     void inOrder(struct cvor* root){ if (root == NULL) return;   else{ inOrder(root->left);   printf("%c ", root->x);   inOrder(root->right); } }
  71.  
  72.  
  73.     void postOrderIterative(struct cvor* root)
  74.         {
  75.             // Check for empty tree
  76.             if (root == NULL)
  77.                 return;
  78.        
  79.             struct Stack* stack = createStack(MAX_SIZE);
  80.             do
  81.             {
  82.                 // Move to leftmost node
  83.                 while (root)
  84.                 {
  85.                     // Push root's right child and then root to stack.
  86.                     if (root->right)
  87.                         push(stack, root->right);
  88.                     push(stack, root);
  89.        
  90.                     // Set root as root's left child  
  91.                     root = root->left;
  92.                 }
  93.        
  94.                 // Pop an item from stack and set it as root    
  95.                 root = pop(stack);
  96.        
  97.                 // If the popped item has a right child and the right child is not
  98.                 // processed yet, then make sure right child is processed before root
  99.                 if (root->right && peek(stack) == root->right)
  100.                 {
  101.                     pop(stack);  // remove right child from stack
  102.                     push(stack, root);  // push root back to stack
  103.                     root = root->right; // change root so that the right
  104.                     // child is processed next
  105.                 }
  106.                 else  // Else print root's data and set root as NULL
  107.                 {
  108.                     printf("%c ", root->x);
  109.                     root = NULL;
  110.                 }
  111.             } while (!isEmpty(stack));
  112.         }
  113.  
  114.  
  115.  
  116.  
  117.     struct cvor* newNode(char data)
  118.         {
  119.             struct cvor* node = (struct cvor*) malloc(sizeof(struct cvor));
  120.             node->x = data;
  121.             node->left = node->right = NULL;
  122.             return node;
  123.         }
  124.  
  125.     int main()
  126.     {
  127.        
  128.         struct cvor* root = NULL;
  129.         root = newNode('i');
  130.         struct cvor* slovo2 = newNode('m');
  131.         struct cvor* slovo3 = newNode('e');
  132.         struct cvor* slovo4 = newNode('p');
  133.         struct cvor* slovo5 = newNode('r');
  134.         struct cvor* slovo6 = newNode('e');
  135.         struct cvor* slovo7 = newNode('z');
  136.         struct cvor* slovo8 = newNode('i');
  137.         struct cvor* slovo9 = newNode('m');
  138.         struct cvor* slovo10 = newNode('e');
  139.  
  140.         ubaci(root, slovo2);
  141.         ubaci(root, slovo3);
  142.         ubaci(root, slovo4);
  143.         ubaci(root, slovo5);
  144.         ubaci(root, slovo6);
  145.         ubaci(root, slovo7);
  146.         ubaci(root, slovo8);
  147.         ubaci(root, slovo9);
  148.         ubaci(root, slovo10);
  149.  
  150.                 printf("preOrder jest:\n");
  151.         preOrder(root);
  152.                 printf("\ninOrder jest:\n");
  153.         inOrder(root);
  154.                 printf("\npostOrder jest:\n");
  155.         postOrderIterative(root);
  156.        
  157.    
  158.    
  159.         return 0;
  160.     }
  161.  
  162. // ---------------------------------------
  163.  
  164. #include "stdafx.h"
  165. #include <iostream>
  166. using namespace std;
  167.  
  168. char a[8] = { 'm','a','r','k','o','c','i','t' };
  169.  
  170.  
  171.  
  172. struct cvor {
  173.     char d;
  174.     struct cvor*desni;
  175.     struct cvor *lijevi;
  176.  
  177.  
  178. };
  179.  
  180. struct cvor *stvori_cvor(char x, struct cvor *l, struct cvor *d) {
  181.     struct cvor *t;
  182.     t = new struct cvor;
  183.     t->d = x;
  184.     t->desni = d;
  185.     t->lijevi = l;
  186.     return t;
  187. }
  188.  
  189. struct cvor* stvori_stablo(char*a, int i, int vel) {
  190.     if (i >= vel) {
  191.         return NULL;
  192.     }
  193.  
  194.     return (stvori_cvor(a[i], stvori_stablo(a, 2 * i + 1, vel), stvori_stablo(a, 2 * i + 2, vel)));
  195.  
  196.  
  197. }
  198. //
  199. ////void ubaci(struct cvor *r, struct cvor* p) {
  200. //
  201. //  if ((r->desni == NULL) && (p ->d> r->d)) {
  202. //
  203. //      r->desni = p;
  204. //  }
  205. //  else if ((r->desni != NULL) && (p->d > r->d)) {
  206. //      return ubaci(r->desni, p);
  207. //  }
  208. //  if ((r->lijevi == NULL) && (p->d < r->d)) {
  209. //      r->lijevi = p;
  210. //  }
  211. //  else if ((r->lijevi != NULL) && (p->d < r->d)) {
  212. //      ubaci(r->lijevi, p);
  213. //  }
  214. //}
  215.  
  216. void preorder(struct cvor *root) {
  217.  
  218.     if (root == NULL)return;
  219.     else {
  220.  
  221.         cout << root->d << endl;
  222.         preorder(root->lijevi);
  223.         preorder(root->desni);
  224.  
  225.     }
  226. }
  227.  
  228.  
  229.  
  230. void inorder(struct cvor *root) {
  231.  
  232.     if (root == NULL)return;
  233.     else {
  234.  
  235.         inorder(root->lijevi);
  236.         cout << root->d << endl;
  237.         inorder(root->desni);
  238.  
  239.     }
  240. }
  241.  
  242. void postorder(struct cvor *root) {
  243.  
  244.     if (root == NULL)return;
  245.     else {
  246.  
  247.         postorder(root->lijevi);
  248.         postorder(root->desni);
  249.         cout << root->d;
  250.     }
  251. }
  252.  
  253.  
  254. struct cvor *stog[8];
  255. int sp = -1;
  256.  
  257. void push(struct cvor*head) {
  258.  
  259.     sp += 1;
  260.     stog[sp] = head;
  261.  
  262. }
  263.  
  264. struct cvor*pop() {
  265.     struct cvor*ret = stog[sp];
  266.     --sp;
  267.     return ret;
  268. }
  269.  
  270. struct cvor* peek() {
  271.     return stog[sp];
  272.  
  273. }
  274.  
  275.  
  276.  
  277. void Preorder(struct cvor *glava) {
  278.  
  279.     struct cvor*p = glava;
  280.     struct cvor*p_t = glava;
  281.     push(p);
  282.     while (sp!=-1) {
  283.  
  284.         p_t = pop();
  285.         cout << p_t->d << endl;
  286.         if (p_t->desni) {
  287.             push(p_t->desni);
  288.         }
  289.         if (p_t->lijevi) {
  290.             push(p_t->lijevi);
  291.         }
  292.        
  293.     }
  294.  
  295. }
  296.  
  297. void Inorder(struct cvor *glava) {
  298.  
  299.    
  300.         cvor * p = glava;
  301.         while (true) {
  302.             while (p != NULL) {
  303.                 push(p);
  304.                 p = p->lijevi;
  305.             }
  306.             if (sp==-1) {
  307.                 break;
  308.             }
  309.             p = pop();
  310.             printf("%c\n", p->d);
  311.             p = p->desni;
  312.         }
  313.     }
  314.  
  315.  
  316.  
  317. void Postorder(struct cvor *glava) {
  318.     struct cvor*current = NULL;
  319.  
  320.     while (sp != -1) {
  321.         if (glava != NULL) {
  322.             push(glava);
  323.             glava = glava->lijevi;
  324.         }
  325.         else {
  326.             struct cvor*temporary = peek();
  327.  
  328.             if (temporary->desni != NULL && current != temporary->desni) {
  329.                 glava = temporary->desni;
  330.             }
  331.             else {
  332.                 cout << temporary->d;
  333.                 current= pop();
  334.             }
  335.         }
  336.     }
  337. }
  338.  
  339.  
  340.  
  341. int main()
  342. {
  343.     struct cvor*me;
  344.     me = stvori_stablo(a, 0, 8);
  345.    
  346.     Postorder(me);
  347.  
  348.     return 0;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement