Created
April 13, 2012 13:47
-
-
Save davidsan/2376968 to your computer and use it in GitHub Desktop.
Vérification de l'équilibre d'un arbre binaire de recherche complet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*header */ | |
| /** | |
| * @brief Fonction qui calcule la hauteur du noeud | |
| * @param node Pointeur vers le noeud | |
| * @return Hauteur du noeud | |
| */ | |
| int getHeight(AVL * node); | |
| /** | |
| * @brief Fonction qui teste si le noeud est équilibré | |
| * @param node Pointeur vers le noeud à tester | |
| * @return 1 si le noeud est équilibré, 0 sinon. | |
| */ | |
| int isBalanced(AVL * node); | |
| /** | |
| * @brief Fonction qui teste si l'arbre binaire de recherche est équilibré | |
| * @param root Pointeur vers la racine de l'arbre binaire de recherche | |
| * @return 1 si l'arbre binaire de recherche est équilibré. | |
| */ | |
| int checkBalance(AVL * root); | |
| /** | |
| * @brief Fonction qui calcule la hauteur de l'arbre en parcourant l'arbre | |
| * @param root Racine de l'arbre | |
| * @return Hauteur de l'arbre | |
| */ | |
| int height(AVL * root); | |
| /** | |
| * @brief Fonction qui renvoie le plus grand entier de deux entiers | |
| * @param a Premier entier | |
| * @param b Deuxième entier | |
| * @return Le plus grand entier de deux entiers | |
| */ | |
| int max(int a, int b); | |
| /** | |
| * @brief Fonction qui compare deux noeuds | |
| * @param a Premier noeud | |
| * @param b Deuxième noeud | |
| * @return 1 si le noeud a est inférieur au noeud b, 0 sinon | |
| */ | |
| /* source */ | |
| int getHeight(AVL * node) { | |
| if (node != NULL) { | |
| return node->height; | |
| } else { | |
| return 0; | |
| } | |
| } | |
| int isBalanced(AVL * node) { | |
| int diff; | |
| if (!node) { | |
| return 1; | |
| } | |
| diff = abs(getHeight(node->left) - getHeight(node->right)); | |
| return diff <= 1; | |
| } | |
| int checkBalance(AVL * root) { | |
| if (!root) { | |
| return 1; | |
| } | |
| return isBalanced(root) && checkBalance(root->left) && checkBalance( | |
| root->right); | |
| } | |
| int height(AVL * root){ | |
| if(!root){ | |
| return 0; | |
| } | |
| else{ | |
| return 1+max(height(root->left), height(root->right)); | |
| } | |
| } | |
| int max(int a, int b) { | |
| if (a < b) { | |
| return b; | |
| } | |
| return a; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment