• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Initialize sibling pointers in a tree

I need an algorithm or pseudo code for the following problem:

Initialize sibling pointers of a tree level by level starting with the root.

This is the tree representation before:

Level 1: A (no sibling pointers)
Level 2: B, C (children of A) (no sibling pointers)
Level 3: D (children of B), F, G (children of C) (no sibling pointers)

and after:

Level 1: A
Level 2: B -> C
Level 3: D -> F -> G

Traversal result would be ABCDEFG.

Thanks
0
gromul
Asked:
gromul
1 Solution
 
ozoCommented:
Isn't F a cousin of D rather than a sibling?
0
 
gromulAuthor Commented:
Ok, the problem is linking cousins.
0
 
ozoCommented:
traverse as in http:/Q_22470611.html keeping track of depth, and start a new pointer chan when the depth changes
0
 
NovaDenizenCommented:
Yep.  Breadth first search will visit the nodes in the proper order.
0
 
itsmeandnobodyelseCommented:
I wonder what's the difference between http:/Q_22470611.html and the current question.

Here you said: "Initialize sibling pointers of a tree level" while the other question was "Traverse a complete tree level by level".

Above you said that you mean cousins rather than siblings. Then you said "This is the tree representation before".  Ok, that means that *currently* children pointers and cousin pointers were well defined, right?

But if so  I don't know what "initialization of cousin pointers means". "Initialization" normally means that you set the pointers to NULL. But if you do so, the tree information was lost and you can't traverse to cousins? Or do you mean by "initialization" that you have an empty tree and wants to build a tree like the one above?

Do you have already some code? E. g. a node struct/class ?

Regards, Alex
 
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now