Destroying a Planetary System: Analyzing Solid's Destruction Function

In summary, the conversation was about writing a function for the destruction of the planetary system "solid". The destruction would also involve deleting the asteroids with a gap smaller than the specified "gap" and converting the asteroids with a larger gap into free-floating planets. The total cost of this operation should be $O(n)$, where $n$ is the maximum between the elements of the tree of free-floating planets and the tree of asteroids. The planetary system would also be deleted from the list of planetary systems it belonged to. The conversation also discussed the correct way to delete the nodes and handle the pointers in the code.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

I have to write a function for the destruction of the planetary system "solid".
With the destruction of the planetary system, the asteroids where the gap between this one and the object, is smaller that "gap" will also be destructed. (so, they have to be deleted) The asteroids for which the gap is greater are converted to free-floating planets so they have to be inserted into the tree of free-floating planets. The total cost of the transport of all the asteroids that are converted into free-floating planets in the tree of free-floating planets should be $O(n)$, where $n$ is the maximal between the elements of the tree of free-floating planets of the star system to which the object belongs and the elements of the tree of asteroids of the planetary system of which the object has identifier "solid". The field "gap" of the new tree should have the value $0$. The destructed planetary system should be deleted from the list of planetary systems of the star system to which it belonged.
(The tree of free-floating planets is not a binary search tree)

I have done the following:

Code:
int destruction(int solid, int gap){
	int ffplanpos=-1;
	int i, j=-1;
	int sum=0;
	asteroid_t *planf = calloc(1, sizeof(asteroid_t));
	asteroid_t *f=NULL;
	plansys_t *p=NULL;
	asteroid_t *next=NULL;
	starsy_t *fflp=NULL;
	starsy_t *prev_fflp=NULL;
	
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p != NULL && p->solid != solid){
			p=p->next;
		}
		if(p != NULL && p->solid == solid){
			j=i;
		}
	}
	
	if(j == -1){
		return -1;
	}
	
	p=StarS[j].plasy;
	
	
	if(p == NULL){
		printf("The planetary system with identifier %d couldn't be found\n", solid);
		return -1;
	}
	
	if(p->asteroids == NULL){
		printf("The planetary system doesn't contain any asteroids\n");
		return -1;
	}
	
	
	f=p->asteroids;
	
	while(f != NULL){
		if(gap<f->gap){
    	                next=f->RC;
    	                free(f);    
    	                f = next;
    	        }
    	        else{
    			next=f->LC;
    	                free(f);    
    	                f = next;
    	        }
	}
	p->asteroids = f;
	if (f != NULL) {
    	        f->PARENT=NULL;
	}

	fflp=StarS[j].ff;
	
	if(f != NULL){
	       planf->as=f->as;
	       planf->gap=0;
	       planf->RC=NULL;
	       planf->LC=NULL;
	       planf->PARENT=NULL;

		   if(fflp == NULL){
			   fflp = planf;
		   }
		   else{
			   while(fflp->RC != NULL){
				   prev_fflp=fflp;
				   fflp=fflp->RC;
			   } 
			   prev_fflp->RC=planf;
		   }
	}
	
	return 0;
	
}

Is it correct?? (Wondering)

Is the way I deleted the nodes correct?? (Wondering)
 
Technology news on Phys.org
  • #2
I had defined some pointers wrong...

Code:
int destruction(int solid, int gap){
	int ffplanpos=-1;
	int i, j=-1;
	int sum=0;
	asteroid_t *planf = calloc(1, sizeof(asteroid_t));
	asteroid_t *f=NULL;
	plansys_t *p=NULL;
	asteroid_t *next=NULL;
	asteroid_t *fflp=NULL;
	asteroid_t *prev_fflp=NULL;
	
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while (p != NULL && p->solid != solid){
			p=p->next;
		}
		if(p != NULL && p->solid == solid){
			j=i;
		}
	}
	
	if(j == -1){
		return -1;
	}
	
	p=StarS[j].plasy;
	
	
	if(p == NULL){
		printf("The planetary system with identifier %d couldn't be found\n", solid);
		return -1;
	}
	
	if(p->asteroids == NULL){
		printf("The planetary system doesn't contain any asteroids\n");
		return -1;
	}
	
	
	f=p->asteroids;
	
	while(f != NULL){
		if(gap<f->gap){
    	                next=f->RC;
    	                free(f);    
    	                f = next;
    	        }
    	        else{
    			next=f->LC;
    	                free(f);    
    	                f = next;
    	        }
	}
	p->asteroids = f;
	if (f != NULL) {
    	        f->PARENT=NULL;
	}

	fflp=StarS[j].ff;
	
	if(f != NULL){
	       planf->as=f->as;
	       planf->gap=0;
	       planf->RC=NULL;
	       planf->LC=NULL;
	       planf->PARENT=NULL;

		   if(fflp == NULL){
			   fflp = planf;
		   }
		   else{
			   while(fflp->RC != NULL){
				   prev_fflp=fflp;
				   fflp=fflp->RC;
			   } 
			   prev_fflp->RC=planf;
		   }
	}
	
	return 0;
	
}

Is the way I deleted the nodes correct?? (Wondering)
 
  • #3
Code:
	f=p->asteroids;
	
	[COLOR="#FF0000"]while(f != NULL){
		if(gap<f->gap){
    	                next=f->RC;
    	                free(f);    
    	                f = next;
    	        }
    	        else{
    			next=f->LC;
    	                free(f);    
    	                f = next;
    	        }
	}[/COLOR]
	p->asteroids = f;
	if (f != NULL) {
    	        f->PARENT=NULL;
	}

	fflp=StarS[j].ff;
	
	if(f != NULL){
	       planf->as=f->as;
	       planf->gap=0;
	       planf->RC=NULL;
	       planf->LC=NULL;
	       planf->PARENT=NULL;

		   if(fflp == NULL){
			   fflp = planf;
		   }
		   else{
			   while(fflp->RC != NULL){
				   prev_fflp=fflp;
				   fflp=fflp->RC;
			   } 
			   prev_fflp->RC=planf;
		   }
	}
	
	return 0;
	
}

With the red part do we not delete all the nodes, no matter if the gap between the asteroid and the object is smaller that "gap" or not?? (Wondering)

Should it maybe be as followed??

Code:
while(f != NULL){
		if(gap < f->gap){
    	                f=f->RC;
    	        }
    	        else{
    			next=f->LC;
    	                free(f);    
    	                f = next;
    	        }
	}

(Wondering)

At the else-statement when we delete the node [m]f[/m] and we set that [m]f[/m] points to [m]f->LC[/m], what happens with [m]f->RC[/m] ?? (Wondering)
 

1. How do you determine the destruction function for a planetary system?

The destruction function for a planetary system is determined by analyzing the physical properties of the system, such as the mass, composition, and orbital motion of the planets and other objects within the system. This information is then used to create a mathematical model that describes how these factors affect the stability of the system over time.

2. What factors contribute to the destruction of a planetary system?

The destruction of a planetary system can be caused by a variety of factors, including collisions between objects, gravitational interactions, and external forces such as radiation or tidal forces. The specific factors that contribute to the destruction of a particular system will depend on its unique characteristics and environment.

3. How can the destruction function be used to predict the fate of a planetary system?

By analyzing the destruction function of a planetary system, scientists can make predictions about its future stability and potential for destruction. This information can be used to better understand the evolution of planetary systems and the conditions necessary for their survival.

4. Can a planetary system be destroyed naturally, or does it require external forces?

While external forces can certainly contribute to the destruction of a planetary system, it is also possible for a system to be destroyed naturally through internal processes. For example, collisions between objects within the system or changes in the orbits of planets can lead to instability and eventual destruction.

5. What impact does the destruction of a planetary system have on its surrounding environment?

The destruction of a planetary system can have a significant impact on its surrounding environment, as it can release large amounts of energy and debris into space. This can affect neighboring systems, potentially leading to the formation of new planets or altering the orbits of existing objects. Additionally, the destruction of a planetary system can provide valuable insights into the processes that shape and influence the formation of planetary systems in the universe.

Similar threads

  • Programming and Computer Science
4
Replies
119
Views
15K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
4K
Replies
12
Views
2K
  • Sci-Fi Writing and World Building
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
Replies
1
Views
975
  • Astronomy and Astrophysics
Replies
2
Views
4K
  • Other Physics Topics
Replies
0
Views
737
  • STEM Academic Advising
Replies
10
Views
4K
Back
Top