LaDissertation.com - Dissertations, fiches de lectures, exemples du BAC
Recherche

Visualisation d'arbre binaire avec python et tkinter

Compte rendu : Visualisation d'arbre binaire avec python et tkinter. Recherche parmi 298 000+ dissertations

Par   •  8 Novembre 2023  •  Compte rendu  •  317 Mots (2 Pages)  •  205 Vues

Page 1 sur 2

import tkinter as tk

class Node:

def __init__(self, value):

self.value = value

self.left = None

self.right = None

def draw_tree(node, x, y, x_dist):

if node:

# Drow current node

canvas.create_oval(x, y, x+30, y+30, fill='white')

canvas.create_text(x+15, y+15, text=node.value)

# drow child connecting with parent, only if present

if node.left:

x_left = x - x_dist

y_left = y + 50

canvas.create_line(x+15, y+30, x_left+15, y_left, arrow=tk.LAST)

draw_tree(node.left, x_left, y_left, x_dist/2)

if node.right:

x_right = x + x_dist

y_right = y + 50

canvas.create_line(x+15, y+30, x_right+15, y_right, arrow=tk.LAST)

draw_tree(node.right, x_right, y_right, x_dist/2)

#create the tkinter window

root = tk.Tk()

root.title('Visualisation de l\'arabe binegre')

#create a canva so that I drow the tree

canvas = tk.Canvas(root, width=800, height=600)

canvas.pack()

#init node

a = Node('a')

b = Node('b')

c = Node('c')

d = Node("d")

f = Node('f')

g = Node('g')

h = Node('h')

e = Node('e')

i = Node('i')

j = Node('j')

a.left = b

a.right = f

b.left = c

b.right = d

c.left = e

f.left = g

g.left = i

f.right = h

h.right = j

# Drow tree from a

draw_tree(a, 400, 50, 200)

root.mainloop()

...

Télécharger au format  txt (1.4 Kb)   pdf (34.4 Kb)   docx (7.7 Kb)  
Voir 1 page de plus »
Uniquement disponible sur LaDissertation.com