TRENDING NEWS

POPULAR NEWS

A Tree Structure In C

How do you implement a tree data structure in C++?

It doesn’t have to be complicated. All you need is a Node struct (or class) that links to other nodes.struct Node {
... // other data
std::vector children_;
};

Node head;
Then add helpers that attach children to other nodes, traversal, removal, etc.

Data Structure of General Tree?

If you want to create a tree, you must define its structure (in C). For that, you must specify how many children you want each node to have.
If you want it to be binary, it will have 2 pointers(left and right), along with the info part. For a 3-child tree, it will have left, right, mid, and info.
Before digging further, why do you want to make a general tree? It is not used in any application. If you want for college programming level, a 3-child general tree would be enough.

Hope this helped. If not, you can mail me

How do I master the tree data structure and heaps in C? Are there good tutorials that provide enough details?

Start reading "Data Structure and Files by Andrew Tanenbaum". It'll help in understanding the concepts. It's my personal favorite. Once you start getting the concepts, follow through with the example codes given in the book.  That should help you in understanding how to convert mathematical representation of these data structures into a working program using language specific constructs. Once that is done, refer "Design and Analysis of algorithms by T. Cormen".This is an advance book and has more complicated problems, try to solve the questions from these books, programmatically of course.Practice will be required to code efficiently.

What is the best site to study tree data structure in C++?

Data Structure is an important topic of computer science. Few topics can't be completed through websites. For them you need video lectures. However, you can try my blog for flash based examples, algorithms, easy to access DS contents at http://codstudio.WordPress.comThere are also few sites as StudyTonight, GeeksForGeeks, and others.

Why is there no tree data structure available in C++ STL?

As Mr Bi said, map/set covers many cases for which you would use a tree, but has a higher level of abstraction, because you don’t want explicitly to traverse the links in a tree when you iterate through a map. You just want to say ++iterator. After the amount of time spent learning about trees in Algorithms & Data Structures, it was interesting to me that they are not used much on their own.There is a balanced binary tree data type in STL, or rather in your STL implementation. Map and set are built on it. You can read the map code and find what its name is, and instantiate it. Best of luck.

How do you find the total number of bytes of a tree data structure in C?

This is a bit of black magic…The simple answer is * . This would be true for any linked data structure.As far as actually determining this, you’ll have to work it out, and there’s more complexity than meets the eye:If your nodes have pointers to other memory items such as strings, or pointers to other data structures, you have to account for those.sizeof(X) or strlen(str) + 1 doesn’t take into account memory allocator overhead. Memory management.A good way to figure out how big your tree is is by doing a simple experiment:Do a small malloc(). Save the address in a char or uint8 variable.Load up your tree with an interesting data set - and don’t do anything else.Do another small malloc() to another char or uint8 variable. Subtract this address from your first address, and print the result as an integer. This is how many bytes were added by your data structure.Even this isn’t completely accurate as it can be overstated by malloc’s typical “memory chunk” allocation approach - and only works if your malloc() doesn’t use mmap() for some allocations - but it will be more accurate for actual runtime estimations than a pen-and-paper exercise, which will understate how much RAM is used, often by quite a bit.If you load your tree with a few thousand nodes, this experiment should give you a reasonably accurate estimate.

Why a lot of binary-tree data structures in C do not have a parent node pointer?

For many applications navigation is done only from the top and down, with no need to go sideways, or in such fashion as sideways pointers can be held in the call stack because of recursive descent. Without need for the parent, it's just wasted space.

TRENDING NEWS