Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Dark Perk Ergo e Sym provati tra wireless, software via browser e peso ridotto
Dark Perk Ergo e Sym provati tra wireless, software via browser e peso ridotto
be quiet! debutta nel settore mouse da gaming con Dark Perk Ergo e Dark Perk Sym: due modelli gemelli per specifiche, con polling rate di 8.000 Hz anche in wireless, sensore PixArt PAW3950 da 32.000 DPI e autonomia dichiarata fino a 110 ore. Nel test, a 8.000 Hz si arriva a circa 30 ore reali, con ricarica completa in un'ora e mezza
DJI RS 5: stabilizzazione e tracking intelligente per ogni videomaker
DJI RS 5: stabilizzazione e tracking intelligente per ogni videomaker
Analizziamo nel dettaglio DJI RS 5, l'ultimo arrivato della famiglia Ronin progettato per videomaker solisti e piccoli studi. Tra tracciamento intelligente migliorato e ricarica ultra rapida, scopriamo come questo gimbal eleva la qualità delle produzioni.
AMD Ryzen 7 9850X3D: Zen 5, 3D V-Cache e frequenze al top per il gaming
AMD Ryzen 7 9850X3D: Zen 5, 3D V-Cache e frequenze al top per il gaming
AMD Ryzen 7 9850X3D è la nuova CPU gaming di riferimento grazie alla 3D V-Cache di seconda generazione e frequenze fino a 5,6 GHz. Nei test offre prestazioni superiori a 9800X3D e 7800X3D, confermando la leadership AMD nel gaming su PC.
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 26-11-2012, 16:17   #1
diegoves
Junior Member
 
Iscritto dal: Oct 2009
Messaggi: 18
Esercizio su alberi binari triangolari

Salve a tutti! Ho appena fatto un compitino di dati e algoritmi, con questo esercizio all'interno:
"Scrivere una funzione ricorsiva che, passato come parametro un albero binario T, verifichi se è triangolare. Usare solo variabili locali d'appoggio, no array o altre strutture. Non si può modificare l'albero. L'interfaccia albero binario ha come metodi: root(), isEmpty(), hasLeft(), left(), hasRight(), right()" (quindi niente riguardante l'altezza dell'albero, il numero di nodi ecc ecc).
Come fare?
Cercando un po' su internet non ho trovato una effettiva definizione di albero triangolare, ma comunque è semplice: è un albero binario con tutti i livelli completi, quindi i suoi nodi sono in numero una potenza di 2 meno 1, e l'ultimo livello L dell'albero contiene 2^L nodi esterni.



Essendo ricorsiva ho pensato di visitare l'albero, e conteggiare un "+1" se la ricorsione scende a sinistra, e "-1" se scende a destra, se finita la ricorsione il conteggio è a 0 allora risulta triangolare, cioè in questa maniera:



ma sfortunatamente non basta, perché in questo caso verifica semplicemente se i nodi aventi figli ne hanno esattamente 2...!! Ovvero così:



Qualche consiglio su come farlo?
diegoves è offline   Rispondi citando il messaggio o parte di esso
Old 01-12-2012, 09:13   #2
diegoves
Junior Member
 
Iscritto dal: Oct 2009
Messaggi: 18
Nessuno sa darmi una mano?
diegoves è offline   Rispondi citando il messaggio o parte di esso
Old 01-12-2012, 14:38   #3
__ZERO_UNO__
Member
 
L'Avatar di __ZERO_UNO__
 
Iscritto dal: Jul 2009
Città: Milano
Messaggi: 270
Codice:
def triangle_h(T, d)
 if empty?(T) return 0;
 if (has_children?(T)) d += 1
 return 1 + triangle_h(left(T)) + triangle_h(right(T))
end

def triangle?(T, d)
 return triangle_h(T, d) == pow(2, d + 1) - 1
end


depth = 0
verdict = triangle?(T, depth)

if (verdict)
 print("It's a fantastic triangle tree!");
else
 print("It's a common tree :(");
Forse funziona.
La funzione restituisce il numero di nodi e calcola la massima profondità(il numero di archi dalla radice ad una foglia).
__________________

AMD PII x4 955 BE | Sapphire HD4850 Vapor-X 1 GB | Samsung SpinPoint F1 500GB | Samsung EcoGreen F4 2TB
Gigabyte GA-MA790FXT-UD5P | Fractal Design Define R3 USB3.0 Titanium Grey | CORSAIR 650W CMPSU-650TX
Noctua U12P SE2 | 2 x 2GB Kingston 1333 MHz | Samsung SyncMaster P2450 | Samsung SyncMaster T200

Ultima modifica di __ZERO_UNO__ : 01-12-2012 alle 14:43.
__ZERO_UNO__ è offline   Rispondi citando il messaggio o parte di esso
Old 02-12-2012, 11:18   #4
Vincenzo1968
Bannato
 
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
Edit

Ultima modifica di Vincenzo1968 : 02-12-2012 alle 11:23. Motivo: codice errato
Vincenzo1968 è offline   Rispondi citando il messaggio o parte di esso
Old 02-12-2012, 13:50   #5
Vincenzo1968
Bannato
 
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
La mia soluzione:

Codice:
int isTriangular(Tree *head)
{
    int leftHeight = -1;
    int rightHeight = -1;

	if( !head )
		return 0;

	if( !head->left && !head->right )
		return 1;

	if( !head->left || !head->right )
		return -1;

	leftHeight = isTriangular(head->left);
	rightHeight = isTriangular(head->right);

	if( leftHeight < 0 || rightHeight < 0 || leftHeight != rightHeight )
		return -1;

	return leftHeight + 1;
}
La funzione restituisce -1 se l'albero non è triangolare; altrimenti restituisce l'altezza.

Questo è il programma di prova:

Codice:
#include <stdio.h>
#include <malloc.h>

#define MAX_STACK 512

//int MemoriaAllocata = 0;
//int MemoriaDeallocata = 0;

typedef struct tagTree
{
	int data;
	struct tagTree *left;
	struct tagTree *right;
} Tree;

Tree *NewNode(int info);
void FreeTree(Tree *head);

int IsTriangular(Tree *head);

Tree *NewNode(int info)
{
	Tree *r;

	r = (Tree *) malloc(sizeof(Tree));

	if( !r )
	{
		printf("Memoria insufficiente.\n");
		return NULL;
	}

	//MemoriaAllocata += sizeof(Tree);

	r->data = info;
	r->left = NULL;
	r->right = NULL;

	return r;
}

void FreeTree(Tree *head)
{
	Tree *temp1, *temp2;

	Tree *stack[MAX_STACK];
	int top;

	top = 0;
 
	if ( head == NULL )
		return;

	temp1 = temp2 = head;

	while ( temp1 != NULL )
	{
		for(; temp1->left != NULL; temp1 = temp1->left)
			stack[top++] = temp1;

		while ( (temp1 != NULL) && (temp1->right == NULL || temp1->right == temp2) )
		{
			temp2 = temp1;
			free(temp2);
			//MemoriaDeallocata += sizeof(Tree);
			if ( top == 0 )
				return;
			temp1 = stack[--top];
		}

		stack[top++] = temp1;
		temp1 = temp1->right;
	}
}

int isTriangular(Tree *head)
{
    int leftHeight = -1;
    int rightHeight = -1;

	if( !head )
		return 0;

	if( !head->left && !head->right )
		return 1;

	if( !head->left || !head->right )
		return -1;

	leftHeight = isTriangular(head->left);
	rightHeight = isTriangular(head->right);

	if( leftHeight < 0 || rightHeight < 0 || leftHeight != rightHeight )
		return -1;

	return leftHeight + 1;
}

int main()
{
	Tree *pTree1 = NULL;
	Tree *pTree2 = NULL;

	pTree1 = NewNode(1);
	pTree1->left = NewNode(2);
	pTree1->right = NewNode(3);
	pTree1->left->left = NewNode(4);
	pTree1->left->right = NewNode(5);
	pTree1->right->left = NewNode(6);
	pTree1->right->right = NewNode(7);
	pTree1->left->left->left = NewNode(8);
	pTree1->left->left->right = NewNode(9);
	pTree1->left->right->left = NewNode(10);
	pTree1->left->right->right = NewNode(11);
	pTree1->right->left->left = NewNode(12);
	pTree1->right->left->right = NewNode(13);
	pTree1->right->right->left = NewNode(14);
	pTree1->right->right->right = NewNode(15);
	

	pTree2 = NewNode(1);
	pTree2->left = NewNode(2);
	pTree2->right = NewNode(3);
	pTree2->left->left = NewNode(4);
	pTree2->left->right = NewNode(5);
	pTree2->right->left = NewNode(6);
	pTree2->right->right = NewNode(7);
	pTree2->left->left->left = NewNode(8);
	pTree2->left->left->right = NewNode(9);
	pTree2->left->right->left = NewNode(10);
	pTree2->left->right->right = NewNode(11);


	if ( isTriangular(pTree1) != -1 )
		printf("\nL'albero 1 e' triangolare.\n");
	else
		printf("\nL'albero 1 non e' triangolare\n");

	if ( isTriangular(pTree2) != -1 )
		printf("\nL'albero 2 e' triangolare.\n");
	else
		printf("\nL'albero 2 non e' triangolare\n");

	FreeTree(pTree1);
	FreeTree(pTree2);

	//printf("\nMemoria allocata   : %d\nMemoria deallocata : %d\n\n", MemoriaAllocata, MemoriaDeallocata);

	return 0;
}

Ultima modifica di Vincenzo1968 : 02-12-2012 alle 14:03. Motivo: ottimizzato codice
Vincenzo1968 è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Dark Perk Ergo e Sym provati tra wireless, software via browser e peso ridotto Dark Perk Ergo e Sym provati tra wireless, softw...
DJI RS 5: stabilizzazione e tracking intelligente per ogni videomaker DJI RS 5: stabilizzazione e tracking intelligent...
AMD Ryzen 7 9850X3D: Zen 5, 3D V-Cache e frequenze al top per il gaming AMD Ryzen 7 9850X3D: Zen 5, 3D V-Cache e frequen...
Le soluzioni FSP per il 2026: potenza e IA al centro Le soluzioni FSP per il 2026: potenza e IA al ce...
AWS annuncia European Sovereign Cloud, il cloud sovrano per convincere l'Europa AWS annuncia European Sovereign Cloud, il cloud ...
HP e lavoro ibrido: le nuove cuffie Poly...
MSI sta lavorando a un dissipatore ottim...
27 offerte Amazon, le prime 5 in elenco ...
Il telescopio spaziale James Webb ha cre...
Il reboot di Painkiller tenta il rilanci...
7 smartphone in super offerta su Amazon,...
Ring abbassa i prezzi su Amazon: videoci...
Blink taglia i prezzi su Amazon: Mini 2K...
Claude al centro di Apple: ecco come Cup...
Pornhub e altri siti porno si ribellano ...
La TV non è smart? Amazon la trasforma c...
Oltre 200 siti di news hanno limitato l'...
Gennaio si chiude positivamente per il m...
Caos in Ubisoft: licenziato un dipendent...
BMW ed Encory avviano il riciclo diretto...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 15:03.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v