473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to deal with Tree with several branchs?

My program deal with a several level tree with several branchs. The
amount of branchs from father node is not known. So I want to creat a
tree with dynamic branchs.

Now I use the structure:
typedef struct gnodes
{
int level; //tree level
struct gnodes* pfather;
struct gnodes* pbrother;
struct gnodes* pchild;
struct gnodes* puncle;
} gnode;

The child pointer link one by one form a chain, something like:
Father(i)->Child(1)->Child(2)->...->Child(n-1)->Child(n).
But I can not search child randomly.

How to design a better structure?
Any suggestions will be appreciated!

Best regards,
Davy

Jul 23 '05 #1
2 1516
Davy wrote:
My program deal with a several level tree with several branchs. The
amount of branchs from father node is not known. So I want to creat a
tree with dynamic branchs.

Now I use the structure:
typedef struct gnodes
{
int level; //tree level
struct gnodes* pfather;
struct gnodes* pbrother;
struct gnodes* pchild;
struct gnodes* puncle;
} gnode;

The child pointer link one by one form a chain, something like:
Father(i)->Child(1)->Child(2)->...->Child(n-1)->Child(n).
But I can not search child randomly.


You could just allocate an array and when you need to further
populate it you just reallocate, extend and copy the child elements.

Basically you need only one procedure:

Changed [gnodes * pchild] to [gnodes ** pchild] since it now
is a pointer to an array of pointers to gnodes, and
initialize both pchild and pCurrentChildSize to 0(null) and
it should work, even delete[] should be safe when the first element
is pushed.

void gnodes::push(gnodes * element)
{
gnodes ** temp = new gnodes*[pCurrentChildSize + 1];
for(unsigned i = 0; i < pCurrentChildSize; ++i)
{
temp[i] = pchild[i];
}
temp[pCurrentChildSize] = element;
++pCurrentChildSize;
delete[] pchild;
pchild = temp;
}

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 23 '05 #2
Davy wrote:
My program deal with a several level tree with several branchs. The
amount of branchs from father node is not known. So I want to creat a
tree with dynamic branchs.

The first thing you'd better do is pick a language. You posted the same
question to comp.lang.c.


Brian
Jul 23 '05 #3

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

Similar topics

1
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...
12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
4
by: Robin Tucker | last post by:
Hi, I'm currently implementing a database with a tree structure in a table. The nodes in the tree are stored as records with a column called "Parent". The root of the tree has a "NULL" parent....
9
by: developer | last post by:
Does anyone know what is the way IE treats span tags(<span>) and table tags(<tr>, <td>)? Should the <span> tag be encolsed in tds and trs if it placed with other elements that are in a table? Can...
25
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
1
by: Davy | last post by:
My program deal with a several level tree with several branchs. The amount of branchs from father node is not known. So I want to creat a tree with dynamic branchs. Now I use the structure:...
2
by: Kevin Lippiatt | last post by:
Can anyone advise me on how well the windows .net tree view control performs when loaded with large numbers of nodes. I'm intersted in how it might perform when used with possibly millions of...
1
by: Satish.Talyan | last post by:
hi, i want to create a dynamic tree hierarchy in javascript.there are two parts in tree, group & user.when we click on group then users come under that's tree category will be opened.problem is...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
5
by: M.Liang Liu | last post by:
I have a project with the following dirs: --------------------------------------------------------------------------------------- +src |-proj0 |-program1 |-program2 |-proj1 |-program1...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.