473,398 Members | 2,188 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,398 software developers and data experts.

Data Structure dilemma

5
Hello,all!
I work on a kind of algorithm where the speed and memory consumption are critical. I understand that these are tradeofs but I want to try to reach an optimal solution.
Now I'll try describe the problem: I have a tree where every node have a variable number of children (maximum . The number of elements can reach many millions. In the algoritm I need to pass through all when for every node I pass through all his children one after another.
I want to ask your advice about the implementation. I have some possibilites and want to choose the optimal. So, the possibilities are:

1) To build it in a tree manner. I define a structure that is similar to a linked list, but the field 'next' will be dynamically allocated to contain the actual number of children. This seems to me the most straightforward implementation,
however it begins to be a bit complicated to allocate the memory everytime and to free all of it at the end. This way also forces me to work in the recursive way and I don't know how efficient it is.

2) To build one single linked list when I arrange the elements in a such way that for a node m at level n the children will be after the children of a node at place m-1. For every node I add a link to his first child and save the number of his children.
In this way, given a node I can pass through all his children at O(N) time.This also forces me to work on the algorithm in the recursion.

3) To work like in step "2" but instead of a linked list to add elements to the array using realloc function. However I don't if this way is fast and efficient. Also if I have to pass through all the nodes one after another, does the array have the advantage over a linked list in the access speed?

4) To work like in "2" and after it to create an array and transfer the elements from the linked list to the array.

Basically,the main advantage of the array is that it has an access of O(1) to every element and I can implement an algorithm in a non recursive way.
Therefore the main question is: for a recursive solution which is better 2 or 1, and for non recursive 3 or 4?

Thanks alot!
mshr25
Jun 15 '07 #1
4 1510
weaknessforcats
9,208 Expert Mod 8TB
I'm not sure of your requirement here:
1) a node can have many children
Ergo: a child can have children
Erfo: a chile is a node
Ergo: use a tree

Do I have that right?

I think you want to avoid a linked list due to the long processing times for millions of nodes.

Are you working in C++??

Ifso, try a map<> if keys are unique or multimap<> if keys are duplicated. These STL containers are maximized for speed so you won't be able to write a faster container.

map<> and multimap<> use a red/black tree which, as you know, is a binary representation of a 2-3-4-tree.
Jun 15 '07 #2
mshr25
5
Thanks for trying to answer!
First of all I work in C and I'm not familiar with C++. I did some google searching and found that my data structure is called octal tree or octree. Every node can have up to 8 children but usually it will have less (in the program I find how many exactly are for every node).There is a root node and also there are root nodes that represent the elements on the last level of division.
I try to avoid linked list because it takes more memory than array. However adding nodes to an array with realloc seems not a good idea (or maybe I wrong).
I'm also not sure whether running through an array cell after cell is faster than running through a linked list link after link-what do you say? I must say that I pass through the structure once and do not have to implement searching and deleting.

There is another method:For every node I check how many nodes it has and allocate an array of pointers to nodes accordingly. This seems to me a bit complicated. In all the implementations I saw on the net there always 8 different pointers while some of them points to data and some of them contain NULL. But it also seems to me a waste of memory.

mshr25
Jun 15 '07 #3
JosAH
11,448 Expert 8TB
I have been using the following structure for my good ol' minimal cost network
flow algorithm for a long time; it works fine for a general tree and it's simple:

Expand|Select|Wrap|Line Numbers
  1. typedef struct node {
  2.    struct node* child; /* pointer to leftmost child */
  3.    struct node* parent; /* pointer to parent */
  4.    struct node* sibbling; /* pointer to next sibbling */
  5.    data_t data;
  6. } node_t;
  7.  
There's just one pointer to the children which are linked together by the sibling
pointer (a linked list). The pointer to the parent makes it possible to easily crawl
back up the tree. Inserting a new leftmost child is easy. Deletion of a child takes
a linear traversal through the sibling list. But most important (to me) is/was that
all the nodes have the same size.

kind regards,

Jos
Jun 15 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
have been using the following structure for my good ol' minimal cost network
flow algorithm for a long time; it works fine for a general tree and it's simple:


Code: ( c )
typedef struct node {
struct node* child; /* pointer to leftmost child */
struct node* parent; /* pointer to parent */
struct node* sibbling; /* pointer to next sibbling */
data_t data;
} node_t;


There's just one pointer to the children which are linked together by the sibling
pointer (a linked list). The pointer to the parent makes it possible to easily crawl
back up the tree. Inserting a new leftmost child is easy. Deletion of a child takes
a linear traversal through the sibling list. But most important (to me) is/was that
all the nodes have the same size.
This looks exactly like the node used by map<> and multimap<>.

Arfe you sure these won't work??
Jun 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Jones | last post by:
need help with data structures.Looking for ways to start, sample code, anything Program description: Design and implement a Visual C++ .NET program that inserts values into a data...
2
by: Pablo | last post by:
I have a dilemma. Currently, I may be passing standard text (strings of char) or binary of 1 to 'x' bytes long to a program for comparison with data previously written to a file. The problem...
2
by: yee young han | last post by:
I need a fast data structure and algorithm like below condition. (1) this data structure contain only 10,000 data entry. (2) data structure's one entry is like below typedef struct _DataEntry_...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
3
by: Bosconian | last post by:
I have an array defined as follows: $scores = 19; $scores = 25; $scores = 23; $scores = 25; .... where the key is the team # and the value is the points. I am outputting the key/values as...
14
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project...
0
by: quantumlady | last post by:
Hi, All. I am working on a project with another student that will be used on Oracle, MySWL and DB2 databases. We have completed all the basic functions, but I would like to see if we can improve...
3
by: iu2 | last post by:
Hi all, I need your professional opinion about this. It is more a general programming dilemma rather then a C++ one, but since the project I write is in C++... We handle big structs of data....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.