Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
160
Task/AVL-tree/FreeBASIC/avl-tree-1.basic
Normal file
160
Task/AVL-tree/FreeBASIC/avl-tree-1.basic
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
#define max(a, b) iif((a > b), a, b)
|
||||
|
||||
Enum
|
||||
_KEY
|
||||
_RIGHT
|
||||
_LEFT
|
||||
_HEIGHT
|
||||
End Enum
|
||||
|
||||
Dim Shared As Integer root = 0
|
||||
Dim Shared As Integer ramas = 5, indice = 0
|
||||
Dim Shared As Integer arbol(ramas, 4)
|
||||
|
||||
Function height(current As Integer) As Integer
|
||||
Return Iif(current = 0, 0, arbol(current, _HEIGHT))
|
||||
End Function
|
||||
|
||||
Function Balance(current As Integer) As Integer
|
||||
Return height(arbol(current, _LEFT)) - height(arbol(current, _RIGHT))
|
||||
End Function
|
||||
|
||||
Function rotateRight(y As Integer) As Integer
|
||||
Dim As Integer x, T2
|
||||
|
||||
x = arbol(y, _LEFT)
|
||||
T2 = arbol(x, _RIGHT)
|
||||
arbol(x, _RIGHT) = y
|
||||
arbol(y, _LEFT) = T2
|
||||
arbol(y, _HEIGHT) = max(height(arbol(y, _LEFT)), height(arbol(y, _RIGHT))) + 1
|
||||
arbol(x, _HEIGHT) = max(height(arbol(x, _LEFT)), height(arbol(x, _RIGHT))) + 1
|
||||
Return x
|
||||
End Function
|
||||
|
||||
Function rotateLeft(x As Integer) As Integer
|
||||
Dim As Integer y, T2
|
||||
|
||||
y = arbol(x, _RIGHT)
|
||||
T2 = arbol(y, _LEFT)
|
||||
arbol(y, _LEFT) = x
|
||||
arbol(x, _RIGHT) = T2
|
||||
arbol(x, _HEIGHT) = max(height(arbol(x, _LEFT)), height(arbol(x, _RIGHT))) + 1
|
||||
arbol(y, _HEIGHT) = max(height(arbol(y, _LEFT)), height(arbol(y, _RIGHT))) + 1
|
||||
Return y
|
||||
End Function
|
||||
|
||||
Function insert(current As Integer, key As Integer) As Integer
|
||||
Dim As Integer balanceo
|
||||
|
||||
If current = 0 Then
|
||||
indice += 1
|
||||
If indice > ramas Then
|
||||
ramas += 5
|
||||
Redim Preserve arbol(ramas, 4)
|
||||
End If
|
||||
arbol(indice, _KEY) = key
|
||||
arbol(indice, _HEIGHT) = 1
|
||||
Return indice
|
||||
End If
|
||||
|
||||
If key < arbol(current, _KEY) Then
|
||||
arbol(current, _LEFT) = insert(arbol(current, _LEFT), key)
|
||||
Elseif key > arbol(current, _KEY) Then
|
||||
arbol(current, _RIGHT) = insert(arbol(current, _RIGHT), key)
|
||||
Else
|
||||
Return current
|
||||
End If
|
||||
|
||||
arbol(current, _HEIGHT) = max(height(arbol(current, _LEFT)), height(arbol(current, _RIGHT))) + 1
|
||||
balanceo = Balance(current)
|
||||
|
||||
If balanceo > 1 And key < arbol(arbol(current, _LEFT), _KEY) Then Return rotateRight(current)
|
||||
If balanceo < -1 And key > arbol(arbol(current, _RIGHT), _KEY) Then Return rotateLeft(current)
|
||||
If balanceo > 1 And key > arbol(arbol(current, _LEFT), _KEY) Then
|
||||
arbol(current, _LEFT) = rotateLeft(arbol(current, _LEFT))
|
||||
Return rotateRight(current)
|
||||
End If
|
||||
If balanceo < -1 And key < arbol(arbol(current, _RIGHT), _KEY) Then
|
||||
arbol(current, _RIGHT) = rotateRight(arbol(current, _RIGHT))
|
||||
Return rotateLeft(current)
|
||||
End If
|
||||
|
||||
Return current
|
||||
End Function
|
||||
|
||||
Function minValueNode(current As Integer) As Integer
|
||||
While arbol(current, _LEFT) '<> 0
|
||||
current = arbol(current, _LEFT)
|
||||
Wend
|
||||
Return current
|
||||
End Function
|
||||
|
||||
Function deleteNode(root As Integer, key As Integer) As Integer
|
||||
Dim As Integer temp, balanceo
|
||||
' Find the node and delete it
|
||||
If root = 0 Then Return root
|
||||
|
||||
If key < arbol(root, _KEY) Then
|
||||
arbol(root, _LEFT) = deleteNode(arbol(root, _LEFT), key)
|
||||
Elseif key > arbol(root, _KEY) Then
|
||||
arbol(root, _RIGHT) = deleteNode(arbol(root, _RIGHT), key)
|
||||
Else
|
||||
If arbol(root, _LEFT) = 0 Or arbol(root, _RIGHT) = 0 Then
|
||||
temp = max(arbol(root, _LEFT), arbol(root, _RIGHT))
|
||||
If temp = 0 Then
|
||||
temp = root
|
||||
root = 0
|
||||
Else
|
||||
root = temp
|
||||
End If
|
||||
Else
|
||||
temp = minValueNode(arbol(root, _RIGHT))
|
||||
arbol(root, _KEY) = arbol(temp, _KEY)
|
||||
arbol(root, _RIGHT) = deleteNode(arbol(root, _RIGHT), arbol(temp, _KEY))
|
||||
End If
|
||||
End If
|
||||
|
||||
If root = 0 Then Return root
|
||||
|
||||
' Update the balance factor of each node and balance the tree
|
||||
arbol(root, _HEIGHT) = 1 + max(height(arbol(root, _LEFT)), height(arbol(root, _RIGHT)))
|
||||
|
||||
balanceo = Balance(root)
|
||||
If balanceo > 1 And Balance(arbol(root, _LEFT)) >= 0 Then Return rotateRight(root)
|
||||
If balanceo > 1 And Balance(arbol(root, _LEFT)) < 0 Then
|
||||
arbol(root, _LEFT) = rotateLeft(arbol(root, _LEFT))
|
||||
Return rotateRight(root)
|
||||
End If
|
||||
If balanceo < -1 And Balance(arbol(root, _RIGHT)) <= 0 Then Return rotateLeft(root)
|
||||
If balanceo < -1 And Balance(arbol(root, _RIGHT)) > 0 Then
|
||||
arbol(root, _RIGHT) = rotateRight(arbol(root, _RIGHT))
|
||||
Return rotateLeft(root)
|
||||
End If
|
||||
|
||||
Return root
|
||||
End Function
|
||||
|
||||
Sub preOrder(temp As Integer)
|
||||
If temp <> 0 Then
|
||||
Print arbol(temp, _KEY); arbol(temp, _HEIGHT); Balance(temp)
|
||||
preOrder(arbol(temp, _LEFT))
|
||||
preOrder(arbol(temp, _RIGHT))
|
||||
End If
|
||||
End Sub
|
||||
|
||||
root = insert(root, 2)
|
||||
root = insert(root, 1)
|
||||
root = insert(root, 7)
|
||||
root = insert(root, 4)
|
||||
root = insert(root, 5)
|
||||
root = insert(root, 3)
|
||||
root = insert(root, 8)
|
||||
|
||||
preOrder(root)
|
||||
|
||||
root = deleteNode(root, 3)
|
||||
|
||||
Print !"\nAfter deletion: "
|
||||
preOrder(root)
|
||||
|
||||
Sleep
|
||||
139
Task/AVL-tree/FreeBASIC/avl-tree-2.basic
Normal file
139
Task/AVL-tree/FreeBASIC/avl-tree-2.basic
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
Type Nodo
|
||||
valor As Integer
|
||||
izda As Nodo Ptr
|
||||
dcha As Nodo Ptr
|
||||
End Type
|
||||
|
||||
Function createNodo(Byval v As Integer, Byval l As Nodo Ptr, Byval r As Nodo Ptr) As Nodo Ptr
|
||||
Dim nuevo As Nodo Ptr = 0
|
||||
nuevo = Callocate(Len(Nodo))
|
||||
nuevo->valor = v
|
||||
nuevo->izda = l
|
||||
nuevo->dcha = r
|
||||
Return nuevo
|
||||
End Function
|
||||
|
||||
Function insertar(Byval v As Integer, n As Nodo Ptr) As Nodo Ptr
|
||||
If (n = 0) Then
|
||||
n = createNodo(v, 0, 0)
|
||||
Elseif (v < n->valor) Then
|
||||
n->izda = insertar(v, n->izda)
|
||||
Elseif (v > n->valor) Then
|
||||
n->dcha = insertar(v, n->dcha)
|
||||
End If
|
||||
Return n
|
||||
End Function
|
||||
|
||||
Function encuentraMin(Byval n As Nodo Ptr) As Nodo Ptr
|
||||
If (n = 0) Then Return 0
|
||||
|
||||
While (n->izda <> 0)
|
||||
n = n->izda
|
||||
Wend
|
||||
Return n
|
||||
End Function
|
||||
|
||||
Function encuentraMax(Byval n As Nodo Ptr) As Nodo Ptr
|
||||
If (n = 0) Then Return 0
|
||||
|
||||
While (n->dcha <> 0)
|
||||
n = n->dcha
|
||||
Wend
|
||||
Return n
|
||||
End Function
|
||||
|
||||
Function preOrden(Byval n As Nodo Ptr) As Nodo Ptr
|
||||
If (n = 0) Then Return 0
|
||||
|
||||
Print (n->valor);
|
||||
preOrden(n->izda)
|
||||
preOrden(n->dcha)
|
||||
Return 0
|
||||
End Function
|
||||
|
||||
Function postOrden(Byval n As Nodo Ptr) As Nodo Ptr
|
||||
If (n = 0) Then Return 0
|
||||
|
||||
postOrden(n->izda)
|
||||
postOrden(n->dcha)
|
||||
Print (n->valor);
|
||||
Return 0
|
||||
End Function
|
||||
|
||||
Function enOrden(Byval n As Nodo Ptr) As Nodo Ptr
|
||||
If (n = 0) Then Return 0
|
||||
|
||||
enOrden(n->izda)
|
||||
Print (n->valor);
|
||||
enOrden(n->dcha)
|
||||
Return 0
|
||||
End Function
|
||||
|
||||
Function eliminarMin(n As Nodo Ptr) As Nodo Ptr
|
||||
If n = 0 Then Return 0
|
||||
|
||||
Dim As Nodo Ptr old = 0
|
||||
If (n->izda <> 0) Then
|
||||
n->izda = eliminarMin(n->izda)
|
||||
Else
|
||||
old = n
|
||||
n = n->dcha
|
||||
Deallocate old
|
||||
End If
|
||||
Return n
|
||||
End Function
|
||||
|
||||
Function eliminarMax(n As Nodo Ptr) As Nodo Ptr
|
||||
If n = 0 Then Return 0
|
||||
|
||||
Dim As Nodo Ptr old = 0
|
||||
If (n->dcha <> 0) Then
|
||||
n->dcha = eliminarMax(n->dcha)
|
||||
Else
|
||||
old = n
|
||||
n = n->izda
|
||||
Deallocate old
|
||||
End If
|
||||
Return n
|
||||
End Function
|
||||
|
||||
Function eliminar(Byval v As Integer, n As Nodo Ptr) As Nodo Ptr
|
||||
If n = 0 Then Return 0
|
||||
|
||||
Dim As Nodo Ptr old = 0
|
||||
If v < n->valor Then
|
||||
n->izda = eliminar(v, n->izda)
|
||||
Elseif v > n->valor Then
|
||||
n->dcha = eliminar(v, n->dcha)
|
||||
Elseif (n->izda <> 0) And (n->dcha <> 0) Then
|
||||
n->valor = encuentraMax(n->izda)->valor
|
||||
n->izda = eliminarMax(n->izda)
|
||||
Else
|
||||
old = n
|
||||
n = Iif(n->izda = 0, n->dcha, n->izda)
|
||||
Deallocate old
|
||||
End If
|
||||
Return n
|
||||
End Function
|
||||
|
||||
Dim raiz As Nodo Ptr = 0
|
||||
|
||||
raiz = insertar(10, raiz)
|
||||
raiz = insertar(11, raiz)
|
||||
raiz = insertar(5 , raiz)
|
||||
raiz = insertar(8 , raiz)
|
||||
raiz = insertar(2 , raiz)
|
||||
raiz = insertar(7 , raiz)
|
||||
|
||||
Print "preOrder : "; preOrden(raiz)
|
||||
Print !"\postOrder : "; postOrden(raiz)
|
||||
Print !"\inOrder : "; enOrden(raiz)
|
||||
|
||||
Print !"\nremoving 5 from tree\n"
|
||||
raiz = eliminar(5, raiz)
|
||||
|
||||
Print "preOrder : "; preOrden(raiz)
|
||||
Print !"\postOrder : "; postOrden(raiz)
|
||||
Print !"\inOrder : "; enOrden(raiz)(root)
|
||||
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue