btree* InsertRoot(btree* root, void* payLoad) { if(root == NULL) { root = (btree *)malloc(sizeof(btree)); if(root == NULL) { puts("Failed to alloc memory for leaf.\n"); return NULL; } root->payload = payLoad; root->lbranch = NULL; root->rbranch = NULL; return root; } if(root->payload > payLoad) root->lbranch = InsertRoot(root->lbranch, payLoad); else if(root->payload < payLoad) root->rbranch = InsertRoot(root->rbranch, payLoad); return root; }
int InsertRoot( pNode * proot, Item D ) { #define root (*proot) if( !root ) { root = NewNode( D ); return 1; } else if( D < root->Data ) { if( InsertRoot( &(root->Left), D ) ) { RotateRight( &(root) ); return 1; } return 0; } else if( D > root->Data ) { if( InsertRoot( &(root->Right), D ) ) { RotateLeft( &(root) ); return 1; } return 0; } return 0; #undef root }
void FindAndReplace(btree* root, const int find, void* newPayLoad) { btree* found = NULL; if(newPayLoad == NULL) return; found = SearchBinaryTree(GetRoot(), find); if(found != NULL) { printf("Found [%s] to replace with [%c]\n", (char *)found->payload, _getLetter(newPayLoad)); SetRoot(DeleteBinaryTreeNode(GetRoot(), _getLetter(found->payload))); SetRoot(InsertRoot(GetRoot(), newPayLoad)); } }
void InsertRand( pNode * proot, Item D ) { #define root (*proot) if( root == NULL ) { root = NewNode( D ); } else if ( D == root->Data ) { return; } else if( rand() < RAND_MAX / root->Size ) { InsertRoot( proot, D ); } else if( D < root->Data ) { InsertRand( &(root->Left), D ); fix_size( root ); } else { InsertRand( &(root->Right), D ); fix_size( root ); } #undef root }