Solved

# Initialize sibling pointers in a tree

Posted on 2007-03-24
413 Views
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
Question by:gromul

LVL 84

Expert Comment

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

Author Comment

ID: 18787560
Ok, the problem is linking cousins.
0

LVL 84

Accepted Solution

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

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

LVL 39

Expert Comment

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.