Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //last
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct tree{
- int key;
- struct tree *left, *right, *parent;
- };
- int cmp(const void *x1, const void *x2){
- return (*(int*)x1 - *(int*)x2);
- }
- struct tree *new_(int *a, int l, int r, struct tree *par){
- if (l == r) return NULL;
- struct tree *non = malloc(sizeof(struct tree));
- if (r - l == 1){
- non -> key = a[l];
- non -> left = NULL;
- non -> right = NULL;
- non -> parent = par;
- return non;
- }
- non -> left = new_(a, l, (l + r) / 2, non);
- non -> right = new_(a, (l + r) / 2 + 1, r, non);
- non -> key = a[(l + r) / 2];
- non -> parent = par;
- return non;
- };
- struct tree *distrib(int *a, int n){
- qsort(a, n, sizeof(int), cmp);
- return new_(a, 0, n, NULL);
- };
- void prin(struct tree *root){
- if (!root) return;
- prin(root -> left);
- printf("%d ", root -> key);
- prin(root -> right);
- }
- int main(void){
- int a[10000];
- int n;
- scanf("%d", &n);
- for (int i = 0; i < n; i++)
- scanf("%d", &a[i]);
- struct tree *root = distrib(a, n);
- prin(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment