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 April 15th, 2006, 03:57 AM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Create a tree structure in visual C++

Hi all,
I want to write a tree structure program from visual C++ .Net, i have to declare root, child, next siblings, prev siblings and able to import metadata from a source and import into my program and display the metadata into a tree structure.

I really need help to get me kickstart in writing this program.
Really appreciate any help given.

Thks in advance.
Kim

Reply With Quote
  #2 (permalink)  
Old April 16th, 2006, 02:10 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

you could declare some structs and define a set functions that process the structs like so:


struct metaData; // forward declaration
struct treeNode
     {
         metaData* myData;
         treeNode* leftChild;
         treeNode* RightChild;
     }

struct tree
     {
         treeNode* rootNode;
     }
    
// function example
tree* createTree() // creates an empty tree Container
treeNode* createNode(metaData& Input) //create a new tree node and intializes with your meta data
int addNode(tree* myTree, treeNode* newNode) //adds news nodes to tree)
// any other functions needed


you could like wise declare as classes with a set methods to operate on the object:

class treeNode
     {
     private:
         metaData* myData;
         treeNode* leftChild;
         treeNode* rightChild;
     public:
         treeNode(); // constructor set initial conditions of object
         setData(); //sets MetaData
         setLeft(); // attaches a left child
         setRight(); // attaches a right child
         // any other methods that you need
     }
class Tree
     {
        private:
         treeNode* Root;
        public:
         Tree(); constructor set initial of tree
         setRoot();
         add(); // add a node to tree
         delete(); // deletes a node from tree
         // any other methods you want
        }


or you could use existing STL containers like stacks, maps, lists, etc...
Reply With Quote
  #3 (permalink)  
Old April 16th, 2006, 05:52 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI everyone and Mike, thks for the push. I've managed to come out a little parent, child and sibling relationship loop. Now, i would need to display this replationship into a dynamic the tree structure display. I wonder how can i go about doing it. I have read up the MSDN help, but still couldn't quite makeup the link.

Second, i need node2 as a capture storage,it holds pervious value and compare to the next metadata coming in at a later time.
I wonder did i do it correctly or there is a better way.

Like if first set of data in (parent = Earth, child = USA), Second set of data in (parent = Earth, child = AUS), Both will have the same parent, and USA and AUS are sibling to each other. I need to display this relationship in a tree structure.



Pls advise,
Part of my code is as the followings.

if (node == NULL)
{
    if (parentlabel!= NULL)
    {
    root->label = new char[strlen(parentlabel)+1];
    strcpy(root->label,parentlabel);
    root->tree = true;
        if (childlabel!= NULL)
        {
            node2 = root->child =new browsernode;;
            node2->parent = root;
            node2->label = new char[strlen(childlabel)+1];
            strcpy(node2->label,childlabel);
            node2->tree = true;

        }
    }

}

  else
  {
        if (parentlabel!= NULL && node->parent != NULL)
        {
            oldlabel = new char[strlen(node->parent->label)+1];
            strcpy(oldlabel,node->parent->label);
            for (n =0 ;n < strlen(oldlabel); n++)
            {
                    if(oldlabel[n] != parentlabel[n])

                        break;
            }
            if (oldlabel[n] == parentlabel[n] && childlabel != NULL)
            {
                    node2 = new browsernode;
                    node2->parent = node->parent;
                    node2->label = new char[strlen(childlabel)+1];
                    strcpy(node2->label,childlabel);
                    node->next_sibling = node2;
                    node2->prev_sibling = node;
            }
            else
            {
                    root->label = new char[strlen (parentlabel)+1];
                    strcpy(root->label,parentlabel);
                    if (childlabel != NULL)
                    {
                        node2 = root->child = new browsernode;
                        node2->parent = root;
                        node2->label = new char[strlen(childlabel)+1];
                        strcpy(node2->label, childlabel);
                    }
            }

        }
        else
            if (childlabel != NULL)
            {
                node = new browsernode;
                node = oldnode->parent;
                while (node->parent !=NULL)
                {
                    node = node->parent;
                }
                root = node;
                node2 = root->child = new browsernode;
                node2->parent = root;
                node2->label = new char[strlen(childlabel)+1];
                strcpy(node2->label, childlabel);
            }

  }

return node2;

KIM



Reply With Quote
  #4 (permalink)  
Old April 17th, 2006, 05:07 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

Not sure what your asking here are you taking about linkage from one node to the next or are you taking phyically displaying the tree. some helpful tips: have some comments in in your code as to what your expecting your logic todo and maybe how you have declared your structures. really to me it looks more like a double linked list with parent and child lables I am guessing, but of coarse I could be missing something. I am farely certain that your code snipet is supposed to add nodes right. just need a little bit more info, also what order(# of child nodes any one parent can have) of tree are you dealing with the prev_sibling next_sibling made me thing of an order greater than 2.
Reply With Quote
  #5 (permalink)  
Old April 17th, 2006, 05:28 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi all and Mike, our program is reading metadata from a pic and add them onto my tree structure, with parent and child relationship. And yes, i'm suppose to create nodes once i've obtain metadata from the pic.
Our program is to have a single root, multiple parents and childs, so, the number of child could get on and on, it depends on the number of metadata found on the pic (jpx format)

Do u know how to walk down and across the tree and compare the latest node information with all the pervious nodes found in the tree? Like

          Earth
            |-USA
            |-AUS
            |-CHINA
            |-SINGAPORE

New node coming in (parent = Earth, child = Russia), this node must be able to compare with all the nodes in my tree sructure in order to see where it should be attached to. If successful, it should look like this,

          Earth
            |-USA
            |-AUS
            |-CHINA
            |-SINGAPORE
            |-Russia
Secondly, if user input (parent = USA, child = Washington) the tree should look like this,

          Earth
            |-USA
               |-Washington
            |-AUS
            |-CHINA
            |-SINGAPORE
            |-Russia

We are thinking of linking and comparing each child node with labal and ROI-Region of Interest comparison method. Do u know how to compare by ROI method, coz, it is a important part of our project doing.

We really need help, if anyone could enlighten us, pls do help.

Thanks a million.
Kim

Reply With Quote
  #6 (permalink)  
Old April 18th, 2006, 04:42 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

traversing a tree is quite easy, just depends oh how you have set up your storage structures and linkage. take what I had declared earlier and have its data be names i.e. john, jeff, mark, and so on.

as simple recursive add routine might be like this:

// add routine user interface.
void add (tree* myTree, treeNode* newNode)
{
     // in this routine there are two cases to handle
     // an empty tree and a not empty tree.
     if(!myTree->root)// if tree is empty add first node
     {
         // tree was empty adding first node
         myTree->root = newNode;
     }
     else // else tree not empty
     {
         // find an insertion point
         myTree->root = insert(myTree->root,newNode);
     }
     return;
}

// recurisive insert routine...traverses the tree for an insertion
// point and adds node, this routine is only called by the add
// routine (user interface)
treeNode* insert(treeNode* root, treeNode* newNode)
{
     // in this routine there are 3 cases to handle
     // newNode less than current node
     // newNode greater than current node
     // newNode equal to current node
     treeNode* curr = root;
     if(newNode->name < curr->name) // if less than go left
     {
         if(curr->leftChild) // test to see if there is a leftChild
         {
            // there was a leftchild traverse left
             curr = insert(curr->leftChild,newNode);
         }
         else // else there is no left child so add.
         curr->leftChild = newNode;
     }
     else if(newNode->name > curr->name) // if greater than go right
     {
         if(curr->rightChild) // test to see if there is a rightChild
         {
            // there was a rightChild traverse right
             curr = insert(curr->rightChild,newNode);
         }
         else // else there is no right child so add.
         curr->rightChild = newNode;
     }
     else // newNode is not < or > must be == to
     {
         // duplicate node delete
         delete newNode;
     }
     return curr;
}


a simple recursive depth first traverse routine to process the nodes might look like this. my process is simply to print the name contain in the node to the console.

void TraverseP ( treeNode* root )
{
     traverseNode = root; // get the root of tree

     if ( traverseNode->leftchild ) // if leftchild traverse tree
         TraverseP( traverseNode->leftChild );
     if (traverseNode->righttchild) // if rightchild traverse tree
         TraverseP( travserNode->rightChild);
     // call process function
     cout<<traversNode->name;

     Return;
}

this tree is constructed of single dynamically allocated nodes defined by the struct declarartion that I posted earlier. my linkage is a pointer from one node to the next. this pattern of tree breathes recurision and is quite powerfull if you can get the hang of it.
Reply With Quote
  #7 (permalink)  
Old June 15th, 2006, 02:22 AM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can you please send me the source code to create a tree structure using vc++?

Thanks,
Satya.
Reply With Quote
  #8 (permalink)  
Old June 22nd, 2006, 11:14 PM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The best and secure way to implement a tree is using STL MAP container:

   map<Key, Element> Tree

say you want a state/city tree:

class statecity
{
   string _state // redundant
   string _city;
   int _ areacode // assuming there's only one area code
public:
    statecity(){
        _state = ""; // init variables
        _city = "";
        _areacode = 0;
    }
    virtual ~statecity(){};
}

 // create an empty container
 map<string, statecity > StateCityTree;

 // create a new instance of your class (node)
 statecity * sc = new statecity;
 sc->_state = "CT";
 sc->_city = "Hartford";
 sc->_areacode = 860;

 // create you node ( entry) in your tree
 StateCityTree["CT"] = *sc;

 // cleanup
 delete sc;

if you want to know the details dig up the STL header files.




Jalapeno
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing tree structure rjonk XSLT 6 November 16th, 2006 10:11 AM
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
Creating a Tree Structure pazzuzu C++ Programming 2 February 26th, 2005 12:07 PM





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