int main(int argc,char* argv[]) { int i=0; BTree* tree=BTree_Create(); BTreeNode n1={'A',NULL,NULL}; BTreeNode n2={'B',NULL,NULL}; BTreeNode n3={'C',NULL,NULL}; BTreeNode n4={'D',NULL,NULL}; BTreeNode n5={'E',NULL,NULL}; BTreeNode n6={'F',NULL,NULL}; BTree_Insert(tree,&n1,0x0,0,0); BTree_Insert(tree,&n2,0x0,1,0); BTree_Insert(tree,&n3,0x1,1,0); BTree_Insert(tree,&n4,0x00,2,0); BTree_Insert(tree,&n5,0x2,2,0); BTree_Insert(tree,&n6,0x2,3,0); printf("The Height of Tree: %d \n",BTree_Height(tree)); BTree_Display(tree,printf_data,4,'-'); //BTreeNode* node=((BTreeHeader*)tree)->root; printf("\nPreOrder_traversal:\n"); preOrder_traversal(((BTreeHeader*)tree)->root); printf("\nMidOrder_traversal:\n"); midOrder_traversal(((BTreeHeader*)tree)->root); printf("\nPostOrder_traversal:\n"); postOrder_traversal(((BTreeHeader*)tree)->root); return 0; }
int main(int argc, char *argv[]) { BTree* tree = BTree_Create(); BTreeNode* current = NULL; BTreeNode* p = NULL; SeqList* list = NULL; int i = 0; struct Node n1 = {{NULL, NULL}, 'A'}; struct Node n2 = {{NULL, NULL}, 'B'}; struct Node n3 = {{NULL, NULL}, 'C'}; struct Node n4 = {{NULL, NULL}, 'D'}; struct Node n5 = {{NULL, NULL}, 'E'}; struct Node n6 = {{NULL, NULL}, 'F'}; BTree_Insert(tree, (BTreeNode*)&n1, 0, 0, 0); BTree_Insert(tree, (BTreeNode*)&n2, 0x00, 1, 0); BTree_Insert(tree, (BTreeNode*)&n3, 0x01, 1, 0); BTree_Insert(tree, (BTreeNode*)&n4, 0x00, 2, 0); BTree_Insert(tree, (BTreeNode*)&n5, 0x02, 2, 0); BTree_Insert(tree, (BTreeNode*)&n6, 0x02, 3, 0); printf("Full Tree: \n"); BTree_Display(tree, printf_data, 4, '-'); printf("Thread via List:\n"); list = SeqList_Create(BTree_Count(tree)); thread_via_list(BTree_Root(tree), list); for(i=0; i<SeqList_Length(list); i++) { printf("%c, ", ((struct Node*)SeqList_Get(list, i))->v); } printf("\n"); printf("Thread via Left:\n"); current = BTree_Root(tree); thread_via_left(current, &p); while( current != NULL ) { printf("%c, ", ((struct Node*)current)->v); current = current->left; } printf("\n"); BTree_Destroy(tree); return 0; }
void test_algorithm(void) { int padding = 1; list_t *imageList = NULL; btree_t *tree; list_t *treeDumpList = NULL; list_t *fileListIter = NULL; int i = 0; char filename[10]; int success = 1; int initsize = 512; srand((unsigned int)time(NULL)); for (i = 0; i < 2800; i++) { list_t *node = NULL; image_t *img = NULL; img = (image_t *)calloc(1, sizeof(image_t)); node = List_Create(); sprintf(filename, "%d.png", i); img->w = 5 + rand() % 20; img->h = 5 + rand() % 20; Util_CopyString(&img->filename, filename); node->payload.type = Payload_Image; node->payload.data = img; List_PushFront(&imageList, node); } List_Sort(&imageList); tree = BTree_Create(); tree->rect.x = 0; tree->rect.y = 0; tree->rect.w = initsize; tree->rect.h = initsize; do { success = 1; for (fileListIter = imageList; fileListIter != NULL; fileListIter = fileListIter->next) { if (BTree_Insert(tree, (image_t *)fileListIter->payload.data, padding) == NULL) { printf("Shit happens\n"); success = 0; break; } } if (!success) { BTree_Destroy(tree); tree = BTree_Create(); tree->rect.x = 0; tree->rect.y = 0; initsize = Util_NextPOT(initsize+1); tree->rect.w = initsize; tree->rect.h = initsize; } } while (!success); printf("-----\n"); List_Destroy(imageList, ListDestroy_List|ListDestroy_PayLoad); treeDumpList = BTree_DumpToList(tree, BTreeTraverse_PostOrder); { list_t *iter = NULL; bitmap_t *canvas = NULL; rect_t *rect = NULL; canvas = Bitmap_Create(initsize, initsize); iter = treeDumpList; for (iter = treeDumpList; iter != NULL; iter = iter->next) { rect = (rect_t *)iter->payload.data; if (rect->image != NULL && rect->image->filename != NULL) { int argb = 0; bitmap_t *pad = NULL; bitmap_t *png = NULL; png = Bitmap_Create(rect->image->w, rect->image->h); pad = Bitmap_Create(rect->image->w + rect->padding * 2, rect->image->h + rect->padding * 2); argb = rand(); argb |= 0xFF000000; Bitmap_Clean(png, argb); Bitmap_Clean(pad, 0); Bitmap_CopyPasteBitmap(pad, png, rect->padding, rect->padding); Bitmap_CopyPasteBitmap(canvas, pad, rect->x, rect->y); Bitmap_Destroy(png); Bitmap_Destroy(pad); } } Bitmap_WriteAsPNG(canvas, "canvas.png"); Bitmap_Destroy(canvas); } List_Destroy(treeDumpList, ListDestroy_List); if (tree != NULL) BTree_Destroy(tree); memtrack_list_allocations(); }
void packpng(void) { list_t *pngFileList = NULL; list_t *fileListIter = NULL; btree_t *tree = NULL; list_t *treeDumpList = NULL; pngFileList = Util_ReadAllPNGs(); tree = BTree_Create(); tree->rect.x = 0; tree->rect.y = 0; tree->rect.w = 512; tree->rect.h = 512; for (fileListIter = pngFileList; fileListIter != NULL; fileListIter = fileListIter->next) { if (BTree_Insert(tree, (image_t *)fileListIter->payload.data, 0) == NULL) { printf("Enlarge needed\n"); } } treeDumpList = BTree_DumpToList(tree, BTreeTraverse_PreOrder); printf("-----\n"); List_Print(treeDumpList); { list_t *iter = NULL; bitmap_t *canvas = NULL; rect_t *rect = NULL; canvas = Bitmap_Create(512, 512); iter = treeDumpList; for (iter = treeDumpList; iter != NULL; iter = iter->next) { rect = (rect_t *)iter->payload.data; if (rect->image != NULL && rect->image->filename != NULL) { bitmap_t *png = Bitmap_CreateFromPNG(rect->image->filename); printf(" %d - %d %s\n", rect->x, rect->y, rect->image->filename); Bitmap_CopyPasteBitmap(canvas, png, rect->x, rect->y); Bitmap_Destroy(png); } } Bitmap_WriteAsPNG(canvas, "canvas.png"); Bitmap_Destroy(canvas); } { Util_CompileRects(treeDumpList, FileFormat_Lua, "digits"); } printf("-----\n"); List_Destroy(treeDumpList, ListDestroy_List); if (pngFileList != NULL) List_Destroy(pngFileList, ListDestroy_List|ListDestroy_PayLoad); if (tree != NULL) BTree_Destroy(tree); memtrack_list_allocations(); }