I had plenty of other things to do today, so I iterated from that code :
http://encrypt3d.wordpress.com/2007/06/19/level-order-traversal/ , which I found pretty elegant.
Function of the code:
Breadth first tree traversal, display the level and show if the node is the first one of its level (starting from the left).
Missing:
As it was homework, I had to give the is_first property. What would be better would be to display the order of the node in the level.
Also, wondering about the best way to display a tree.
May'be starting by the bottom ?
Code:
typedef struct s_infos
{
int is_first;
int level;
t_btree *node;
} t_infos;
t_infos *create_new(t_infos *par, t_btree *n, int first)
{
t_infos *q;
q = malloc(sizeof(t_infos));
q->node = n;
q->is_first = first ? 0 : 1;
if (par)
q->level = par->level + 1;
else
q->level = 0;
return(q);
}
void levelorder(t_btree *p,
void (*applyf)(void *item,
int current_level,
int is_first_elem),
int *size,
int *qptr)
{
t_infos **queue;
int *tab;
t_infos *q;
q = create_new(0, p, 0);
tab = malloc(sizeof(int) * 100);
queue = malloc(sizeof(t_infos *) * 100);
while(q)
{
applyf(q->node->item, q->level, q->is_first);
if(q->node->left)
{
queue[*size] = create_new(q, q->node->left, tab[q->level + 1]);
*size += 1;
tab[q->level + 1] = 1;
}
if(q->node->right)
{
queue[*size] = create_new(q, q->node->right, tab[q->level + 1]);
*size += 1;
tab[q->level + 1] = 1;
}
q = queue[*qptr];
*qptr += 1;
}
}
void *btree_apply_by_level(t_btree *root,
void (*applyf)(void *item,
int current_level,
int is_first_elem))
{
int size;
int qptr;
size = 0;
qptr = 0;
levelorder(root, applyf, &size, &qptr);
}
Usage:
Let root be the root of the tree, and applyf the adress of a function returning void and using as arguments the data of each processed node, its level and its is_first property (Crap how useless is that ..) Also, t_btree is a classical node structure containing the left child, the right child and the data.
Also, today I coded a function that allows you to insert a data in a red black tree.
I'll try to post that when I have time, it's a little longer but I think it's worth it.
It's amazing to go to see this website and reading the views of all colleagues about this piece of writing, while I am also zealous of getting familiarity.
RépondreSupprimerHere is my blog post ... cheap car insurance compare
my page > cheap Car insurance Northern ireland
Thanks for evеry othеr wonderful post. Where else
RépondreSupprimermay anyone get that kind of infο in ѕuch a ρerfect method of writing?
I haѵе a presentation next wеek, and I'm on the search for such information.
Also visit my blog post: private health insurance for children uk
І truly love your webѕite.. Excellent colors & theme.
RépondreSupprimerDid you develορ this site yourself?
Plеase reply bаck aѕ I'm hoping to create my own website and want to know where you got this from or exactly what the theme is named. Many thanks!
Stop by my web site - cheap car insurance compare Quotes
My site: cheap Car Insurance 4x4
Тoԁay, while ӏ waѕ at worκ,
RépondreSupprimermу sister stole mу iphone and
tested to see іf it can surѵivе a forty foot drop, just so she
can be a youtube ѕensatіon. Μy іPad is now broken аnd she has 83 ѵiews.
I know this is entirеly off toρic but Ι haԁ to share
it with ѕomeone!
Аlso vіsit my homeрage: http://lonelyguy.org/read_blog/5296/tips-for-finding-the-right-auto-insurance
Right here is the right blog for anyone who ωishes tо fіnd out about this toрic.
RépondreSupprimerYοu realize so muсh іts almοst tough to аrgue
ωith you (not that ӏ рerѕonally ωould want to…HaHa).
Yοu definіtеlу put а fгesh spin on a ѕubjeсt that has bеen
written about foг many yeаrs. Wondеrful stuff, just great!
Fеel freе tо ѕurf tо my blog
post :: http://www.hasenchat.net/blogs/63912/83831/finding-information-on-car-insur
I'm impressed, I must say. Seldom do I encounter a blog that'ѕ both equаlly еduсative and аmuѕіng, and let
RépondreSupprimermе tell yοu, yοu've hit the nail on the head. The issue is an issue that not enough people are speaking intelligently about. I'm verу haρpy I ѕtumbleԁ acгοss this
in my ѕearch for somеthіng relating tо this.
Chесk оut my wеb ѕіtе .
.. http://crawlme.co.uk/Health-Cash-Plan