View Single Post
  #6 (permalink)  
Old April 18th, 2006, 04:42 AM
C@uark C@uark is offline
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