NodeT *createBinTree( int branch, NodeT *parent ) { NodeT *p; int id; /* read node id */ if ( branch == 0 ) printf( "Root identifier [0 to end] =" ); else if ( branch == 1 ) printf( "Left child of %d [0 to end] =", parent->id ); else printf( "Right child of %d [0 to end] =", parent->id ); scanf("%d", &id); if ( id == 0 ) return NULL; else { /* build node pointed to by p */ p = ( NodeT *)malloc(sizeof( NodeT )); if ( p == NULL ) fatalError( "Out of space in createBinTree" ); /* fill in node */ p->id = id; p->left = createBinTree( 1, p ); p->right = createBinTree( 2, p ); } return p; }
NodeT* createBinTree() { NodeT *p; char *data=(char*)malloc(sizeof(char)*10); fscanf(input,"%s",data); if(strcmp(data,"*")==0) return NULL; else { p=createNODE(data); p->left=createBinTree(); p->right=createBinTree(); } return p; }
NodeT *createBinTree() { char *data=(char*) malloc(sizeof(char)*20); scanf("%s",data); if(strcmp(data,"*")==0) return NULL; else { NodeT *p=(NodeT*)malloc(sizeof (NodeT)); p->data=data; p->left=createBinTree(); p->right=createBinTree(); return p; } }
t_btree* createBinTree() { t_btree *p; char *s; s = (char*)malloc(sizeof(char) * 100); fscanf(f, "%s",s); if (strcmp (s, "*") == 0) return (NULL); else { p = createNode(atoi(s)); p->left = createBinTree(); p->right = createBinTree(); } return p; }
nodeT* createBinTree() { nodeT* p; char content[20]; scanf("%s",content); if(strcmp(content,"*")==0) return NULL; p=(nodeT*)malloc(sizeof(nodeT)); p->data=atoi(content); p->left=createBinTree(); p->right=createBinTree(); return p; }
NodeT* createeBinTree() { NodeT* p; char* data =(char*)malloc(sizeof(char)*100); scanf("%s",data); if(strcmp(data,"*")==0) { return NULL; } else { p=createNode(atoi(data)); p->left=createBinTree(); p->right=createBinTree(); } return p; }
int main() { nodeT *root=createBinTree(); nodeL *firstFromList=getListFromTree(root); traverseList(firstFromList); printf("\n"); prettyPrint(root,0); return 0; }
nodeT* createBinTree(FILE *f) { nodeT *p; char *dat=( char* ) malloc(sizeof(char)*100); fscanf(f,"%s",dat); if (strcmp(dat,"*")==0) { return NULL; } else { p=createNode(atoi(dat)); p->left=createBinTree(f); p->right=createBinTree(f); } p->height=max(p->left,p->right)+1; return p; }
void exploitMemLeaks() { // All data allocated and tracked, all will be freed int * low = calloc(1, sizeof(int)); * low = 1; int * mid = calloc(1, sizeof(int)); * mid = 2; int * hi = calloc(1, sizeof(int)); * hi = 3; printf("Created the following data to add to a tree:\n"); printf("Low: %d, Mid: %d, Hi: %d\n", * low, * mid, * hi); // Test inside a tree Tree * test = createBinTree(&compNum, &destructor); addToTree(test, mid); addToTree(test, low); addToTree(test, hi); printInOrder(test, &print); // Up to here, as long as destroyBinTree is called, no leaks are possible // Now lets look at some of the issues with the library // Left will contain Low (1), we know this. This function allocates memory // so it's return value must be freed Tree * left = getLeftSubtree(test); free(left); // All is well, no memory leaks here // Now let's get the right subtree, Hi (3), but before we free it, let's get // get one of it's subtrees too. We know it won't contain anything so // presumably we'll get some sort of error return value Tree * right = getRightSubtree(test); Tree * empty = getRightSubtree(right); if (empty == NULL) printf("The call to an empty subtree returned NULL.\n"); // Oh good, we did get some error handling, great now there's no problem // OOPS, no, there's a huge issue. The getSubtree functions are presumably // doing the following: allocating memory to a temporary new tree pointer, // checking what's in the subtree and returning NULL if nothing is in it. // So the temporary new tree pointer to allocated memory is completely lost // in all instances. // We can't even detect the error without a memory checker because the // following call of free(NULL) has no effect free(empty); free(right); destroyBinTree(test); }
int main() { int i; i=0; NodeT* root=createBinTree(); head=getListFromTree(root); root=getTreeFromList(); prettyPrint(root,i); return 0; }
BinTree* createBinTree(DLL** nodeL) { char* c = (char*)(*nodeL)->data; BinTree* New = createBinTreeNode((*nodeL)->data); *nodeL = (*nodeL)->next; //If it is a operand, it will be a leaf, otherwise it is an internal node(2 more function calls) //the strlen condition is necessary since a negative number would be seen as an operator if((strlen(c) == 1)&& ((c[0] == '+')||(c[0] == '-')||(c[0] == '*')||(c[0] == '/'))) { New->left = createBinTree(nodeL); New->right = createBinTree(nodeL); return New; } else { return New; } }
int main() { input=fopen("input.dat", "r"); output=fopen("output.dat", "w"); NODET *root=createBinTree(); NODEL *firstFromList=getListFromTree(root); traverseList(firstFromList); root=getTreeFromList(&firstFromList); prettyPrint(root,0); return 0; }
int main() { root=createBinTree(); head=getLfromT(root); printf("tree=>list: \n"); printList(); delTree(root); root=NULL; root=getTfromL(&head); printf("list=>tree: \n"); prettyPrint(root,0); return 0; }
int main(void) { t_btree *root, *rootFromList; f = fopen("input.dat", "r"); g = fopen("output.dat", "w"); root = createBinTree(); getListFromTree(root); traverseList(head); rootFromList = getTreeFromList(); prettyPrint(root, 0); fprintf (g, "\n"); return (0); }
int main() { NodeT *root; root = createBinTree( 0, NULL ); show(root,0); printf("identifier of node to interchange:"); int i=0; scanf("%d",&i); interchangeodes(find(root,i)); show(root,0); printf("height:%d\n",detheight(root)); printf("nr of leaves:%d\n",nmb_of_leaves(root)); return 0; }
binTree* addElement(binTree *root, int cur) { if (root == NULL) { return createBinTree(cur); } else if (root->value > cur) { root->left = addElement(root->left, cur); } else if (root->value < cur) { root->right = addElement(root->right, cur); } return root; }
int main() { FILE *f=fopen("input.txt","r"); if(f==NULL) { perror("Can't open file!\n"); return -1; } NodeT *root=createBinTree(f); AVLTree(root); root=insertNode(root,11); root=deleteNode(root,11); prettyPrint(root,0); return 0; }
int main(void) { TreeT *root; FILE *f; f = fopen("input.txt", "r+"); if (f == NULL) { perror("Cannot open file"); return(-1); } root = createBinTree(f); printf("The tree made from the input is : \n"); prettyPrint(root, 0); return 0; }
int main(void) { TreeT *root1; ListT *firstFromList; root1 = createBinTree(); printf("The input tree is : \n"); prettyPrint(root1, 0); firstFromList = getListFromTree(root1); printf("The list from the tree is : \n"); traverseList(firstFromList); printf("\n"); root1 = getTreeFromList(&firstFromList); printf("The tree from the list is : \n"); prettyPrint(root1, 0); return 0; }
int main() { FILE *file, *fileOutput; file= fopen("input.dat", "r"); if(file== NULL) { perror("Cannot open file"); return -100; } fileOutput = fopen("output.dat", "w"); getContent = &getStringContent; printElement = &printStringElement; NodeT *root = NULL; root = createBinTree(file); prettyPrint(root,0,fileOutput); return 0; }