How to implement a tree-like data structure in rust? [duplicate] - pointers

This question already has answers here:
How do I express mutually recursive data structures in safe Rust?
(4 answers)
How to model complex recursive data structures (graphs)?
(1 answer)
Why can't I store a value and a reference to that value in the same struct?
(4 answers)
Closed 6 days ago.
I need to implement a tree-like data structure, in which each node possibly has 2 children, and each child node has a reference to its parent (except root node).
Here is a sketch:
strcut Node {
parent: Option< ??? >,
child_1: Option<Box<Node>>,
child_2: Option<Box<Node>>
}
As you can see I don't know what type should a parent field be. Can it be just a plain unsafe pointer? What is the proper way of implementing it?

Related

Method for recursive fill the ancestor properties of item

I dont know if this question is simple or not (or silly)
You have getItem methods to get an object using its Id (expressid) .
I want to get, for example, the ancestor properties ... and calculate properties (m2, m3. etc)
I can do it recursively calling to the ifc parent elements, and then again ., and again ...
Is there any other way ? Is there any dedicated function ?
This also means to learn about the ifc itself, but I dont know if I'm reinventing the wheel ?
Thanks
PD.
Somebody with enough reputation could create a ifc.js tag ....?
IFC.js just reflects the data within the IFC file. That means that the complexity / convolution of the data structures are not due to the library itself, but to the data structure of the IFC schema. Indeed, getting a bit familiar with the schema is a requirement to perform specific operations.
Just for your information, it's quite uncommon to compute the quantity data of elements yourself (surface, volume, etc). What the vast majority of BIM softwares do is read the explicit data of the IFC file (generally expressed as QSets).
It could be interesting to improve the ifc.js libraby to get a structured object of all properties for a construction element by its expressid.
A construction element is a leaf element under ifcRoot in class hierarchy.

Assign slice of pointers to slice of interface that they implement [duplicate]

This question already has answers here:
Type converting slices of interfaces
(9 answers)
Closed 10 months ago.
I need some clearance of golang behavior. Imagine we have an interface with some method, and we have a type that implements that method. If we assign pointer to type to variable defined as interface, golang allows us to do it.
But when we try to assign slice of pointers to type to the variable defined to contain slice of interfaces, golang panics...
Can anyone explain why?
Here is an example
as it came in here:
they do not have the same representation in memory.

Limits on number of used pointers in Queue implementation

This is a question from the last years exam in my Data Structures course..
So, a Queue (data structure) consisting of n elements is implemented using pointers. The question is to find the maximum and minimum number of pointers used in that data structure?
So, I don't really understand where to start with this. I know the implementation of Queue using pointers. From it, I guess we only need two pointers, one for Front and Rear, which may be the minimum.
On the other hand, the elements of the Queue are cells that contain pointers on the next element, so there should be n+1 pointers?
Would be grateful for a nice explanation of this..or at least a hint if nothing else?

When to use pointers [duplicate]

This question already has answers here:
Pointers vs. values in parameters and return values
(5 answers)
Closed 7 years ago.
I'm new to the Go Language, and have only minimal background in C/C++, so naturally I'm struggling with the idea of when to use pointers and when not to use pointers. Although this question might be considered open-ended, I'm wondering what some guidelines on when to return structs and when to return pointers, (and equivalently when to accept structs / pointers as arguments).
From what I can guess, the following statements hold true:
structs are passed into functions by value. That is, a copy of a structure is made when passing it into a function.
if I want to pass a structure by reference, then I would instead use a pointer argument in the function definition, and use the addressof operator when calling the function.
The reason why I would want to pass in a structure by reference is because either the structure I'm passing in is large, and it would be taxing on memory to pass it by value (unlikely) or if I want to make changes to the copy that I'm passing in (more likely).
As a corollary to 3.), I should pass by value unless I have one of the reasons above to pass by reference.
Are my assumptions correct? Or am I missing the mark on pointers?
Your assumptions are correct. About #3, Go is concurrent language and passing by reference in goroutines make them all read same structure which is safe, but also make them modify same structure which is dangerous.

Dont know how to solve a maybe infinitfe loop (Tree) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have a problem.
I have a element and another list of elements, which are connected to the first one in some way.
i want to check, which elements of the list are children of the first. Save the result in an array. Then i will pick the first child and look for his children. save that and so on and on and on.
The problem is, i dont know the exact number of relations. so i could have many many loops and searches.
do i need to program every single loop and array (to save to) or is there a better way?
EDIT:
Iam talking about DB tables. I have two tables. I want to check the children (data in table2) for every element in table1. So i start with a Loop in table1. Elements in Table 1 and table2 are connected with coordinates xy. so iam searching for all elements in table2 where table1_element1.xy == table2.xy. As result there could be n children. Now i want to save them and start for those children a new loop to find for every child his children based on the coordinates. save that new result an so on and on.
More clear?
Thank you.
this problem can be easily solved with the help of recursion.
In this case your termination condition will be when any node don't have any relation in the list or when you reach the end of the list where you are storing the children.
Thanks
While the question is a little vague, rather than programming every individual loop, a more elegant way of writing this would be through recursion. You would set your break condition to be when you find a child (if I'm understanding the problem correctly) with no relations.
As for the storage issue, it sounds like you may want to use some form of a Topology data structure where each node would hold a list of all nodes connected to it through your relation.
Hope this helps.

Resources