Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct binary_tree
- {
- int value;
- struct binary_tree * leftSideOfTree;
- struct binary_tree * rightSideOfTree;
- };
- typedef struct binary_tree tree;
- struct data
- {
- int point;
- };
- typedef struct data pointer;
- tree * createTree ()
- {
- return NULL;
- }
- int ifVoid(tree * source)
- {
- return source == NULL;
- }
- void showTree(tree * source)
- {
- printf("\n<");
- if(ifVoid(source))
- {
- printf("%d\n",source->value);
- showTree(source->leftSideOfTree);
- showTree(source->rightSideOfTree);
- }
- printf(">\n");
- }
- void writeInTree(tree ** source,int tempPut)
- {
- pointer * conducter;
- conducter = (pointer*)malloc(sizeof(pointer));
- tempPut = conducter->point;
- *source = (tree*)malloc(sizeof(source));
- if(*source == NULL)
- {
- (*source)->leftSideOfTree = NULL;
- (*source)->rightSideOfTree = NULL;
- (*source)->value = tempPut;
- }else
- {
- if(tempPut >(*source)->value)
- {
- writeInTree(&(*source)->leftSideOfTree,tempPut);
- }
- if(tempPut<(*source)->value)
- {
- writeInTree(&(*source)->rightSideOfTree,tempPut);
- }
- }
- }
- void putData(pointer * conducter)
- {
- printf("Digite um numero para adicionar na arvore");
- scanf("%d",&conducter->point);
- }
- int isInTheTree (tree * source,int tempPut)
- {
- if(ifVoid(source))
- {
- return 0;
- }
- return source ->value == tempPut || isInTheTree(source->leftSideOfTree,tempPut) || isInTheTree(source->rightSideOfTree,tempPut);
- }
- int main ()
- {
- int templatenumber,resp = 0;
- pointer * conducter;
- conducter = (pointer*)malloc(sizeof(pointer));
- tree * source = createTree();
- do
- {
- system("CLS");
- printf("=================|Menu da Arvore|===========================\n");
- printf("-------------------------------------------------------------\n");
- printf("[1] - Adicionar um elemento na arvore\n");
- printf("[2] - Mostrar a Arvore\n");
- printf("[3] - Fechar o programa\n");
- printf("---------------------------------------------------------------\n");
- printf("Sua opcao:");
- scanf("%d",&resp);
- switch(resp)
- {
- case 1:
- putData(conducter);
- ifVoid(source);
- writeInTree(&source,templatenumber);
- break;
- case 2:
- isInTheTree(source,templatenumber);
- showTree(source);
- system("PAUSE");
- break;
- case 3:
- printf("Encerrando o programa\n");
- break;
- default :
- printf("Opcao invalida!\n Escolha uma opcao valida!\n");
- break;
- }
- }while(resp != 3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement