Spaces:
Running
Running
Upload code.txt
Browse files
code.txt
CHANGED
@@ -625,12 +625,17 @@ r_7.insert_droit(13)
|
|
625 |
r_13 = r_7.get_droit()
|
626 |
r_13.insert_gauche(9)
|
627 |
c = 0
|
628 |
-
if
|
629 |
print("Test 1 : OK")
|
630 |
c += 1
|
631 |
else :
|
632 |
print("Test 1 : échec")
|
633 |
-
if
|
|
|
|
|
|
|
|
|
|
|
634 |
print("OK")
|
635 |
else :
|
636 |
print("KO")
|
@@ -1838,70 +1843,6 @@ if c == 2 :
|
|
1838 |
else :
|
1839 |
print("KO")
|
1840 |
;;;
|
1841 |
-
hauteur d'un arbre (N2 T)
|
1842 |
-
;;;
|
1843 |
-
Écrire une fonction hauteur qui prend en paramètre une instance de la classe arbre T et renvoie la hauteur de l'arbre binaire T.
|
1844 |
-
La classe Arbre possède 3 méthodes :
|
1845 |
-
- get_gauche() renvoie l'arbre gauche
|
1846 |
-
- get_droit() renvoie l'arbre droit
|
1847 |
-
- get_valeur() renvoie la valeur du noeud
|
1848 |
-
;;;
|
1849 |
-
class ArbreBinaire:
|
1850 |
-
def __init__(self, valeur):
|
1851 |
-
self.valeur = valeur
|
1852 |
-
self.enfant_gauche = None
|
1853 |
-
self.enfant_droit = None
|
1854 |
-
def insert_gauche(self, valeur):
|
1855 |
-
if self.enfant_gauche == None:
|
1856 |
-
self.enfant_gauche = ArbreBinaire(valeur)
|
1857 |
-
else:
|
1858 |
-
new_node = ArbreBinaire(valeur)
|
1859 |
-
new_node.enfant_gauche = self.enfant_gauche
|
1860 |
-
self.enfant_gauche = new_node
|
1861 |
-
def insert_droit(self, valeur):
|
1862 |
-
if self.enfant_droit == None:
|
1863 |
-
self.enfant_droit = ArbreBinaire(valeur)
|
1864 |
-
else:
|
1865 |
-
new_node = ArbreBinaire(valeur)
|
1866 |
-
new_node.enfant_droit = self.enfant_droit
|
1867 |
-
self.enfant_droit = new_node
|
1868 |
-
def get_valeur(self):
|
1869 |
-
return self.valeur
|
1870 |
-
def get_gauche(self):
|
1871 |
-
return self.enfant_gauche
|
1872 |
-
def get_droit(self):
|
1873 |
-
return self.enfant_droit
|
1874 |
-
racine = ArbreBinaire('A')
|
1875 |
-
racine.insert_gauche('B')
|
1876 |
-
racine.insert_droit('F')
|
1877 |
-
|
1878 |
-
b_node = racine.get_gauche()
|
1879 |
-
b_node.insert_gauche('C')
|
1880 |
-
b_node.insert_droit('D')
|
1881 |
-
|
1882 |
-
f_node = racine.get_droit()
|
1883 |
-
f_node.insert_gauche('G')
|
1884 |
-
f_node.insert_droit('H')
|
1885 |
-
|
1886 |
-
c_node = b_node.get_gauche()
|
1887 |
-
c_node.insert_droit('E')
|
1888 |
-
|
1889 |
-
g_node = f_node.get_gauche()
|
1890 |
-
g_node.insert_gauche('I')
|
1891 |
-
|
1892 |
-
h_node = f_node.get_droit()
|
1893 |
-
h_node.insert_droit('J')
|
1894 |
-
c = 0
|
1895 |
-
if hauteur(racine) == 4:
|
1896 |
-
print("Test 1 : OK")
|
1897 |
-
c += 1
|
1898 |
-
else :
|
1899 |
-
print("Test 1 : échec")
|
1900 |
-
if c == 1 :
|
1901 |
-
print("OK")
|
1902 |
-
else :
|
1903 |
-
print("KO")
|
1904 |
-
;;;
|
1905 |
hauteur d'un arbre binaire (N2 T)
|
1906 |
;;;
|
1907 |
Écrire une fonction hauteur qui prend en paramètre une instance de la classe arbre T et renvoie la hauteur de l'arbre binaire T.
|
|
|
625 |
r_13 = r_7.get_droit()
|
626 |
r_13.insert_gauche(9)
|
627 |
c = 0
|
628 |
+
if recherche_abr(racine_r,4):
|
629 |
print("Test 1 : OK")
|
630 |
c += 1
|
631 |
else :
|
632 |
print("Test 1 : échec")
|
633 |
+
if not recherche_abr(racine_r,42):
|
634 |
+
print("Test 2 : OK")
|
635 |
+
c += 1
|
636 |
+
else :
|
637 |
+
print("Test 2 : échec")
|
638 |
+
if c == 2 :
|
639 |
print("OK")
|
640 |
else :
|
641 |
print("KO")
|
|
|
1843 |
else :
|
1844 |
print("KO")
|
1845 |
;;;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1846 |
hauteur d'un arbre binaire (N2 T)
|
1847 |
;;;
|
1848 |
Écrire une fonction hauteur qui prend en paramètre une instance de la classe arbre T et renvoie la hauteur de l'arbre binaire T.
|