Skip to content

Commit 12be564

Browse files
authored
Merge pull request #3 from Gathros/master
Adding C to the tree traversal and verlet
2 parents 1f0b727 + 610566d commit 12be564

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

chapters/computational_physics/verlet.md

+69
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,72 @@ xlabel('Time (s)'); ylabel('Height (m)');
432432
Submitted by P. Mekhail
433433

434434
![Verlet LabVIEW](verlet_labview.png)
435+
436+
#### C
437+
438+
```
439+
#include <stdio.h>
440+
441+
// Simple function for velocity-verlet
442+
void verlet(double pos, double acc, double dt){
443+
444+
// Note that we are using a temp variable for the previous position
445+
double prev_pos, temp_pos, time;
446+
prev_pos = pos;
447+
time = 0;
448+
449+
while (pos > 0){
450+
time += dt;
451+
temp_pos = pos;
452+
pos = pos*2 - prev_pos + acc * dt * dt;
453+
prev_pos = temp_pos;
454+
}
455+
456+
printf("%f\n", time);
457+
458+
}
459+
460+
// Simple function for stormer-verlet
461+
void stormer_verlet(double pos, double acc, double dt){
462+
463+
double prev_pos, temp_pos, time, vel;
464+
prev_pos = pos;
465+
vel = 0;
466+
time = 0;
467+
while (pos > 0){
468+
time += dt;
469+
temp_pos = pos;
470+
pos = pos*2 - prev_pos + acc * dt * dt;
471+
prev_pos = temp_pos;
472+
473+
// The acceleration is constant, so the velocity is straightforward
474+
vel += acc*dt;
475+
}
476+
477+
printf("%f\n", time);
478+
479+
}
480+
481+
// Simple function for velocity-verlet
482+
void velocity_verlet(double pos, double acc, double dt){
483+
484+
double time, vel;
485+
vel = 0;
486+
time = 0;
487+
while (pos > 0){
488+
time += dt;
489+
pos += vel*dt + 0.5*acc * dt * dt;
490+
vel += acc*dt;
491+
}
492+
493+
printf("%f\n", time);
494+
495+
}
496+
497+
int main(){
498+
verlet(5.0, -10, 0.01);
499+
stormer_verlet(5.0, -10, 0.01);
500+
velocity_verlet(5.0, -10, 0.01);
501+
502+
}
503+
```

chapters/fundamental_algorithms/tree_traversal.md

+161
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,164 @@ def main():
327327

328328
main()
329329
```
330+
331+
### C
332+
333+
```C
334+
#include <stdio.h>
335+
#include <stdlib.h>
336+
#include <string.h>
337+
338+
typedef struct node{
339+
struct node *children; // I chose a pointer to create a dynamic array
340+
int children_num;
341+
int ID;
342+
} node;
343+
344+
// There is no stack or queue in c so I created my own
345+
typedef struct node_list{
346+
node n;
347+
struct node_list *last_list, *next_list;
348+
} node_list;
349+
350+
typedef struct node_points{
351+
node_list *start_point, *end_point;
352+
} node_points;
353+
354+
void push(node_points *np, node *n){
355+
// Adding node into a queue or a stack
356+
node_list *temp = (node_list*)malloc(sizeof(node_list));
357+
temp->n = *n;
358+
temp->last_list = temp->next_list = NULL;
359+
360+
if(!np->end_point){
361+
np->start_point = temp;
362+
}else{
363+
np->end_point->next_list = temp;
364+
temp->last_list = np->end_point;
365+
}
366+
np->end_point = temp;
367+
}
368+
369+
void stack_pop(node_points *np){
370+
// Removing the last node_list of the stack
371+
node_list *temp;
372+
temp = np->end_point;
373+
if(temp){
374+
np->end_point = temp->last_list;
375+
if(!np->end_point){
376+
np->start_point = NULL;
377+
}
378+
free(temp);
379+
}
380+
}
381+
382+
void queue_pop(node_points *np){
383+
// Removing the first node_list of the queue
384+
node_list *temp;
385+
temp = np->start_point;
386+
if(temp){
387+
np->start_point = temp->next_list;
388+
if(!np->start_point){
389+
np->end_point = NULL;
390+
}
391+
free(temp);
392+
}
393+
}
394+
395+
void create_tree(node *n, int num_row, int num_child) {
396+
n->ID = num_row;
397+
if(num_row == 0){
398+
return;
399+
}
400+
401+
// Creating children
402+
// Using malloc to make an array of nodes with size num_child
403+
n->children = (node *)malloc(num_child*sizeof(*n->children));
404+
// When an array is made into a pointer you can't get it's size later
405+
n->children_num = num_child;
406+
for(int i = 0; i < num_child; ++i){
407+
node child;
408+
create_tree(&child, num_row - 1, num_child);
409+
*(n->children + i) = child;
410+
}
411+
}
412+
413+
void DFS_recursive(node *n){
414+
printf("%d\n", n->ID);
415+
416+
// Checking if the node's children exist
417+
if(!n->children){
418+
return;
419+
}
420+
421+
for(int i = 0; i < n->children_num; ++i){
422+
DFS_recursive((n->children + i));
423+
}
424+
}
425+
426+
void DFS_stack(node *n){
427+
// Creating a stack and then setting its value to 0
428+
node_points stack;
429+
memset(&stack, 0, sizeof(node_points));
430+
push(&stack, n);
431+
node temp;
432+
433+
while(stack.start_point != NULL){
434+
temp = stack.end_point->n;
435+
printf("%d\n", temp.ID);
436+
stack_pop(&stack);
437+
for(int i=0; i < temp.children_num; ++i){
438+
// Checking if the node has any children
439+
if(!temp.children){
440+
break;
441+
}
442+
push(&stack, temp.children + i);
443+
}
444+
}
445+
}
446+
447+
void BFS_queue(node *n){
448+
// Creating a queue and then setting its value to 0
449+
node_points queue;
450+
memset(&queue, 0, sizeof(node_points));
451+
push(&queue, n);
452+
node temp;
453+
454+
while(queue.start_point != NULL){
455+
temp = queue.start_point->n;
456+
printf("%d\n", temp.ID);
457+
queue_pop(&queue);
458+
for(int i = 0; i < temp.children_num; ++i){
459+
// Checking if the node has any children
460+
if(!temp.children){
461+
break;
462+
}
463+
push(&queue, temp.children + i);
464+
}
465+
}
466+
}
467+
468+
void destroy_tree(node *n){
469+
// This function is for cleaning up all the nodes
470+
if(n->ID == 0){
471+
return;
472+
}
473+
474+
for(int i = 0; i < n->children_num; ++i){
475+
destroy_tree(n->children + i);
476+
}
477+
free(n->children);
478+
}
479+
480+
int main() {
481+
node root;
482+
create_tree(&root, 3, 3);
483+
//DFS_recursive(&root);
484+
//DFS_stack(&root);
485+
BFS_queue(&root);
486+
destroy_tree(&root);
487+
488+
return 0;
489+
}
490+
```

0 commit comments

Comments
 (0)