void remove_stack_node(node_t *n) { for (stacking_list_t *s = stack_head; s != NULL; s = s->next) if (s->node == n) { remove_stack(s); return; } }
int insert(m_tree_t *tree, key_t new_key, intervalListNode *newInterval) { m_tree_t *tmp_node; stack_t *s = create_stack(); stack_t *rotate_s = create_stack(); if( tree->left == NULL ) { intervalListNode *interval =(intervalListNode*)malloc(sizeof(intervalListNode)); if(interval==NULL) printf("insert malloc failed!\n"); interval->leftPoint = newInterval->leftPoint; interval->rightPoint = newInterval->rightPoint; interval->next = NULL; tree->left = (m_tree_t *) interval; tree->key = new_key; tree->right = NULL; tree->leftMin = newInterval->leftPoint; tree->rightMax = newInterval->rightPoint; tree->l = -2147483648; // to represent -infinity tree->r = 2147483647; // to represent infinity updateLeafMeasure(tree); } else { tmp_node = tree; while( tmp_node->right != NULL ) { push(tmp_node, s); push(tmp_node, rotate_s); if( new_key < tmp_node->key ) tmp_node = tmp_node->left; else tmp_node = tmp_node->right; } /* found the candidate leaf. Test whether key distinct */ if( tmp_node->key == new_key ) { // if the key exists, then add the current interval to the interval list. addAssociatedInterval(tmp_node, newInterval); tmp_node->leftMin = min(tmp_node->leftMin, newInterval->leftPoint); tmp_node->rightMax = max(tmp_node->rightMax, newInterval->rightPoint); updateLeafMeasure(tmp_node); } /* key is distinct, now perform the insert */ else { m_tree_t *old_leaf, *new_leaf; old_leaf = get_node(); old_leaf->left = tmp_node->left; old_leaf->key = tmp_node->key; old_leaf->right = NULL; old_leaf->height = 0; old_leaf->leftMin = tmp_node->leftMin; old_leaf->rightMax = tmp_node->rightMax; new_leaf = get_node(); addAssociatedInterval(new_leaf, newInterval); new_leaf->key = new_key; new_leaf->right = NULL; new_leaf->height = 0; new_leaf->leftMin = newInterval->leftPoint; new_leaf->rightMax = newInterval->rightPoint; if( tmp_node->key < new_key ) { tmp_node->left = old_leaf; tmp_node->right = new_leaf; tmp_node->key = new_key; old_leaf->l = tmp_node->l; old_leaf->r = new_key; new_leaf->l = new_key; new_leaf->r = tmp_node->r; } else { tmp_node->left = new_leaf; tmp_node->right = old_leaf; new_leaf->l = tmp_node->l; new_leaf->r = tmp_node->key; old_leaf->l = tmp_node->key; old_leaf->r = tmp_node->r; } updateLeafMeasure(old_leaf); updateLeafMeasure(new_leaf); tmp_node->height = 1; tmp_node->leftMin = min(tmp_node->left->leftMin,tmp_node->right->leftMin); tmp_node->rightMax = max(tmp_node->left->rightMax,tmp_node->right->rightMax); //push(tmp_node, s); updateInternalMeasure(tmp_node); tmp_node->leftMin = min(tmp_node->left->leftMin,tmp_node->right->leftMin); tmp_node->rightMax = max(tmp_node->left->rightMax,tmp_node->right->rightMax); } // update measures while(!stack_empty(s)) { tmp_node = top(s); pop(s); updateInternalMeasure(tmp_node); tmp_node->leftMin = min(tmp_node->left->leftMin,tmp_node->right->leftMin); tmp_node->rightMax = max(tmp_node->left->rightMax,tmp_node->right->rightMax); } // rebalance int finished = 0; while(!stack_empty(rotate_s) && !finished) { tmp_node = top(rotate_s); pop(rotate_s); int tmp_height, old_height; old_height= tmp_node->height; if( tmp_node->left->height - tmp_node->right->height == 2 ) { if( tmp_node->left->left->height - tmp_node->right->height == 1 ) { right_rotation( tmp_node ); tmp_node->right->height = tmp_node->right->left->height + 1; tmp_node->height = tmp_node->right->height + 1; } else { left_rotation( tmp_node->left ); right_rotation( tmp_node ); tmp_height = tmp_node->left->left->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else if( tmp_node->left->height - tmp_node->right->height == -2 ) { if( tmp_node->right->right->height - tmp_node->left->height == 1 ) { left_rotation( tmp_node ); tmp_node->left->height = tmp_node->left->right->height + 1; tmp_node->height = tmp_node->left->height + 1; } else { right_rotation( tmp_node->right ); left_rotation( tmp_node ); tmp_height = tmp_node->right->right->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else /* update height even if there was no rotation */ { if( tmp_node->left->height > tmp_node->right->height ) tmp_node->height = tmp_node->left->height + 1; else tmp_node->height = tmp_node->right->height + 1; } if( tmp_node->height == old_height ) finished = 1; } } remove_stack(s); remove_stack(rotate_s); return 0; }
/* Deletes the line of number index, renumbering all lines after that line, and returns a pointer to the deleted line */ char * delete_line(text_t *txt, int index){ text_t * init_text=txt; if(txt==NULL){ //No Tree return NULL; }else{ int length=length_text(txt); //Check index with maxlength if(index>length){ return NULL; }else{ //Initialize stack createstack(); text_t * uppernode; text_t * sel_node; text_t * other_node; while(txt->right!=NULL){ push(txt); uppernode=txt; if(index<=txt->key){ sel_node=txt->left; other_node=txt->right; txt->key=txt->key-1; txt=txt->left; }else{ index=index-txt->key; sel_node=txt->right; other_node=txt->left; txt=txt->right; } } //Pop the last node pop(); uppernode->left=other_node->left; uppernode->right=other_node->right; uppernode->key=other_node->key; uppernode->height=other_node->height; text_t * tmp_node=NULL; int finished=0; while( !stack_empty() && !finished ){ int tmp_height, old_height; tmp_node = pop(); old_height= tmp_node->height; if( tmp_node->left->height - tmp_node->right->height == 2 ) { if( tmp_node->left->left->height - tmp_node->right->height == 1 ) { right_rotation( tmp_node ); tmp_node->right->height = tmp_node->right->left->height + 1; tmp_node->height = tmp_node->right->height + 1; } else { left_rotation( tmp_node->left ); right_rotation( tmp_node ); tmp_height = tmp_node->left->left->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } }else if( tmp_node->left->height - tmp_node->right->height == -2 ) { if( tmp_node->right->right->height - tmp_node->left->height == 1 ) { left_rotation( tmp_node ); tmp_node->left->height =tmp_node->left->right->height + 1; tmp_node->height = tmp_node->left->height + 1; } else { right_rotation( tmp_node->right ); left_rotation( tmp_node ); tmp_height = tmp_node->right->right->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else /* update height even if there was no rotation */ { if( tmp_node->left->height > tmp_node->right->height ) tmp_node->height = tmp_node->left->height + 1; else tmp_node->height = tmp_node->right->height + 1; } if( tmp_node->height == old_height ) finished = 1; } remove_stack(); return (char *)sel_node->left; /* if(index-txt->key==1){ }else{ //Node not found printf("shouldn't come here"); }*/ } } }
/* Inserts the line before the line of number index, if such a line exists, to new line, renumbering all lines after that line. If no such line exists, it appends new line as new last line */ void insert_line(text_t *txt, int index, char * new_line) { if(txt==NULL){ //printf("Should not come here"); return NULL; }else if( txt->left == NULL ) { if(index>length_text(txt)+1){ index=length_text(txt)+1; } txt->left = (struct text_t *) new_line; txt->key = 0; txt->height = 0; txt->right = NULL; }else{ //check max line if(index>length_text(txt)+1){ index=length_text(txt)+1; } createstack(); while(txt->right!=NULL){ push(txt); if(index<=txt->key){ txt->key=txt->key+1; txt=txt->left; }else if(index>txt->key){ index=index-txt->key; txt=txt->right; } } //create a new Node text_t * old_node=malloc(sizeof(text_t)); old_node->key=0; old_node->left=txt->left; old_node->right=txt->right; old_node->height=0; text_t * new_node=malloc(sizeof(text_t)); new_node->key=0; new_node->left=(struct text_t *)new_line; new_node->right=NULL; new_node->height=0; if(index-txt->key==0){ txt->left=new_node; txt->right=old_node; txt->height=height(txt); }else if(index-txt->key==1){ txt->left=new_node; txt->right=old_node; txt->height=height(txt); }else{ txt->left=old_node; txt->right=new_node; txt->height=height(txt); } txt->key=1; //balance the tree //Need stack text_t * tmp_node=NULL; int finished =0; while( !stack_empty() && !finished ){ int tmp_height, old_height; tmp_node = pop(); old_height= tmp_node->height; if( tmp_node->left->height - tmp_node->right->height == 2 ) { if( tmp_node->left->left->height - tmp_node->right->height == 1 ) { right_rotation( tmp_node ); tmp_node->right->height = tmp_node->right->left->height + 1; tmp_node->height = tmp_node->right->height + 1; } else { left_rotation( tmp_node->left ); right_rotation( tmp_node ); tmp_height = tmp_node->left->left->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } }else if( tmp_node->left->height - tmp_node->right->height == -2 ) { if( tmp_node->right->right->height - tmp_node->left->height == 1 ) { left_rotation( tmp_node ); tmp_node->left->height =tmp_node->left->right->height + 1; tmp_node->height = tmp_node->left->height + 1; } else { right_rotation( tmp_node->right ); left_rotation( tmp_node ); tmp_height = tmp_node->right->right->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else /* update height even if there was no rotation */ { if( tmp_node->left->height > tmp_node->right->height ) tmp_node->height = tmp_node->left->height + 1; else tmp_node->height = tmp_node->right->height + 1; } if( tmp_node->height == old_height ) finished = 1; } remove_stack(); } }
int insert_balanced(tree_node_t *tree, key_t new_key,object_t *new_object) { tree_node_t *tmp_node; //int finished; stack_t *stack; int inserted_key = 32767; tree_node_t *last_node; if( tree->left == NULL ) { tree->left = (tree_node_t *) new_object; tree->key = new_key; tree->height = 0; tree->left_leaves = tree->right_leaves = 0; tree->right = NULL; } else { stack = create_stack(); tmp_node = tree; last_node = tmp_node; while( tmp_node->right != NULL ) { push(tmp_node,stack); last_node = tmp_node; if( new_key <= tmp_node->left->key ) tmp_node = tmp_node->left; else { new_key = new_key - tmp_node->left->key; tmp_node = tmp_node->right; } } /* found the candidate leaf. Test whether key distinct */ if( tmp_node->key == new_key ) { tree_node_t *old_leaf, *new_leaf; old_leaf = get_node(); old_leaf->left = tmp_node->left; old_leaf->key = tmp_node->key; old_leaf->right= NULL; old_leaf->height = 0; new_leaf = get_node(); new_leaf->left = (tree_node_t *) new_object; new_leaf->key = 1; new_leaf->right = NULL; new_leaf->height = 0; tmp_node->left = new_leaf; tmp_node->right = old_leaf; tmp_node->height = 1; tmp_node->key = new_leaf->key + old_leaf->key; } else { tree_node_t *old_leaf, *new_leaf; old_leaf = get_node(); old_leaf->left = tmp_node->left; old_leaf->key = tmp_node->key; old_leaf->right= NULL; old_leaf->height = 0; new_leaf = get_node(); new_leaf->left = (tree_node_t *) new_object; new_leaf->key = 1; new_leaf->right = NULL; new_leaf->height = 0; tmp_node->left = old_leaf; tmp_node->right = new_leaf; tmp_node->key = new_leaf->key + old_leaf->key; tmp_node->height = 1; } /* rebalance */ while( !stack_empty(stack))// && !finished ) { int tmp_height, old_height; tmp_node = pop(stack); tmp_node->key = tmp_node->left->key + tmp_node->right->key; old_height= tmp_node->height; if( tmp_node->left->height - tmp_node->right->height == 2 ) { if( tmp_node->left->left->height - tmp_node->right->height == 1 ) { right_rotation( tmp_node ); tmp_node->right->height = tmp_node->right->left->height + 1; tmp_node->height = tmp_node->right->height + 1; } else { left_rotation(tmp_node->left); right_rotation( tmp_node ); tmp_height = tmp_node->left->left->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else if( tmp_node->left->height - tmp_node->right->height == -2 ) { if( tmp_node->right->right->height - tmp_node->left->height == 1 ) { left_rotation( tmp_node ); tmp_node->left->height = tmp_node->left->right->height + 1; tmp_node->height = tmp_node->left->height + 1; } else { right_rotation( tmp_node->right ); left_rotation( tmp_node ); tmp_height = tmp_node->right->right->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else /* update height even if there was no rotation */ { if( tmp_node->left->height > tmp_node->right->height ) { tmp_node->height = tmp_node->left->height + 1; } else { tmp_node->height = tmp_node->right->height + 1; } } } remove_stack(stack); } return( 0 ); }
object_t *_delete_balanced(tree_node_t *tree, key_t delete_key) { tree_node_t *tmp_node, *upper_node, *other_node; //int finished; stack_t *stack; object_t *deleted_object; if( tree->left == NULL ) return( NULL ); else if( tree->right == NULL ) { if( tree->key == delete_key ) { deleted_object = (object_t *) tree->left; tree->left = NULL; return( deleted_object ); } else return( NULL ); } else { stack = create_stack(); tmp_node = tree; while( tmp_node->right != NULL ) { push(tmp_node,stack); upper_node = tmp_node; if( delete_key <= tmp_node->left->key ) { tmp_node = upper_node->left; other_node = upper_node->right; } else { delete_key = delete_key - tmp_node->left->key; tmp_node = upper_node->right; other_node = upper_node->left; } } if( tmp_node->key != delete_key ) return( NULL ); else { upper_node->key = other_node->key; upper_node->left = other_node->left; upper_node->right = other_node->right; upper_node->height = 0; deleted_object = (object_t *) tmp_node->left; return_node( tmp_node ); return_node( other_node ); update_leafcount(upper_node); } /* rebalance */ //finished = 0; /*if (!stack_empty(stack)) { print_stack(stack); }*/ //Throw out the top of stack. pop(stack); while( !stack_empty(stack))// && !finished ) { int tmp_height, old_height; tmp_node = pop(stack); tmp_node->key = tmp_node->left->key + tmp_node->right->key; old_height= tmp_node->height; if( tmp_node->left->height - tmp_node->right->height == 2 ) { if( tmp_node->left->left->height - tmp_node->right->height == 1 ) { right_rotation(tmp_node); tmp_node->right->height = tmp_node->right->left->height + 1; tmp_node->height = tmp_node->right->height + 1; } else { left_rotation(tmp_node->left); right_rotation(tmp_node); tmp_height = tmp_node->left->left->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else if( tmp_node->left->height - tmp_node->right->height == -2 ) { if( tmp_node->right->right->height - tmp_node->left->height == 1 ) { left_rotation( tmp_node ); tmp_node->left->height = tmp_node->left->right->height + 1; tmp_node->height = tmp_node->left->height + 1; } else { right_rotation( tmp_node->right ); left_rotation( tmp_node ); tmp_height = tmp_node->right->right->height; tmp_node->left->height = tmp_height + 1; tmp_node->right->height = tmp_height + 1; tmp_node->height = tmp_height + 2; } } else /* update height even if there was no rotation */ { if( tmp_node->left->height > tmp_node->right->height ) tmp_node->height = tmp_node->left->height + 1; else tmp_node->height = tmp_node->right->height + 1; } update_leafcount(tmp_node); /*if( tmp_node->height == old_height ) finished = 1;*/ } remove_stack(stack); } return( deleted_object ); }