anchor.diff:      {_, Pos} = binary_search(Key, KeyValues, KeyNo, 1, KeyNo, SortFunction),
       {KeyNo_1, SubtreeNo_1, KeyValues_1, Subtrees_1} = lists:nth(Pos, Subtrees),
       iterator_from_1(Key, {KeyNo_1, SubtreeNo_1, KeyValues_1, Subtrees_1}, [{lists:sublist(KeyValues, Pos, KeyNo), lists:sublist(Subtrees, Pos, SubtreeNo), nil} | Iterator], SortFunction, nil);
  -iterator_from_1(Key, {KeyNo, SubtreeNo, KeyValues, Subtrees}, Iterator, SortFunction, State) when is_list(Subtrees) ->
  -    {_, Pos} = binary_search(Key, KeyValues, KeyNo, 1, KeyNo, SortFunction),
  -    {KeyNo_1, SubtreeNo_1, KeyValues_1, Subtrees_1} = lists:nth(Pos, Subtrees),
  -    iterator_from_1(Key, {KeyNo_1, SubtreeNo_1, KeyValues_1, Subtrees_1}, [{lists:sublist(KeyValues, Pos, KeyNo), lists:sublist(Subtrees, Pos, SubtreeNo), State} | Iterator], SortFunction, State);
   iterator_from_1(Key, {KeyNo, SubtreeNo, KeyValues, SubtreesKey}, Iterator, SortFunction, {StateTarget, _, _, LookupFunction} = State) ->
       Subtrees = LookupFunction(StateTarget, lookup, SubtreesKey),
       {_, Pos} = binary_search(Key, KeyValues, KeyNo, 1, KeyNo, SortFunction),
  
positive.code:                 SubtreeNo,
                  KeyValues,
                      lists:sublist(Subtrees, 1, KeyPos - 1) ++ [delete_1(Key, TreeXC, SubtreeNoMin, KeyNoMax, SortFunction, nil)] ++ lists:sublist(Subtrees, KeyPos + 1, SubtreeNo)
              }
      end;
  delete_1_3(Key, {KeyNo, SubtreeNo, KeyValues, Subtrees}, SubtreeNoMin, KeyNoMax, KeyPos, SortFunction, {StateTarget, _, InsertFunction, _} = State) when is_list(Subtrees) ->
      {KeyNoXCLeft, SubtreeNoXCLeft, KeyValuesXCLeft, SubtreesXCLeft} = case KeyPos == 1 of
                                                                            true ->
                                                                                {0, 0, [], []};
                                                                            _ ->
                                                                                lists:nth(KeyPos - 1, Subtrees)
                                                                        end,
      {KeyNoXC, SubtreeNoXC, KeyValuesXC, SubtreesXC} = TreeXC = lists:nth(KeyPos, Subtrees),
      {KeyNoXCRight, SubtreeNoXCRight, KeyValuesXCRight, SubtreesXCRight} = case KeyPos == SubtreeNo of
                                                                                true ->
                                                                                    {0, 0, [], []};
                                                                                _ ->
                                                                                    lists:nth(KeyPos + 1, Subtrees)
                                                                            end,
      case KeyNoXC < SubtreeNoMin of
          true ->
              if
                  KeyNoXCLeft >= SubtreeNoMin ->
                      % CLRS: case 3a - left
                      {
                          KeyNo,
                          SubtreeNo,
                              lists:sublist(KeyValues, 1, KeyPos - 2) ++ [lists:nth(KeyNoXCLeft, KeyValuesXCLeft)] ++ lists:sublist(KeyValues, KeyPos, KeyNo),
                          InsertFunction(StateTarget,
                              insert,
                              lists:sublist(Subtrees, 1, KeyPos - 2) ++
                                  [{KeyNoXCLeft - 1, case SubtreeNoXCLeft == 0 of
                                                         true ->
                                                             0;
                                                         _ ->
                                                             SubtreeNoXCLeft - 1
                                                     end, lists:sublist(KeyValuesXCLeft, 1, KeyNoXCLeft - 1),
                                      InsertFunction(StateTarget,
                                          insert,
                                          case SubtreeNoXCLeft == 0 of
                                              true ->
                                                  [];
                                              _ ->
                                                  lists:sublist(SubtreesXCLeft, 1, KeyNoXCLeft)
                                          end
                                      )
                                  },
                                      delete_1(Key, {KeyNoXC + 1, case SubtreeNoXC == 0 of
                                                                      true ->
                                                                          0;
                                                                      _ ->
                                                                          SubtreeNoXC + 1
                                                                  end,
                                              [lists:nth(KeyPos - 1, KeyValues)] ++ lists:sublist(KeyValuesXC, 1, KeyNoXC),
                                          InsertFunction(StateTarget,
                                              insert,
                                              case SubtreeNoXCLeft == 0 of
                                                  true ->
                                                      [];
                                                  _ ->
                                                      [lists:nth(SubtreeNoXCLeft, SubtreesXCLeft)]
                                              end ++ SubtreesXC
                                          )
                                      }, SubtreeNoMin, KeyNoMax, SortFunction, State)
                                  ] ++ lists:sublist(Subtrees, KeyPos + 1, SubtreeNo)
                          )
                      };
                  KeyNoXCRight >= SubtreeNoMin ->
                      % CLRS: case 3a - right
                      {
                          KeyNo,
                          SubtreeNo,
                              lists:sublist(KeyValues, 1, KeyPos - 1) ++ [lists:nth(1, KeyValuesXCRight)] ++ lists:sublist(KeyValues, KeyPos + 1, KeyNo),
                          InsertFunction(StateTarget,
                              insert,
                              lists:sublist(Subtrees, 1, KeyPos - 1) ++
                                  [delete_1(Key, {KeyNoXC + 1, case SubtreeNoXC == 0 of
                                                                   true ->
                                                                       0;
                                                                   _ ->
                                                                       SubtreeNoXC + 1
                                                               end, lists:sublist(KeyValuesXC, 1, KeyNoXC) ++ [lists:nth(KeyPos, KeyValues)],
                                      InsertFunction(StateTarget,
                                          insert,
                                          SubtreesXC ++ case SubtreeNoXCRight == 0 of
                                                            true ->
                                                                [];
                                                            _ ->
                                                                [lists:nth(1, SubtreesXCRight)]
                                                        end
                                      )
                                  }, SubtreeNoMin, KeyNoMax, SortFunction, State),
                                      {KeyNoXCRight - 1, case SubtreeNoXCRight == 0 of
                                                             true ->
                                                                 0;
                                                             _ ->
                                                                 SubtreeNoXCRight - 1
                                                         end, lists:sublist(KeyValuesXCRight, 2, KeyNoXCRight),
                                          InsertFunction(StateTarget,
                                              insert,
                                              case SubtreeNoXCRight == 0 of
                                                  true ->
                                                      [];
                                                  _ ->
                                                      lists:sublist(SubtreesXCRight, 2, SubtreeNoXCRight)
                                              end
                                          )
                                      }
                                  ] ++ lists:sublist(Subtrees, KeyPos + 2, SubtreeNo)
                          )
                      };
                  KeyNoXCRight == 0 ->
                      % CLRS: case 3b left
                      case KeyNo == 1 of
                          true ->
                              delete_1(Key, {KeyNoXCLeft + KeyNoXC + 1, SubtreeNoXCLeft + SubtreeNoXC, KeyValuesXCLeft ++ KeyValues ++ KeyValuesXC, SubtreesXCLeft ++ SubtreesXC}, SubtreeNoMin, KeyNoMax, SortFunction, State);
                          _ ->
                              {
                                  KeyNo - 1,
                                  SubtreeNo - 1,
                                      lists:sublist(KeyValues, 1, KeyPos - 2) ++ lists:sublist(KeyValues, KeyPos, KeyNo),
                                  InsertFunction(StateTarget,
                                      insert,
                                      lists:sublist(Subtrees, 1, KeyPos - 2) ++
                                          [
                                              delete_1(Key, {KeyNoXCLeft + KeyNoXC + 1, case SubtreeNoXC == 0 of
                                                                                            true ->
                                                                                                0;
                                                                                            _ ->
                                                                                                SubtreeNoXCLeft + SubtreeNoXC
                                                                                        end, KeyValuesXCLeft ++ [lists:nth(KeyPos - 1, KeyValues)] ++ KeyValuesXC,
                                                  InsertFunction(StateTarget,
                                                      insert,
                                                      SubtreesXCLeft ++ SubtreesXC
                                                  )
                                              }, SubtreeNoMin, KeyNoMax, SortFunction, State)
                                          ] ++ lists:sublist(Subtrees, KeyPos + 1, SubtreeNo)
                                  )
                              }
                      end;
                  true ->
                      % CLRS: case 3b right
                      case KeyNo == 1 of
                          true ->
                              delete_1(Key, {KeyNoXC + KeyNoXCRight + 1, SubtreeNoXC + SubtreeNoXCRight, KeyValuesXC ++ KeyValues ++ KeyValuesXCRight, SubtreesXC ++ SubtreesXCRight}, SubtreeNoMin, KeyNoMax, SortFunction, State);
                          _ ->
                              {
                                  KeyNo - 1,
                                  SubtreeNo - 1,
                                      lists:sublist(KeyValues, 1, KeyPos - 1) ++ lists:sublist(KeyValues, KeyPos + 1, KeyNo),
                                  InsertFunction(StateTarget,
                                      insert,
                                      lists:sublist(Subtrees, 1, KeyPos - 1) ++
                                          [
                                              delete_1(Key, {KeyNoXC + KeyNoXCRight + 1, case SubtreeNoXC == 0 of
                                                                                             true ->
                                                                                                 0;
                                                                                             _ ->
                                                                                                 SubtreeNoXC + SubtreeNoXCRight
                                                                                         end, KeyValuesXC ++ [lists:nth(KeyPos, KeyValues)] ++ KeyValuesXCRight,
                                                  InsertFunction(StateTarget,
                                                      insert,
                                                      SubtreesXC ++ SubtreesXCRight
                                                  )
                                              }, SubtreeNoMin, KeyNoMax, SortFunction, State)
                                          ] ++ lists:sublist(Subtrees, KeyPos + 2, SubtreeNo)
                                  )
                              }
                      end
              end;
          _ ->
              % CLRS: case 3 else
              {
                  KeyNo,
                  SubtreeNo,
                  KeyValues,
                  InsertFunction(StateTarget,
                      insert,
                      lists:sublist(Subtrees, 1, KeyPos - 1) ++ [delete_1(Key, TreeXC, SubtreeNoMin, KeyNoMax, SortFunction, State)] ++ lists:sublist(Subtrees, KeyPos + 1, SubtreeNo)
                  )
              }
      end;
  delete_1_3(Key, {KeyNo, SubtreeNo, KeyValues, Subtrees}, SubtreeNoMin, KeyNoMax, KeyPos, SortFunction, {StateTarget, _, InsertFunction, _} = State) when is_list(Subtrees) ->
      {KeyNoXCLeft, SubtreeNoXCLeft, KeyValuesXCLeft, SubtreesXCLeft} = case KeyPos == 1 of
                                                                            true ->
                                                                                {0, 0, [], []};
                                                                            _ ->
                                                                                lists:nth(KeyPos - 1, Subtrees)
                                                                        end,
      {KeyNoXC, SubtreeNoXC, KeyValuesXC, SubtreesXC} = TreeXC = lists:nth(KeyPos, Subtrees),
      {KeyNoXCRight, SubtreeNoXCRight, KeyValuesXCRight, SubtreesXCRight} = case KeyPos == SubtreeNo of
                                                                                true ->
                                                                                    {0, 0, [], []};
                                                                                _ ->
                                                                                    lists:nth(KeyPos + 1, Subtrees)
                                                                            end,
      case KeyNoXC < SubtreeNoMin of
          true ->
              if
                  KeyNoXCLeft >= SubtreeNoMin ->
                      % CLRS: case 3a - left
                      {
                          KeyNo,
                          SubtreeNo,
                              lists:sublist(KeyValues, 1, KeyPos - 2) ++ [lists:nth(KeyNoXCLeft, KeyValuesXCLeft)] ++ lists:sublist(KeyValues, KeyPos, KeyNo),
                          InsertFunction(StateTarget,
                              insert,
                              lists:sublist(Subtrees, 1, KeyPos - 2) ++
                                  [{KeyNoXCLeft - 1, case SubtreeNoXCLeft == 0 of
                                                         true ->
                                                             0;
                                                         _ ->
                                                             SubtreeNoXCLeft - 1
                                                     end, lists:sublist(KeyValuesXCLeft, 1, KeyNoXCLeft - 1),
                                      InsertFunction(StateTarget,
                                          insert,
                                          case SubtreeNoXCLeft == 0 of
                                              true ->
                                                  [];
                                              _ ->
                                                  lists:sublist(SubtreesXCLeft, 1, KeyNoXCLeft)
                                          end
                                      )
                                  },
                                      delete_1(Key, {KeyNoXC + 1, case SubtreeNoXC == 0 of
                                                                      true ->
                                                                          0;
                                                                      _ ->
                                                                          SubtreeNoXC + 1
                                                                  end,
                                              [lists:nth(KeyPos - 1, KeyValues)] ++ lists:sublist(KeyValuesXC, 1, KeyNoXC),
                                          InsertFunction(StateTarget,
                                              insert,
                                              case SubtreeNoXCLeft == 0 of
                                                  true ->
                                                      [];
                                                  _ ->
                                                      [lists:nth(SubtreeNoXCLeft, SubtreesXCLeft)]
                                              end ++ SubtreesXC
                                          )
                                      }, SubtreeNoMin, KeyNoMax, SortFunction, State)
                                  ] ++ lists:sublist(Subtrees, KeyPos + 1, SubtreeNo)
                          )
                      };
                  KeyNoXCRight >= SubtreeNoMin ->
                      % CLRS: case 3a - right
                      {
                          KeyNo,
                          SubtreeNo,
                              lists:sublist(KeyValues, 1, KeyPos - 1) ++ [lists:nth(1, KeyValuesXCRight)] ++ lists:sublist(KeyValues, KeyPos + 1, KeyNo),
                          InsertFunction(StateTarget,
                              insert,
                              lists:sublist(Subtrees, 1, KeyPos - 1) ++
                                  [delete_1(Key, {KeyNoXC + 1, case SubtreeNoXC == 0 of
                                                                   true ->
                                                                       0;
                                                                   _ ->
                                                                       SubtreeNoXC + 1
                                                               end, lists:sublist(KeyValuesXC, 1, KeyNoXC) ++ [lists:nth(KeyPos, KeyValues)],
                                      InsertFunction(StateTarget,
                                          insert,
                                          SubtreesXC ++ case SubtreeNoXCRight == 0 of
                                                            true ->
                                                                [];
                                                            _ ->
                                                                [lists:nth(1, SubtreesXCRight)]
                                                        end
                                      )
                                  }, SubtreeNoMin, KeyNoMax, SortFunction, State),
                                      {KeyNoXCRight - 1, case SubtreeNoXCRight == 0 of
                                                             true ->
                                                                 0;
                                                             _ ->
                                                                 SubtreeNoXCRight - 1
                                                         end, lists:sublist(KeyValuesXCRight, 2, KeyNoXCRight),
                                          InsertFunction(StateTarget,
                                              insert,
                                              case SubtreeNoXCRight == 0 of
                                                  true ->
                                                      [];
                                                  _ ->
                                                      lists:sublist(SubtreesXCRight, 2, SubtreeNoXCRight)
                                              end
                                          )
                                      }
                                  ] ++ lists:sublist(Subtrees, KeyPos + 2, SubtreeNo)
                          )
                      };
                  KeyNoXCRight == 0 ->
                      % CLRS: case 3b left
                      case KeyNo == 1 of
                          true ->
                              delete_1(Key, {KeyNoXCLeft + KeyNoXC + 1, SubtreeNoXCLeft + SubtreeNoXC, KeyValuesXCLeft ++ KeyValues ++ KeyValuesXC, SubtreesXCLeft ++ SubtreesXC}, SubtreeNoMin, KeyNoMax, SortFunction, State);
                          _ ->
                              {
                                  KeyNo - 1,
                                  SubtreeNo - 1,
                                      lists:sublist(KeyValues, 1, KeyPos - 2) ++ lists:sublist(KeyValues, KeyPos, KeyNo),
                                  InsertFunction(StateTarget,
                                      insert,
                                      lists:sublist(Subtrees, 1, KeyPos - 2) ++
                                          [
                                              delete_1(Key, {KeyNoXCLeft + KeyNoXC + 1, case SubtreeNoXC == 0 of
                                                                                            true ->
                                                                                                0;
                                                                                            _ ->
                                                                                                SubtreeNoXCLeft + SubtreeNoXC
                                                                                        end, KeyValuesXCLeft ++ [lists:nth(KeyPos - 1, KeyValues)] ++ KeyValuesXC,
                                                  InsertFunction(StateTarget,
                                                      insert,
                                                      SubtreesXCLeft ++ SubtreesXC
                                                  )
                                              }, SubtreeNoMin, KeyNoMax, SortFunction, State)
                                          ] ++ lists:sublist(Subtrees, KeyPos + 1, SubtreeNo)
                                  )
                              }
                      end;
                  true ->
                      % CLRS: case 3b right
                      case KeyNo == 1 of
                          true ->
                              delete_1(Key, {KeyNoXC + KeyNoXCRight + 1, SubtreeNoXC + SubtreeNoXCRight, KeyValuesXC ++ KeyValues ++ KeyValuesXCRight, SubtreesXC ++ SubtreesXCRight}, SubtreeNoMin, KeyNoMax, SortFunction, State);
                          _ ->
                              {
                                  KeyNo - 1,
                                  SubtreeNo - 1,
                                      lists:sublist(KeyValues, 1, KeyPos - 1) ++ lists:sublist(KeyValues, KeyPos + 1, KeyNo),
                                  InsertFunction(StateTarget,
                                      insert,
                                      lists:sublist(Subtrees, 1, KeyPos - 1) ++
                                          [
                                              delete_1(Key, {KeyNoXC + KeyNoXCRight + 1, case SubtreeNoXC == 0 of
                                                                                             true ->
                                                                                                 0;
                                                                                             _ ->
                                                                                                 SubtreeNoXC + SubtreeNoXCRight
                                                                                         end, KeyValuesXC ++ [lists:nth(KeyPos, KeyValues)] ++ KeyValuesXCRight,
                                                  InsertFunction(StateTarget,
                                                      insert,
                                                      SubtreesXC ++ SubtreesXCRight
                                                  )
                                              }, SubtreeNoMin, KeyNoMax, SortFunction, State)
                                          ] ++ lists:sublist(Subtrees, KeyPos + 2, SubtreeNo)
                                  )
                              }
                      end
              end;
          _ ->
              % CLRS: case 3 else
              {
                  KeyNo,
                  SubtreeNo,
                  KeyValues,
                  InsertFunction(StateTarget,
                      insert,
                      lists:sublist(Subtrees, 1, KeyPos - 1) ++ [delete_1(Key, TreeXC, SubtreeNoMin, KeyNoMax, SortFunction, State)] ++ lists:sublist(Subtrees, KeyPos + 1, SubtreeNo)
                  )
              }
      end;
  delete_1_3(Key, {KeyNo, SubtreeNo, KeyValues, SubtreesKey}, SubtreeNoMin, KeyNoMax, KeyPos, SortFunction, {StateTarget, DeleteFunction, InsertFunction, LookupFunction} = State) ->
  
negative.code: iterator_1({KeyValues, Subtrees, nil}, Iterator) ->
      {_, _, KeyValues_1, Subtrees_1} = lists:nth(1, Subtrees),
      iterator_1({KeyValues_1, Subtrees_1, nil}, [{KeyValues, Subtrees, nil} | Iterator]);
  iterator_1({KeyValues, SubtreesKey, {StateTarget, _, _, LookupFunction} = State}, Iterator) ->
      Subtrees = LookupFunction(StateTarget, lookup, SubtreesKey),
      {_, _, KeyValues_1, Subtrees_1Key} = lists:nth(1, Subtrees),
  