DeleteNodeFromTree

VOID __stdcall DeleteNodeFromTree(RTL_AVL_TABLE *Table, RTL_BALANCED_LINKS *NodeToDelete){
  _RTL_BALANCED_LINKS *LeftChild; 
  _RTL_BALANCED_LINKS *RightChild; 
  _RTL_BALANCED_LINKS *Parent; 
  char v7; 
  _RTL_BALANCED_LINKS *v8; 
  _RTL_BALANCED_LINKS *v9; 
  _RTL_BALANCED_LINKS *v10; 
  _RTL_BALANCED_LINKS *v11; 
  RTL_BALANCED_LINKS *v12; 
  char Balance; 
  bool v14; 
  _RTL_BALANCED_LINKS *j; 
  _RTL_BALANCED_LINKS *v16; 
  _RTL_BALANCED_LINKS *v17; 
  _RTL_BALANCED_LINKS *v18; 
  _RTL_BALANCED_LINKS *i; 
  LeftChild = NodeToDelete->LeftChild;
  if( LeftChild && (RightChild = NodeToDelete->RightChild) != 0i64 )
  {
    if( NodeToDelete->Balance < 0 )
    {
      RightChild = NodeToDelete->LeftChild;
      for( i = LeftChild->RightChild; i; i = i->RightChild )
        RightChild = i;
    }
    else
    {
      for( j = RightChild->LeftChild; j; j = j->LeftChild )
        RightChild = j;
    }
  }
  else
  {
    RightChild = NodeToDelete;
  }
  Parent = RightChild->Parent;
  v7 = -1;
  v8 = RightChild->LeftChild;
  v9 = RightChild->Parent->LeftChild;
  if( v8 )
  {
    if( v9 == RightChild )
    {
      Parent->LeftChild = v8;
    }
    else
    {
      Parent->RightChild = v8;
      v7 = 1;
    }
    v11 = RightChild->LeftChild;
  }
  else
  {
    v10 = RightChild->RightChild;
    if( v9 == RightChild )
    {
      Parent->LeftChild = v10;
    }
    else
    {
      Parent->RightChild = v10;
      v7 = 1;
    }
    v11 = RightChild->RightChild;
    if( !v11 )
      goto LABEL_7;
  }
  v11->Parent = RightChild->Parent;
LABEL_7:
  Table->BalancedRoot.Balance = 0;
  v12 = RightChild->Parent;
  while( 1 )
  {
    Balance = v12->Balance;
    if( Balance == v7 )
    {
      v12->Balance = 0;
      goto LABEL_15;
    }
    if( !Balance )
      break;
    if( (unsigned int)RebalanceNode(v12) )
      goto LABEL_12;
    v12 = v12->Parent;
LABEL_15:
    v7 = 1;
    v14 = v12->Parent->RightChild == v12;
    v12 = v12->Parent;
    if( !v14 )
      v7 = -1;
  }
  v12->Balance = -v7;
  if( Table->BalancedRoot.Balance )
    --Table->DepthOfTree;
LABEL_12:
  if( NodeToDelete != RightChild )
  {
    *(_OWORD *)&RightChild->Parent = *(_OWORD *)&NodeToDelete->Parent;
    *(_OWORD *)&RightChild->RightChild = *(_OWORD *)&NodeToDelete->RightChild;
    v16 = RightChild->Parent;
    if( NodeToDelete->Parent->LeftChild == NodeToDelete )
      v16->LeftChild = RightChild;
    else
      v16->RightChild = RightChild;
    v17 = RightChild->LeftChild;
    if( v17 )
      v17->Parent = RightChild;
    v18 = RightChild->RightChild;
    if( v18 )
      v18->Parent = RightChild;
  }
}

Referenced by:

RtlDeleteElementGenericTableAvl
RtlDeleteElementGenericTableAvlEx