# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Isn't F a cousin of D rather than a sibling?
0
Author Commented:
Ok, the problem is linking cousins.
0
Commented:
traverse as in http:/Q_22470611.html keeping track of depth, and start a new pointer chan when the depth changes
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Yep.  Breadth first search will visit the nodes in the proper order.
0
Commented:
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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.