All pastes #549666 Raw Edit

Unnamed

public c v1 · immutable
·published 2007-06-08 05:59 UTC
rendered paste body
void delete(treeNode** pRoot, int value){  treeNode* parent = (treeNode*)malloc(sizeof(treeNode));  //Save the parent of the node as "parent".  if((*pRoot)->left->data == value || (*pRoot)->right->data == value)    parent = (*pRoot);     //Moves to the parent of the value being deleted. RECURSES DOWN.  //If value is smaller than the info, then it moves to the left child.  if(value < (*pRoot)->info && (*pRoot)->left->info != value)    delete(&((*pRoot)->left), value);  //Otherwise, keep recursing to the right.  if(value > (*pRoot)->info && (*pRoot)->right->info != value)    delete(&((*pRoot)->right), value);  int direction;  //Moves to the delete node.  if((*pRoot)->left->info == value){//If value is on the left side, go there.    parent = parent->left;    direction = 1;//Parent went to left direction.  }  else{//If value is on the right side, go there.    parent = parent->right;    direction = 2;//Parent went to right direction.  }  //If the node to be deleted has NO CHILDREN, delete it and end function.  if(parent->left == NULL && parent->right == NULL){    parent = NULL;    free(parent);    if(direction == 1)//If parent moved left, then set left NULL.      (*pRoot)->left = NULL;    else      (*pRoot)->right = NULL;    return;  }  //If node has ONE CHILD, make pRoot point to parent's only child.  if(parent->left == NULL || parent->right == NULL){    //If left of delete node is empty, make pRoot point to right.    if(parent->left == NULL){     //If pRoot went left, then the pointer left should change.      if(direction == 1)        (*pRoot)->left = parent->right;    //If direction is right, then pointer right should change.      else        (*pRoot)->right = parent->right;      parent = NULL;      return;    }    if(parent->right == NULL){      if(direction == 1)        (*pRoot)->left = parent->left;      else        (*pRoot)->right = parent->left;      parent = NULL;      return;    }  }