Skip to content

Instantly share code, notes, and snippets.

@encukou
Created July 15, 2020 14:33
Show Gist options
  • Save encukou/8b60e4f8a0bf783a2435887bf3d5ade0 to your computer and use it in GitHub Desktop.
Save encukou/8b60e4f8a0bf783a2435887bf3d5ade0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct node {
int number;
struct node *left;
struct node *right;
};
void print_tree(struct node *tree, int depth, char rl) {
if (tree) {
for (int i=0; i<depth; i++) {
printf(" ");
}
printf("%c %d\n", rl, tree->number);
print_tree(tree->left, depth+1, '^');
print_tree(tree->right, depth+1, 'v');
}
}
void del_tree(struct node *tree);
struct node *add_number(struct node *parent, int to_right, int num) {
struct node *result = malloc(sizeof(struct node));
result->number = num;
result->left = NULL;
result->right = NULL;
if (parent) {
if (to_right) {
del_tree(parent->right);
parent->right = result;
} else {
del_tree(parent->left);
parent->left = result;
}
}
return result;
}
void del_tree(struct node *tree) {
if (tree) {
del_tree(tree->left);
del_tree(tree->right);
free(tree);
}
}
void print_binary(int i) {
char buf[33];
buf[32] = 0;
for (int pos=0; pos < 32; pos++) {
if (i & 1) {
buf[31-pos] = '1';
} else {
buf[31-pos] = '0';
}
i >>= 1;
}
printf("%s\n", buf);
}
struct dvojice {
int prava;
int leva;
};
void vypis_dvojici(struct dvojice *p) {
printf("%d a %d\n", (*p).prava, (*p).leva);
}
void main() {
char i = 0b00000000000000000000000000000010;
char j = 0;
char *p = &i;
if (i) {
printf("Hello world!\n");
}
p -= 1;
*p += 1;
printf("%d %ld %d %d %p %p\n", i, &i, j, sizeof("ó"), &i, &j);
char hello[] = "Hi world";
printf("%s\n", hello);
printf("%c\n", hello[0]);
printf("%d\n", sizeof(hello));
for (int i=0; hello[i]; i++) {
printf("%c", hello[i]);
}
printf("\n");
for (char *p=hello; *p; p++) {
printf("%c", *p);
}
printf("\n");
print_binary(i);
struct dvojice par = {7, 8};
printf("%d a %d\n", par.prava, par.leva);
struct dvojice par2 = {9, 10};
vypis_dvojici(&par);
vypis_dvojici(&par2);
struct node *tree = add_number(NULL, 0, 3);;
struct node *l = add_number(tree, 0, 6);
struct node *r = add_number(tree, 1, 7);
add_number(l, 0, 9);
struct node *x;
add_number(x = add_number(l, 1, 10), 0, -9);
add_number(x, 1, 4);
add_number(r, 1, 2);
print_tree(tree, 0, ' ');
//print_list(tree);
del_tree(tree);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment