====== Binary Search Tree e Heap ====== Nella lezione del 3/12, abbiamo visto i [[wp>Binary_search_tree|BST]], mentre in quella di lunedi' vedremo l'[[wp>Binary_heap|Heap]]. A partire da queste strutture dati, potremo costruire l'algoritmo di ordinamento noto come [[wp>Heapsort]]. {{teaching:info3:heap.tgz|Qui}} trovate vari esempi di Heap e Heapsort in Python. === Binary Search Tree e Heap === In today's lecture, we have seen the [[wp>Binary_search_tree|BST]]. We will see the [[wp>Binary_heap|Heap]] data structure next time, as well as the [[wp>Heapsort]] algorithm. {{teaching:info3:heap.tgz|Here}} you can find the Heap and Heapsort examples in Python. == Binary Search Tree Implementation == #!/usr/bin/python class BinNode(object): def __init__(self,parent,key,content=None): self.data = content self.key = key self.left = None self.right = None self.parent= parent def __getitem__(self,side): if side in [ 0, 'left', 'l' ] : return self.left elif side in [ 1, 'right', 'r' ] : return self.right raise KeyError, side def __setitem__(self,side,item): if not isinstance(item,BinNode) : raise TypeError, item if side in [ 0, 'left', 'l' ] : self.left = item elif side in [ 1, 'right', 'r' ] : self.right = item else : raise KeyError, side def isLeaf(self): return not (self.left or self.right) def search(self,key): if self.key==key : return self if key