Solved

Initialize sibling pointers in a tree

Posted on 2007-03-24
5
415 Views
Last Modified: 2008-02-01
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
Comment
Question by:gromul
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18787555
Isn't F a cousin of D rather than a sibling?
0
 

Author Comment

by:gromul
ID: 18787560
Ok, the problem is linking cousins.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 18787584
traverse as in http:/Q_22470611.html keeping track of depth, and start a new pointer chan when the depth changes
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 18808589
Yep.  Breadth first search will visit the nodes in the proper order.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18808928
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question