Arbres en Prolog Un arbre binaire est une
Arbres en Prolog � Un arbre binaire est une structure pouvant contenir des données. Chaque élément de l'arbre contient une donnée et a au plus un 'parent' et deux 'enfants'. / 1 2 3 / / 4 5 6 7 CSI 2520
Arbres en Prolog On peut representer les arbres avec une structure t(elem, gauche, droit) ou 'elem' est la valeur de la racine, et gauche et droit sont les sous-arbres à gauche/droite de la racine. Un arbre vide sera represente par 'nul'. Ainsi, un arbre à un seul élément 1: t(1, nul): 1 / 2 3 t(1, t(2, nul), t(3, nul)). CSI 2520
Un arbre binaire CSI 2520
Parcours inordre print. Info(nul). print. Info(t(Root. Info, Left. Subtr, Right. Subtr)) : print. Info(Left. Subtr), write(Root. Info), write(' '), print. Info(Right. Subtr). ? - print. Info(t(73, t(31, t(5, nul), t(101, t(83, nul, t(97, nul)), nul))). 5 31 73 83 97 101 Yes CSI 2520
Recherche dans un arbre search(Info, t(Info, _, _)). search(Info, t(Root. Info, Left, _Right)) : precedes(Info, Root. Info), search(Info, Left). search(Info, t(Root. Info, _Left, Right)) : precedes(Root. Info, Info), search(Info, Right). precedes(Info 1, Info 2) : Info 1 < Info 2. CSI 2520
Insertion dans un arbre insert(Info, nul, nul)). insert(Info, t(Root. Info, Left, Right), t(Root. Info, Left. Plus, Right)) : precedes(Info, Root. Info), insert(Info, Left. Plus). insert(Info, t(Root. Info, Left, Right), t(Root. Info, Left, Right. Plus)) : precedes(Root. Info, Info), insert(Info, Right. Plus). CSI 2520
Retrait à la racine d’un arbre delete(Info, t(Info, nul, Right). delete(Info, t(Info, Left, nul), Left). delete(Info, t(Info, Left, Right), t(New. Root, New. Left, Right)) : remove. Max(Left, New. Root). % remove. Max(Tree, New. Tree, Max) CSI 2520
Retrait dans un arbre delete(Info, t(Root. Info, Left, Right), t(Root. Info, Left. Smaller, Right)) : precedes(Info, Root. Info), delete(Info, Left. Smaller). delete(Info, t(Root. Info, Left, Right), t(Root. Info, Left, Right. Smaller)) : precedes(Root. Info, Info), delete(Info, Right. Smaller). CSI 2520
Retrait du max élément remove. Max(t(Max, Left, nul), Left, Max). remove. Max(t(Root, Left, Right), t(Root, Left, Right. Smaller), Max) : remove. Max(Right, Right. Smaller, Max). CSI 2520
- Slides: 9