{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
Considérons l'arbre binaire suivant:\n",
"\n",
"
On représente l'arbre avec le tuple :
\n", "\n", "arbre = ('1', '2', '3', '4', '5', None, '6', None, None, '7', '8', None, None, '9', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Chaque noeud se repère par son indice $n$ dans la liste,\n", "* son fils gauche se trouvant alors à l’indice : $2n+ 1$ \n", "* son fils droit à l’indice : $2n+ 2$.\n", "\n", "➄ est à l’indice 4, son fils gauche se trouve alors à l’indice 9 et son fils droit à l’indice 10.\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# on utilisera ces 2 arbres pour les tests\n", "arbre1 = ('1', '2', '3', '4', '5', None, '6', None, None, '7', '8', None, None, '9', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None)\n", "arbre2=()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ci-dessous le programme qui permet d'afficher un arbre :
\n", "\n", " il nécessite d'avoir réussi la fonction hauteur " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import networkx as nx\n", "import matplotlib.pyplot as plt\n", "\n", "def repr_graph(arbre, size=(8,8), null_node=False):\n", " \"\"\"\n", " size : tuple de 2 entiers. Si size est int -> (size, size)\n", " null_node : si True, trace les liaisons vers les sous-arbres vides\n", " \"\"\"\n", " def parkour(arbre, noeuds, branches, labels, positions, profondeur, pos_courante, pos_parent, null_node):\n", " if arbre !=():\n", " noeuds[0].append(pos_courante)\n", " positions[pos_courante] = (pos_courante, profondeur)\n", " profondeur -= 1\n", " labels[pos_courante] = str(arbre[0])\n", " branches[0].append((pos_courante, pos_parent))\n", " pos_gauche = pos_courante - 2**profondeur\n", " parkour(arbre[1], noeuds, branches, labels, positions, profondeur, pos_gauche, pos_courante, null_node)\n", " pos_droit = pos_courante + 2**profondeur\n", " parkour(arbre[2], noeuds, branches, labels, positions, profondeur, pos_droit, pos_courante, null_node)\n", " elif null_node:\n", " noeuds[1].append(pos_courante)\n", " positions[pos_courante] = (pos_courante, profondeur)\n", " branches[1].append((pos_courante, pos_parent))\n", " \n", " \n", " if arbre ==():\n", " return\n", " \n", " branches = [[]]\n", " profondeur = hauteur(arbre)\n", " pos_courante = 2**profondeur\n", " noeuds = [[pos_courante]]\n", " positions = {pos_courante: (pos_courante, profondeur)} \n", " labels = {pos_courante: str(arbre[0])}\n", " \n", " if null_node:\n", " branches.append([])\n", " noeuds.append([])\n", " \n", " profondeur -= 1\n", " parkour(arbre[1], noeuds, branches, labels, positions, profondeur, pos_courante - 2**profondeur, pos_courante, null_node)\n", " parkour(arbre[2], noeuds, branches, labels, positions, profondeur, pos_courante + 2**profondeur, pos_courante, null_node) \n", "\n", " mon_arbre = nx.Graph()\n", " \n", " if type(size) == int:\n", " size = (size, size) \n", " plt.figure(figsize=size)\n", " \n", " nx.draw_networkx_nodes(mon_arbre, positions, nodelist=noeuds[0], node_color=\"white\", node_size=550, edgecolors=\"blue\")\n", " nx.draw_networkx_edges(mon_arbre, positions, edgelist=branches[0], edge_color=\"black\", width=2)\n", " nx.draw_networkx_labels(mon_arbre, positions, labels)\n", "\n", " if null_node:\n", " nx.draw_networkx_nodes(mon_arbre, positions, nodelist=noeuds[1], node_color=\"white\", node_size=50, edgecolors=\"grey\")\n", " nx.draw_networkx_edges(mon_arbre, positions, edgelist=branches[1], edge_color=\"grey\", width=1)\n", "\n", " ax = plt.gca()\n", " ax.margins(0.1)\n", " plt.axis(\"off\")\n", " plt.show()\n", " plt.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "repr_graph(arbre3,(4,3),False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "