int main() { struct searchTree * t = initTree(); struct searchTree *root = t = treeInsert(t , 3); treeInsert(t , 8); struct searchTree * x = treeInsert(t , 4); struct searchTree * y = treeInsert(t , 14); treeInsert(t , 9); treeInsert(t , 2); treeInsert(t , 0); treeWalk(t); treeDelete(x); treeWalk(root); treeDelete(y); treeWalk(root); printf("\nmax = %d min = %d \n", treeMax(t) -> k, treeMin(t) -> k); if(treeSearch(t, 3) != NULL) printf("found\n"); printf("%d\n",t -> k); t = treeSuccessor(t); printf("%d\n",t -> k); t = treePredecessor(t); printf("%d\n",t -> k); }
/*This function takes in a pointer to a pointer to the root of a tree and a target string and deletes the target string from the tree if it exists there*/ static void treeDelete (struct node **root, const char* target) { struct node *toFree; /*pointer to the node we're deleting/freeing*/ int equality; /*integer to store results of strcmp*/ if (*root) { /*if root exists*/ if((equality = strcmp((*root)->key, target)) == 0) { /*if the root and target are same*/ if ((*root)->child[RIGHT]) { free((*root)->key); /*free key currently there, so we can set key to min of right child*/ (*root)->key = treeDeleteMin(&(*root)->child[RIGHT]); } else { /*if no right child --> just make left child the new root*/ toFree = *root; /*set pointer to free node*/ *root = toFree->child[LEFT]; /*set new root to left child*/ free(toFree->key); /*free string in node we're deleting*/ free(toFree); /*free old root node*/ } } else if (equality > 0) { /*root key > target --> look left*/ treeDelete(&(*root)->child[LEFT], target); } else { /*root key < target --> look right*/ treeDelete(&(*root)->child[RIGHT], target); } /*fix height and size data in tree, then rebalance it after the deletion*/ treeFix(*root); treeRebalance(root); } }
/* * Deletes the entire tree with root x. */ void treeDelete(Node *x) { if (x != NULL) { treeDelete(x->getLeft()); treeDelete(x->getRight()); delete x; } }
void buildTree(void){ treeADT tree=newTree(); string userOP; int userInt,element; printf("\n**** Tree builder ****\n\n"); while(TRUE){ printf("(i)nsert, (d)elete, (f)ind, (p)rint, (q)uit\n"); printf("Your choice: "); userOP=GetLine(); if(StringEqual(userOP,"i")){ printf("To end insertion enter: -1.\n"); while(TRUE){ printf("TreeInsert: "); userInt=GetInteger(); if(userInt==-1) break; treeInsert(tree,userInt); } } if(StringEqual(userOP,"d")){ printf("To end deletion enter: -1.\n"); while(TRUE){ printf("TreeDelete: "); userInt=GetInteger(); if(userInt==-1) break; treeDelete(tree,userInt); } } if(StringEqual(userOP,"f")){ printf("FindNode: "); userInt=GetInteger(); element=findNode(tree,userInt); if(element!=NOT_FOUND) printf("\nFound %d in your tree! :-)\n",userInt); else printf("\nDid not find %d in your tree. :-(\n",userInt); } if(StringEqual(userOP,"p")){ printf("\nThis is your current tree:\n\n"); displayTreeStructure(tree); printf("Preorder: "); printTree(tree,preOrder); printf("Inorder: "); printTree(tree,inOrder); printf("Postorder: "); printTree(tree,postOrder); printf("\n"); printf("Height: %d\n",treeHeight(tree)); printf("Black-height: %d\n",blackHeight(tree)); } if(StringEqual(userOP,"q")) break; printf("\n"); } freeTree(tree); }
ModifiedTree::~ModifiedTree() { treeDelete(root); }
int main() { const int arr[] = {30, 10, 45, 38, 20, 50, 25, 33, 8, 12}; const int SIZE = 10; struct node *newNode = NULL, *root = NULL, *deleteNode; int i, choice, search; // Insert array elements into tree for (i = 0; i < SIZE; ++i) { newNode = (struct node *)malloc(sizeof(struct node)); newNode->key = arr[i]; newNode->left = NULL; newNode->right = NULL; newNode->p = NULL; treeInsert(&root, newNode); } printf("Initial binary search tree was created!\n"); printf("Elements from array: "); printf("<30, 10, 45, 38, 20, 50, 25, 33, 8, 12>\n"); printf("were inserted in that order.\n\n"); // Menu-Driven do { printf("MENU SELECTION\n"); printf("\t0)\tEXIT\n"); printf("\t1)\tDisplay inorder\n"); printf("\t2)\tDisplay postorder\n"); printf("\t3)\tDisplay preorder\n"); printf("\t4)\tTree search\n"); printf("\t5)\tTree height\n"); printf("\t6)\tTree delete\n"); printf("\n\t==> "); scanf("%d", &choice); switch(choice) { case 0 : printf("\nHave a nice day!\n"); break; case 1 : printf("\n\nInorder: "); inorder(root); printf("\n\n"); break; case 2 : printf("\n\nPostorder: "); postorder(root); printf("\n\n"); break; case 3 : printf("\n\nPreorder: "); preorder(root); printf("\n\n"); break; case 4 : printf("\n\nSpecify search value\n\t==> "); scanf("%d", &search); treeSearch(root, search, 1); printf("\n\n"); break; case 5 : printf("\n\nTree height %d\n\n", height(root)); break; case 6 : printf("\n\nSpecify key value of node to delete\n\t==> "); scanf("%d", &search); deleteNode = treeSearch(root, search, 0); if (deleteNode != NULL) { treeDelete(&root, deleteNode); printf("Complete!\n\n"); } else printf("\nNode with that key value not present!\n\n"); free(deleteNode); break; default : printf("\n\nInvalid Choice\n\n"); } } while(choice != 0); return 0; }
int main() { Arvore *raiz = NULL; Arvore *buscasNaArvore = NULL; Arvore *trazMenorMaior = NULL; int chave; int chaveBuscada; //int valor; int opcao; do{ printf("Arvores de busca binaria sem balanceamento\n"); printf("1 - Inserir\n"); printf("2 - Mostrar\n"); printf("3 - Pesquisar\n"); printf("4 - Relembrar quem esta na raiz\n"); printf("5 - Mostrar menor valor na arvore\n"); printf("6 - Mostrar maior valor na arvore\n"); printf("7 - Remover valor da arvore\n"); printf("0 - Sair\n"); scanf("%d",&opcao); switch(opcao){ case 1: printf("\ninforme a chave e tecle enter\n"); scanf("%d",&chave); raiz = treeInsert(raiz,chave); break; case 2: printf("Valores alocados nessa arvore = \n"); mostraArvoreEmOrdem(raiz); printf("\n"); break; case 3: printf("\ndigite uma chave a ser buscada nessa arvore e tecle enter\n"); scanf("%d",&chaveBuscada); buscasNaArvore = treeSearch(raiz,chaveBuscada); if(buscasNaArvore!=NULL){ printf("encontrei esse cara\n"); }else{ printf("valor nao presente na arvore\n"); } break; case 4: mostraRaizDaArvore(raiz); break; case 5: if(raiz!=NULL){ trazMenorMaior = treeMinimum(raiz); printf("menor valor nesta arvore = %d\n",trazMenorMaior->chave); } break; case 6: if(raiz!=NULL){ trazMenorMaior = treeMaximum(raiz); printf("maior valor nesta arvore = %d\n",trazMenorMaior->chave); } break; case 7: if(raiz!=NULL){ printf("Informe o valor para deletar e tecle enter\n"); scanf("%d",&chaveBuscada); raiz = treeDelete(raiz,chaveBuscada); } break; } }while(opcao!=0); return 0; }
struct arvore *treeDelete(Arvore *raiz,int chavePraDeletar){ if(raiz==NULL){ /// arvore vazia beleza retorna NULL return NULL; }else{ /// essa parte serve para encontrar na árvore onde está a chave que quero if(raiz->chave > chavePraDeletar){ raiz->esq = treeDelete(raiz->esq,chavePraDeletar); }else{ if(raiz->chave < chavePraDeletar){ raiz->dir = treeDelete(raiz->dir,chavePraDeletar); }else{/// aqui achou a chave que deve ser deletada /// nó sem filhos( nós folhas ) if(raiz->esq == NULL && raiz->dir==NULL){ free(raiz); raiz = NULL; }else{ /// só tem um filho a direita if(raiz->esq==NULL){ Arvore *t = raiz; raiz = raiz->dir; free(t); }else{ /// só tem um filho à esquerda if(raiz->dir==NULL){ Arvore *t = raiz; raiz = raiz->esq; free(t); }else{ /// aqui são para blocos que possuem 2 filhos Arvore *f = raiz->esq; while(f->dir != NULL){ f = f->dir; } raiz->chave = f->chave; /// troca as informações aqui f->chave = chavePraDeletar; raiz->esq = treeDelete(raiz->esq,chavePraDeletar); } } } } } } return raiz; }
/*this function takes a pointer to an ordered set and string and deletes it from the set if it exists in the set, otherwise it does nothing*/ void orderedSetDelete(struct orderedSet *s, const char *elem) { treeDelete(&(s->set), elem); /*deletes element from tree that orderedset struct has a pointer to*/ }