Principe d une liste simplement chainée Algorithmes, structures, données Tail onqueue 8 7 dequeue head Arbre - Height (hauteur) : Taille du chemin le plus long vers une feuille - Size (taille) : Nombre de nœuds de l arbre - Lowness (bassesse) : Taille du chemin le plus court vers une feuille - Leaves (sorties) : Nombre de feuilles - Isomorphic: Deux arbres sont isomorphiques s ils ont la même structure (même nombre de nœuds et placés au même endroit) - Balanced : Un arbre est dit équilibré si le poids du sous arbre gauche a ou moins de différence avec le poids du sous arbre droit Arbre de recherche Lors de l insertion d une valeur on va choisir où l insérer pour avoir une recherche plus efficace, si la valeur ajoutée est inférieure à son père on va l ajouter à gauche sinon on va l ajouter à droite. AVL (un AVL est équilibré) de minimale : Hauteur de l AVL Nombre de sommets + + = + + = 7 7+ + = 5 6 7 5 Exemple : Création d un AVL avec les entrées suivantes : 9,,,,, 8,, 6, 5,, 7 9 9 9 On effectue donc une rotation : Problème car l arbre devient déséquilibré 9 9 9 On effectue alors une première rotation pour remonter, puis une seconde pour rééquilibrer l arbre : 9 9
On ne rencontre pas de problème jusqu à 5 : Qu on résout par une rotation : 9 9 8 6 6 5 8 5 Et de la même manière jusqu à la dernière insertion : 6 9 5 8 7 Si on veut retirer tous les nœuds de l arbre précédent sans avoir à le rééquilibrer on devra suivre l ordre de suppression suivant :,, 7,,, 5, 8,,, 9, 6 Implémentation java public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> implements Iterable<AnyType> { // The tree root private BinaryNode<AnyType> root; * Construct the tree. public BinarySearchTree( ) { root = null; /////////////// isempty * Test if the tree is logically empty. * @return true if empty, false otherwise. public boolean isempty( ) { return root==null; /////////////// makeempty * Make the tree logically empty. public void makeempty( ) {
root=null; /////////////// contains * Find an item in the tree. * @param x the item to search for. * @return true if not found. public boolean contains( AnyType x ) { return contains(x, root); * Internal method to find an item in a subtree. * @param x is item to search for. * @return node containing the matched item. private boolean contains( AnyType x, BinaryNode<AnyType> t ) { if(t == null) return false; if(t.element.equals(x)) return true; return contains(x, x.compareto(t.element)<? t.left : t.right); /////////////// insert * Insert into the tree; duplicates are ignored. * @param x the item to insert. public void insert( AnyType x ) { root = insert(x, root); * Internal method to insert into a subtree. * @param x the item to insert. * @return the new root of the subtree. private BinaryNode<AnyType> insert( AnyType x, BinaryNode<AnyType> t ) { if(t == null) t = new BinaryNode<AnyType>(x); else{ if(t.element.compareto(x)<) t.right = insert(x, t.right); else if (t.element.compareto(x)>) t.left = insert(x, t.left); /////////////// findmin
* Find the smallest item in the tree. * @return smallest item or null if empty. public AnyType findmin( ) throws EmptyTreeException { if(isempty()) throw new EmptyTreeException(); return findmin(root).element; * Internal method to find the smallest item in a subtree. * @return node containing the smallest item. private BinaryNode<AnyType> findmin( BinaryNode<AnyType> t ) { if(t.left==null) return findmin(t.left); /////////////// findmax * Find the largest item in the tree. * @return the largest item of null if empty. public AnyType findmax( ) throws EmptyTreeException { if(isempty()) throw new EmptyTreeException(); return findmax(root).element; * Internal method to find the largest item in a subtree. * @return node containing the largest item. private BinaryNode<AnyType> findmax( BinaryNode<AnyType> t ) { if(t.right==null) return findmax(t.right); /////////////// remove * Remove from the tree. Nothing is done if x is not found. * @param x the item to remove. public void remove( AnyType x ) { if(contains(x, root)) root = remove(x, root);
* Internal method to remove from a subtree. * @param x the item to remove. * @return the new root of the subtree. private BinaryNode<AnyType> remove( AnyType x, BinaryNode<AnyType> t ) { if(t==null) return null; int c = x.compareto(t.element); if(c>) t.right=remove(x,t.right); else if(c<) t.left=remove(x, t.left); else if(t.left==null) return t.right; else if(t.right==null) return t.left; else t.left = removemax(t.left, t); private BinaryNode removemax(binarynode t, BinaryNode n){ if(t.right==null){ n.element=t.element; return t.left; t.right=removemax(t.right, n); /////////////// removelessthan * Remove from the tree all the elements * less than min * @param min the minimum value left in the tree public void removelessthan(anytype min) { root = removelessthan(root, min); private BinaryNode<AnyType> removelessthan(binarynode<anytype> t, AnyType min) { if(t==null) return null; if(min.compareto(t.element)>) return removelessthan(t.right, min); t.left=removelessthan(t.left, min);
//////////////////////////////////////////////////// // Convenience method to print a tree //////////////////////////////////////////////////// public void display() { display(root,"",""); private void display(binarynode<anytype> t, String r, String p) { if ( t == null ) { System.out.println(r); else { String rs = t.element.tostring(); System.out.println(r + rs); if ( t.left!= null t.right!= null ) { String rr = p + ' ' + makestring('_',rs.length()) + ' '; display(t.right,rr, p + ' ' + makestring(' ',rs.length() + )); System.out.println(p + ' '); display(t.left,rr, p + makestring(' ',rs.length() + )); private String makestring(char c, int k) { String s = ""; for ( int i = ; i < k; i++ ) { s += c; return s; //////////////////////////////////////////////////// // Inner class BinaryNode<AnyType> //////////////////////////////////////////////////// // Basic node stored in unbalanced binary search trees private static class BinaryNode<AnyType> { // Constructors BinaryNode( AnyType theelement ) { this( theelement, null, null ); BinaryNode( AnyType theelement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt ) { element = theelement; left = lt; right = rt; AnyType element; // The data in the node BinaryNode<AnyType> left; // Left child BinaryNode<AnyType> right; // Right child