Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. struct Node{
  7.     int val;
  8.    struct Node* left, *right;
  9. };
  10.  
  11. struct Node* newNode(int val) {
  12.     struct Node* node = malloc(sizeof(struct Node));
  13.     node->val = val;
  14.     node->left = node->right = NULL;
  15.     return node;
  16. }
  17.  
  18. void inorder(struct Node* root) {
  19.     if (!root) return;
  20.     inorder(root->left);
  21.     printf("%d\n",  root->val);
  22.     inorder(root->right);
  23. }
  24.  
  25. void inorder2(struct Node* root) {
  26.     struct Node* s[20];
  27.     int pos = -1;
  28.     while(root != NULL || pos >= 0) {
  29.         while(root != NULL) {
  30.             s[++pos] = root;
  31.             root = root->left;
  32.         }
  33.         root = s[pos--];
  34.         printf("%d\n",  root->val);
  35.         root = root->right;
  36.     }
  37. }
  38.  
  39. void inorder3(struct Node* root) {
  40.     while(root != NULL) {
  41.         if (root->left == NULL) {
  42.             printf("%d\n",  root->val);
  43.             root = root->right;
  44.         } else {
  45.             struct Node* pre = root->left;
  46.             while(pre->right!= NULL && pre->right!= root) {
  47.                 pre = pre->right;
  48.             }
  49.             if (pre->right == root) {
  50.                 printf("%d\n",  root->val);
  51.                 pre->right = NULL;
  52.                 root = root->right;
  53.             } else {
  54.                 pre->right = root;
  55.                 root= root->left;
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. void preorder(struct Node* root) {
  62.     if (!root) return;
  63.     printf("%d\n",  root->val);
  64.     preorder(root->left);
  65.     preorder(root->right);
  66. }
  67.  
  68. void preorder2(struct Node* root) {
  69.     struct Node* s[100];
  70.     int pos = -1;
  71.     while(root != NULL || pos >= 0) {
  72.         if (root != NULL) {
  73.             printf("%d\n",  root->val);
  74.             s[++pos] = root->right;
  75.             root = root->left;
  76.         } else {
  77.             root = s[pos--];
  78.         }
  79.     }
  80. }
  81.  
  82. void postorder(struct Node* root) {
  83.     if (!root) return;
  84.     postorder(root->left);
  85.     postorder(root->right);
  86.     printf("%d\n",  root->val);
  87. }
  88. struct item{
  89.     struct Node* node;
  90.     int visiting;
  91. };
  92.  
  93. struct item * newItem(struct Node* node) {
  94.     struct item * it = malloc(sizeof(struct item));
  95.     it->node = node;
  96.     it->visiting = 1;
  97.     return it;
  98. }
  99.  
  100. void postorder2(struct Node* root) {
  101.     struct item * s[100];
  102.     int pos = -1;
  103.     while(root != NULL || pos >= 0) {
  104.         if (root != NULL) {
  105.             s[++pos] = newItem(root);
  106.             s[pos]->visiting = 0;
  107.             if (root->right != NULL)
  108.                 s[++pos] = newItem(root->right);
  109.             root = root->left;
  110.         } else {
  111.             struct item * it = s[pos--];
  112.             root = it->node;
  113.             if (it->visiting == 1) {
  114.                 s[++pos] = newItem(root);
  115.                 s[pos]->visiting = 0;
  116.                 if (root->right != NULL)
  117.                     s[++pos] = newItem(root->right);
  118.                 root = root->left;
  119.             } else {
  120.                 printf("%d\n",  root->val);
  121.                 root = NULL;
  122.             }
  123.         }
  124.     }
  125. }
  126. #define N 10
  127. void next_permutation(int a[N]) {
  128.     int p1 = -1;
  129.     for(int i=N-1; i>0; --i) {
  130.         if (a[i-1] < a[i]) {
  131.             p1 = i-1;
  132.             break;
  133.         }
  134.     }
  135.     if (p1 == -1) return;
  136.     int p2 = p1 + 1;
  137.     for(int i=p1+1; i<N; ++i) {
  138.         if (a[i] > a[p1] && a[i] < a[p2]) p2 = i;
  139.     }
  140.     int tmp = a[p2];
  141.     a[p2] = a[p1];
  142.     a[p1] = tmp;
  143.     int L,  R;
  144.     L = p1+1;
  145.     R = N-1;
  146.     while(L < R) {
  147.         int tmp = a[R];
  148.         a[R] = a[L];
  149.         a[L] = tmp;
  150.         ++L; --R;
  151.     }
  152. }
  153.  
  154.  
  155. void shuffle(int a[N]) {
  156.     for(int i=N-1; i>0; --i) {
  157.         int pos = ((int)rand()) % (i+1);
  158.         // swap a[pos],  a[i]
  159.         int tmp = a[i];
  160.         a[i] = a[pos];
  161.         a[pos] = tmp;
  162.     }
  163. }
  164.  
  165. void shuffle2(int a[N]) {
  166.     for(int i=1; i<N; ++i) {
  167.         int pos = ((int)rand()) % (i+1);
  168.         // swap a[pos],  a[i]
  169.         int tmp = a[i];
  170.         a[i] = a[pos];
  171.         a[pos] = tmp;
  172.     }
  173. }
  174.  
  175. int main(int argc, const char *argv[])
  176. {
  177.     int a[10] = {0,  1, 2, 3, 4, 5, 6, 7, 8, 9};
  178.     for(int j=0; j<100; ++j) {
  179.         shuffle2(a);
  180.     for(int i=0; i<N; ++i) {
  181.         printf("%d, ",  a[i]);
  182.     }
  183.     printf("\n");
  184.     }
  185.     return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement