473,591 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with tree and enhanced queue.

Dear all,

I want to create a tree, where every node has an undetermined number
of children (during the population of the tree, the exact number of
children of each node , will become clear). So i thought that i could
use in each node a queue in which we will add, every time a new child
is born, the address of this node. Here are the structures i have used:

// The queue node. Every node contains a pointer to a tree node and a
pointer to the next
// element of the queue
typedef struct queue_tag {
struct TreeNode_tag * child_ptr;
struct queue_tag * next;
} QueueNode;

// the queue
typedef struct {
QueueNode *head;
QueueNode *tail;
} queue;

// the tree. Every leaf contains an integer i and a queue which
contains the pointers to node's children
typedef struct TreeNode_tag {
int i;
queue *q;
} TreeNode;

// The tree
typedef TreeNode * tree;

The problem is that i get a segmentation fault every time i try to
access the queue of the tree node. Here is the code i use to start the
tree :

tree *t;
*t = (TreeNode *)malloc(sizeof (TreeNode));

(*t)->i = 0; // a random value
(*t)->q->head = NULL; // initially the node has no children.
(*t)->q->tail = NULL;

I use gcc.

Thank you for your time.

Jan 5 '06 #1
4 1578
"negative" <pa*****@gmail. com> writes:
The problem is that i get a segmentation fault every time i try to
access the queue of the tree node. Here is the code i use to start the
tree : tree *t;
*t = (TreeNode *)malloc(sizeof (TreeNode));


Where have you allocated space for the tree's queue? I think you require:

*t = malloc(sizeof(T reeNode));
(*t)->q = malloc(sizeof(Q ueue));

--
Chris.
Jan 5 '06 #2
negative wrote:
Dear all,

<snip>

// the tree. Every leaf contains an integer i and a queue which
contains the pointers to node's children
typedef struct TreeNode_tag {
int i;
queue *q;
} TreeNode;

// The tree
typedef TreeNode * tree;

The problem is that i get a segmentation fault every time i try to
access the queue of the tree node. Here is the code i use to start the
tree :

tree *t;
*t = (TreeNode *)malloc(sizeof (TreeNode)); No bearing on your problem, but the cast is unnecessary and
sizeof( *t ) (brackets optional) is often preferred to sizeof(TreeNode )
since it's harder to get wrong.

(*t)->i = 0; // a random value
(*t)->q->head = NULL; // initially the node has no children.
(*t)->q->tail = NULL;


(*t)->q is an uninitialised pointer.

--
imalone
Jan 5 '06 #3
Hi,

At 5 Jan 2006 05:18:11 -0800,
negative wrote:
// The tree
typedef TreeNode * tree;

The problem is that i get a segmentation fault every time i try to
access the queue of the tree node. Here is the code i use to start the
tree :

tree *t; This is the same as
TreeNode **t;

Well, you got some errors here.
*t = (TreeNode *)malloc(sizeof (TreeNode));


Why did you do a typedef, when you are using this instead of
*t = (tree) malloc (sizeof (TreeNode));

Here you (try) initialize the value t points to with something, but
t points to some random place in memory. You should have done something
like
t = (tree *) malloc (sizeof (tree));
first.
Regards,
Roland
--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro****** ******@knapp.co m
Jan 5 '06 #4

Chris McDonald wrote:
Where have you allocated space for the tree's queue? I think you require:

*t = malloc(sizeof(T reeNode));
(*t)->q = malloc(sizeof(Q ueue));

--
Chris.


thanks Chris

This was finally the problem. I had forgotten to allocate space for
the tree's queue.

Negative...

Jan 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8110
by: Adalbert | last post by:
First, I'm sorry for my english. Second, I've a little question: is some tree stucture like collections ArrayList or Queue to hold tree of same objects in .NET? Maybe I can use TreeView to that. I must hold tree of Halma's game (like checkers), which I generate with minimax algorithm. Hmm, is it good in english? :-) I hope :-) Thanks for all answers.
8
3679
by: Shock | last post by:
Hello everyone, I am having a problem with the program below. I have isolated the problem to the onclick event that is located throughout arrQuestions. The onclick event refers to a function and passes it two parameters. For the life of me I cannot figure out what the error is, but one occurs everytime I click a &%*$&# radio button. Also, the function only alerts the user immediately as to what question/answer they clicked. I am...
7
3632
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
7
2892
by: Casper | last post by:
In scanning a drive and comparing file content, I need to remember filepaths to every single file I encounter so I can take actions on certain files later on. Rather than having a huge list enumerating the complete filepath to every file it seems the smarter way (faster, more memmory efficient) is to model the filesystem treestructure in a abstract tree - and having only the filenames & node pointer in an Array. struct tree {
5
9559
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
0
2603
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated monomers) which are represented as doubly pointed noncomplete binary trees. There are three different types of monomers (hence the three different constructer calling functions) the first one is the "leaves" of the tree, the second adds length...
3
5780
by: Rob Latour | last post by:
The following snippet (vb.net 2005) is working just fine in xp but not in vista in xp it lists all related sub keys in the registry just fine. in vista it doesn't list certain ones (like Unimodem) Dim rk As Microsoft.Win32.RegistryKey rk = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion") For Each SubKeyName As String In rk.GetSubKeyNames()
6
20008
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree are printed on a separate line. my codes are below,( for printing inorder, preorder and post order) I have no Idea how I can print them in , level-order traversal I think I should use a Queue, but how? do you have any code that can help me? would...
3
1396
by: slizorn | last post by:
hi guys, the error is as follows: error C2440: '=' : cannot convert from 'TreeNode<T> *' to 'char *' with 1> 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i get this error from the following line of code: assign = searchTree(treeObj->root ,data1);
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.