/// <summary> /// Cambia los padres cuando no son padres e hijos entre ellos /// </summary> /// <param name="node1"></param> /// <param name="node2"></param> private void SwapFathersEspecial(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2) { var father1 = node1.Father; var father2 = node2.Father; var node1IsRightChild = node1.IsRightChild(); var node2IsRightChild = node2.IsRightChild(); node2.Father = father1; if (node1IsRightChild) { father1.RigthChild = node2; } else if (father1 != null) { father1.LeftChild = node2; } node1.Father = father2; if (node2IsRightChild) { father2.RigthChild = node1; } else if (father2 != null) { father2.LeftChild = node1; } }
private void SwapWithOneFatherOfTheOther(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2) { var node1Father = node1.Father; var node1IsRightChild = node1.IsRightChild(); var node2IsRightChild = node2.IsRightChild(); var node1RightChild = node1.RigthChild; var node2RightChild = node2.RigthChild; var node1LeftChild = node1.LeftChild; var node2LeftChild = node2.LeftChild; node1.Father = node2; node2.Father = node1Father; if (node1IsRightChild) { node2.Father.RigthChild = node2; } else { if (node1Father != null) { node2.Father.LeftChild = node2; } } if (node2IsRightChild) { node2.RigthChild = node1; node2.LeftChild = node1LeftChild; if (node2.LeftChild != null) { node2.LeftChild.Father = node2; } } else { node2.LeftChild = node1; node2.RigthChild = node1RightChild; if (node2.RigthChild != null) { node2.RigthChild.Father = node2; } } node1.RigthChild = node2RightChild; if (node1.RigthChild != null) { node1.RigthChild.Father = node1; } node1.LeftChild = node2LeftChild; if (node1.LeftChild != null) { node1.LeftChild.Father = node1; } }
void Swap(AVLCountNodeKey <TKey, TValue> node1, AVLCountNodeKey <TKey, TValue> node2) { if (node1 == root) { root = node2; } if (node2 == root) { root = node1; } var node1IsRightChild = node1.IsRightChild(); var node2IsRightChild = node2.IsRightChild(); var node1Height = node1.Height; node1.Height = node2.Height; node2.Height = node1Height; var node1Father = node1.Father; var node2Father = node2.Father; var node1LeftChild = node1.LeftChild; var node2LeftChild = node2.LeftChild; var node1RightChild = node1.RigthChild; var node2RightChild = node2.RigthChild; if (!((node1.Father == node2) || (node2.Father == node1))) { SwapFathersEspecial(node1, node2); SwapRightChildEspecial(node1, node2); SwapLeftChildEspecial(node1, node2); } else if (node1.Father == node2) { SwapWithOneFatherOfTheOther(node2, node1); } else { SwapWithOneFatherOfTheOther(node1, node2); } }