Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
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 June 14th, 2006, 01:13 PM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vidhya_venkat
Default Need help with tree data structure

Hi,

I have been give the following problem to solve:

Implement a procedure that efficiently converts a linked list into a tree.

PROBLEM BACKGROUND: Each Object in the linked list has a pointer to the next Object in the list. Each Object has an ID and knows the ID of its parent in the tree (parent_ID). IDs are unique and range from 1 to N in a list with N elements, however N is not initially known. The tree root’s parent_ID is 0, but the root’s ID is not initially known.

Objects are represented using the following structure:

typedef struct Object
    {
    // THESE FIELDS ARE ALREADY DEFINED; YOU MAY NOT MODIFY THEM
    Object* next_ptr; // Next object in list; NULL if end of list.
    unsigned int ID; // unique identifier for this object.
    unsigned int parent_ID; // 0, if object is root of tree.

    // THESE FIELDS REPRESENT THE TREE; YOU MUST FILL THEM IN
    Object* parent_ptr; // Initialized as NULL.
    unsigned int child_count; // Initialized as 0.
    Object** child_ptr_array; // Initialized as NULL.
    } Object;


YOUR TASK: Your task is to implement the following procedure:

Object* Convert_Objects_To_Tree (Object* head_object_ptr);

This procedure must:

(1) return a pointer to the root Object,
(2) fill in the parent_ptr, child_count, and child_ptr_array fields for each Object in the linked list, but
(3) not modify the next_ptr, ID, or parent_ID fields in any Object.

Can anybody help on this? The way I'm approaching this problem is to find the root node first by checking the parent ID==0 and then iterating through each Object in the linked list to find out all the children of the root node...and likewise for each node.
But I'm unable to proceed and need help on how to approach this. Thanks.

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing tree structure rjonk XSLT 6 November 16th, 2006 10:11 AM
tree data structure pandjie Java Basics 1 January 16th, 2006 05:15 AM
Creating a Tree Structure pazzuzu C++ Programming 2 February 26th, 2005 12:07 PM
Show data in tree-view structure kiennt Crystal Reports 1 March 1st, 2004 06:21 PM





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