TreeLib: Swap left and right
This commit is contained in:
		
							parent
							
								
									7cfb2c414e
								
							
						
					
					
						commit
						db5f0fd0c5
					
				
					 1 changed files with 36 additions and 2 deletions
				
			
		|  | @ -181,10 +181,44 @@ def create_node_before(some, arguments, but, i, dont, know, which, yet): | ||||||
| def swap(node, other): | def swap(node, other): | ||||||
| 	raise NotImplementedError("YAGNI (You aren't gonna need it).") | 	raise NotImplementedError("YAGNI (You aren't gonna need it).") | ||||||
| 
 | 
 | ||||||
|  | # Exception, kterou některé metody při špatném použití mohou házet | ||||||
|  | # Hlavní důvod je možnost informovat o selhání, aby se příslušný problém dal zobrazit na frontendu, | ||||||
|  | class TreeLibError(RuntimeError): | ||||||
|  | 	pass | ||||||
|  | 
 | ||||||
| def swap_pred(node): | def swap_pred(node): | ||||||
| 	pass | 	if node is None: | ||||||
|  | 		raise TreeLibError("Nelze přesunout None. Tohle by se nemělo stát.") | ||||||
|  | 	pred = safe_pred(node) | ||||||
|  | 	if pred is None: | ||||||
|  | 		raise TreeLibError("Nelze posunout vlevo, není tam žádný další uzel.") | ||||||
|  | 	pre_pred = safe_pred(pred) | ||||||
|  | 	succ = node.succ | ||||||
|  | 
 | ||||||
|  | 	if pre_pred is not None: | ||||||
|  | 		pre_pred.succ = node | ||||||
|  | 		pre_pred.save() | ||||||
|  | 	node.succ = pred | ||||||
|  | 	node.save() | ||||||
|  | 	pred.succ = succ | ||||||
|  | 	pred.save() | ||||||
|  | 
 | ||||||
| def swap_succ(node): | def swap_succ(node): | ||||||
| 	pass | 	if node is None: | ||||||
|  | 		raise TreeLibError("Nelze přesunout None. Tohle by se nemělo stát.") | ||||||
|  | 	succ = node.succ | ||||||
|  | 	if succ is None: | ||||||
|  | 		raise TreeLibError("Nelze posunout vpravo, není tam žádný další uzel") | ||||||
|  | 	pred = safe_pred(node) | ||||||
|  | 	post_succ = succ.succ | ||||||
|  | 
 | ||||||
|  | 	if pred is not None: | ||||||
|  | 		pred.succ = succ | ||||||
|  | 		pred.save() | ||||||
|  | 	succ.succ = node | ||||||
|  | 	succ.save() | ||||||
|  | 	node.succ = post_succ | ||||||
|  | 	node.save() | ||||||
| 
 | 
 | ||||||
| # Rotace stromu | # Rotace stromu | ||||||
| # Dokumentace viz wiki: | # Dokumentace viz wiki: | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 Pavel 'LEdoian' Turinsky
						Pavel 'LEdoian' Turinsky