|
|
 |
| C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C++ Programming section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

January 24th, 2005, 07:45 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Creating a Tree Structure
Hi All,
I want to build a structure of userdefined elements,which will be having child & subchild elemetnts as well(Like a Tree Structure). Using STL or so...
Is there any build-In facility in c++,by which I could create a tree structure like this & query for elements as well, within this structure.
If not any suggestions how to proceed.....
Thanks...
|

February 8th, 2005, 06:08 AM
|
|
Registered User
|
|
Join Date: Feb 2005
Location: mumbai, maharashtra, India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi...
you could simply create a binary tree....
check this code...
#include<stdlib.h>
#include<stdio.h>
struct tree_el {
int val;
struct tree_el * right, * left;
};
typedef struct tree_el node;
void insert(node ** tree, node * item) {
if(!(*tree)) {
*tree = item;
return;
}
if(item->val<(*tree)->val)
insert(&(*tree)->left, item);
else if(item->val>(*tree)->val)
insert(&(*tree)->right, item);
}
void printout(node * tree) {
if(tree->left) printout(tree->left);
printf("%d\n",tree->val);
if(tree->right) printout(tree->right);
}
void main() {
node * curr, * root;
int i;
root = NULL;
for(i=1;i<=10;i++) {
curr = (node *)malloc(sizeof(node));
curr->left = curr->right = NULL;
curr->val = rand();
insert(&root, curr);
}
printout(root);
}
|

February 26th, 2005, 12:07 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Location: Mumbai, Maharashtra, India.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |