Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node{
- int elemento;
- struct node *left;
- struct node *right;
- }Node;
- Node *initialize(){
- return NULL;
- }
- Node *insert(Node *root, int x){
- Node *novo;
- novo = malloc(sizeof(Node));
- novo->elemento = x;
- if(root == NULL){
- novo->right = NULL;
- novo->left = NULL;
- return novo;
- }
- else{
- if(novo->elemento >= root->elemento){
- root->right = insert(root->right,x);
- }
- else if(novo->elemento < root->elemento){
- root->left = insert(root->left,x);
- }
- }
- }
- void freeTree(Node *root){
- if(root !=NULL){
- freeTree(root->left);
- freeTree(root->right);
- free(root);
- }
- }
- void printTree(Node *root){
- printf(" (");
- if(root !=NULL){
- printf(" %d ",root->elemento);
- printTree(root->left);
- printTree(root->right);
- }
- printf(") ");
- }
- int main(){
- int x,i;
- Node *root = initialize();
- while(scanf("%d ",&x) != EOF){
- printf("----\n");
- printf("Adicionando %d\n", x);
- root = insert(root,x);
- printf(" ");
- printTree(root);
- printf("\n");
- }
- printf("----\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement