Bom galera sou iniciante, e estou aprendendo sobre árvores, achei uma árvore AVL já implementada na internet mas não estou entendendo 2 métodos do código, os métodos são o height e max. Eu sei o que eles fazem mas não entendo como funcionam. Vou colar um trecho do código aqui em baixo.
[code]private int height( No t )
{
return t == null ? -1 : t.height;
}
private No rotateWithLeftChild( No k2 )
{
No k1 = k2.esquerda;
k2.esquerda = k1.direita;
k1.direita = k2;
k2.height = max( height( k2.esquerda ), height( k2.direita ) ) + 1;
k1.height = max( height( k1.esquerda ), k2.height ) + 1;
return k1;
}
/**
* Rotate binary tree node with right child.
* For AVL trees, this is a single rotation for case 4.
* Update heights, then return new root.
*/
private No rotateWithRightChild( No k1 )
{
No k2 = k1.direita;
k1.direita = k2.esquerda;
k2.esquerda = k1;
k1.height = max( height( k1.esquerda ), height( k1.direita ) ) + 1;
k2.height = max( height( k2.direita ), k1.height ) + 1;
return k2;
}
/**
* Double rotate binary tree node: first left child
* with its right child; then node k3 with new left child.
* For AVL trees, this is a double rotation for case 2.
* Update heights, then return new root.
*/
private No doubleWithLeftChild( No k3 )
{
k3.esquerda = rotateWithRightChild( k3.esquerda );
return rotateWithLeftChild( k3 );
}
/**
* Double rotate binary tree node: first right child
* with its left child; then node k1 with new right child.
* For AVL trees, this is a double rotation for case 3.
* Update heights, then return new root.
*/
private No doubleWithRightChild( No k1 )
{
k1.direita = rotateWithLeftChild( k1.direita );
return rotateWithRightChild( k1 );
}
private int max( int lhs, int rhs )
{
return lhs > rhs ? lhs : rhs;
}[/code]
Valeu!