Wrox Programmer Forums
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 24th, 2005, 07:45 AM
Authorized User
 
Join Date: Jul 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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...


Reply With Quote
  #2 (permalink)  
Old February 8th, 2005, 06:08 AM
Registered User
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rupsee Send a message via AIM to rupsee
Default

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);
}


Reply With Quote
  #3 (permalink)  
Old February 26th, 2005, 12:07 PM
Authorized User
 
Join Date: Feb 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

check this link

http://www.damtp.cam.ac.uk/user/kp229/tree/

punkaj
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing tree structure rjonk XSLT 6 November 16th, 2006 10:11 AM
Create a tree structure in visual C++ limkimh88 C++ Programming 7 June 22nd, 2006 11:14 PM
Need help with tree data structure vidhya_venkat C++ Programming 0 June 14th, 2006 01:13 PM
tree data structure pandjie Java Basics 1 January 16th, 2006 05:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.