#include "stdio.h" #include "stdlib.h" typedef struct block { int d; block *l; block *r; } block; block **initTree() { block **t = (block**)malloc(sizeof(block*)); return t; } block *createBlock(int data) { block *n = (block*)malloc(sizeof(block)); n->d = data; n->l = n->r = NULL; return n; } void insertLeft(block **t, int data) { if ((*t)->l != NULL) { printf("Ja existe bloco na parte esquerda\n"); return; } block *n = createBlock(data); (*t)->l = n; } void insertRight(block **t, int data) { if ((*t)->r != NULL) { printf("Ja existe bloco na parte direita\n"); return; } block *n = createBlock(data); (*t)->r = n; } void insertRoot(block **t, int data) { /*if (t != NULL) { printf("Ja existe um bloco para essa raiz\n"); return; }*/ block *n = createBlock(data); (*t) = n; } void showPrefixed(block *t) { if (t != NULL) { printf("%d\n", t->d); showPrefixed(t->l); showPrefixed(t->r); } } void showInfixed(block *t) { if (t != NULL) { showPrefixed(t->l); printf("%d\n", t->d); showPrefixed(t->r); } } void insertSorted(block **t, int data) { if (*t == NULL) *t = createBlock(data); else if (data < (*t)->d) insertSorted(&(*t)->l, data); else if (data > (*t)->d) insertSorted(&(*t)->r, data); } void insertSorted2(block *t, int data) { if (t == NULL) t = createBlock(data); else if (data < (t)->d) insertSorted(&(t)->l, data); else if (data > (t)->d) insertSorted(&(t)->r, data); } void temp1(int *a) { int *b = (int*) malloc(sizeof(int)); b[0] = 1000; a = b; } void temp2(int **a) { int *b = (int*) malloc(sizeof(int)); b[0] = 2000; *a = b; } int main() { block *t = NULL; /*t = initTree(); insertRoot(t, 10); insertLeft(t, 5); insertRight(t, 15);*/ showPrefixed(t); insertSorted(&t, 10); insertSorted(&t, 5); insertSorted(&t, 15); showInfixed(t); /*block *n = NULL; insertSorted2(n, 100); insertSorted2(n, 50); insertSorted2(n, 150); showInfixed(n); int *t1 = NULL; temp1(t1); //printf("%d\n", t1[0]); int *t2; temp2(&t2); printf("%d\n", t2[0]);*/ return 0; }