I'm taking this term a course in C, and I have got an assignment which deals namely with pointers - building an m-ary tree.
Some description: We receive at the command line arguments: the file name of a text and two numbers which represent the keys of some two vertices in the graph (I will explain later what we have to do with these two vertices).
The first line of the text is actually the total number of the vertices of the graph, the next line could for example include numbers like "2 5" which implies that vertices 2 and 5 are children of vertex with key 0, the next line may include "6 0" which says that vertex with key of 1 is the father of 6 and 0 vertices, and so on...
If some line contains only '-' then it's a leaf. This part actually deals with parsing and defining the suitable structure for vertex, and I have already done that (but I have to take care of corner cases later on...).
Now, my problem begins - we have to find the number of edges in the tree in Big O of 1 time complexity; find the root in Big O of n (where n is the number of vertices) time complexity; find the simple shortest path between the two vertices (I think it can also be done with BFS) we received at the command line in Big O of n squared time complexity; find the minimal and maximum heights of the tree; find the diameter of the tree in Big O of n squared time complexity.
To implement it, we have to use BFS and we can use their implementation of queue.
Here is my vertex struct:
typedef struct Vertex {
size_t key;
unsigned int amountOfNeighbors; // The current amount of neighbors
unsigned int capacity; // The capacity of the neighbors (It's updating during run-time)
struct Vertex* parent;
struct Vertex** neighbors; // The possible parent and children of a vertex
} Vertex;
I have went through the pseudo-code of BFS and it uses the idea of the next and previous vertices of a vertex - it's a concept which is not used in my implementation and I really don't know how I can mingle it with my code properly...
Secondly, I have no idea how I can calculate the number of edges in the tree in O(1) - it seems impossible - it requires me to go through all the vertices at least once which is O(n)...
So I actually need help to adjust the BFS algorithm to my needs, and find a way to calculate the number of edges in constant time complexity.
Thanks in advance!