C/Visual C - Ocupando código en C++ de árbol balanceado

 
Vista:

Ocupando código en C++ de árbol balanceado

Publicado por Ricardo (1 intervención) el 10/05/2001 17:51:27
Necesito el código de un àrbo balanceado... Urge!!!!
Lo necesito para hacer un compresor de datos.... NO PREGUNTEN!!!!

Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Implementación de un árbol balanceado (AVL) en C++

Publicado por Alejandro (913 intervenciones) el 24/09/2024 20:41:17
Aquí tienes un ejemplo de código en C++ para implementar un árbol balanceado (AVL):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
 
// Estructura para representar un nodo del árbol AVL
struct Node {
    int data;
    int height;
    Node* left;
    Node* right;
};
 
// Función para obtener la altura de un nodo
int getHeight(Node* node) {
    if (node == nullptr) {
        return 0;
    }
    return node->height;
}
 
// Función para obtener el factor de equilibrio de un nodo
int getBalanceFactor(Node* node) {
    if (node == nullptr) {
        return 0;
    }
    return getHeight(node->left) - getHeight(node->right);
}
 
// Función para actualizar la altura de un nodo
void updateHeight(Node* node) {
    int leftHeight = getHeight(node->left);
    int rightHeight = getHeight(node->right);
    node->height = std::max(leftHeight, rightHeight) + 1;
}
 
// Función para realizar una rotación simple a la derecha
Node* rotateRight(Node* node) {
    Node* newRoot = node->left;
    node->left = newRoot->right;
    newRoot->right = node;
    updateHeight(node);
    updateHeight(newRoot);
    return newRoot;
}
 
// Función para realizar una rotación simple a la izquierda
Node* rotateLeft(Node* node) {
    Node* newRoot = node->right;
    node->right = newRoot->left;
    newRoot->left = node;
    updateHeight(node);
    updateHeight(newRoot);
    return newRoot;
}
 
// Función para balancear un nodo
Node* balanceNode(Node* node) {
    updateHeight(node);
    int balanceFactor = getBalanceFactor(node);
    if (balanceFactor > 1) {
        if (getBalanceFactor(node->left) < 0) {
            node->left = rotateLeft(node->left);
        }
        return rotateRight(node);
    }
    if (balanceFactor < -1) {
        if (getBalanceFactor(node->right) > 0) {
            node->right = rotateRight(node->right);
        }
        return rotateLeft(node);
    }
    return node;
}
 
// Función para insertar un nodo en el árbol AVL
Node* insertNode(Node* root, int data) {
    if (root == nullptr) {
        Node* newNode = new Node;
        newNode->data = data;
        newNode->height = 1;
        newNode->left = nullptr;
        newNode->right = nullptr;
        return newNode;
    }
    if (data < root->data) {
        root->left = insertNode(root->left, data);
    } else {
        root->right = insertNode(root->right, data);
    }
    return balanceNode(root);
}
 
// Función para imprimir el árbol AVL en orden
void printInOrder(Node* root) {
    if (root == nullptr) {
        return;
    }
    printInOrder(root->left);
    std::cout << root->data << " ";
    printInOrder(root->right);
}
 
int main() {
    Node* root = nullptr;
 
    // Insertar nodos en el árbol AVL
    root = insertNode(root, 10);
    root = insertNode(root, 20);
    root = insertNode(root, 30);
    root = insertNode(root, 40);
    root = insertNode(root, 50);
    root = insertNode(root, 25);
 
    // Imprimir el árbol AVL en orden
    std::cout << "Árbol AVL en orden: ";
    printInOrder(root);
    std::cout << std::endl;
 
    return 0;
}

Este código implementa un árbol AVL básico en C++. La función `insertNode` se utiliza para insertar nodos en el árbol AVL, y la función `printInOrder` se utiliza para imprimir los nodos en orden.

Recuerda que este es solo un ejemplo básico para implementar un árbol AVL en C++. Puedes personalizar y ampliar el código según tus necesidades específicas para tu compresor de datos.

Espero que esto te ayude a implementar un árbol balanceado en C++. ¡Buena suerte con tu proyecto, Ricardo!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar