Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,068 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,583 people online right now. Registration is fast and FREE... Join Now!




Binary Search Tree

 
Reply to this topicStart new topic

Binary Search Tree, Pretty Confused(Very New to C++)

jreibe3000
22 Apr, 2008 - 08:36 PM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
I have a pretty hefty program I'm trying to get together. I can't post ALL the code, but long story short the three methods I'm currently working on are a breadth-first traversal function, a recursive searching function, and another search function that calls the recursive search function for the binary tree. Below I will post the code for my depth-first traversal method:

CODE

template <class Type>
void BinarySearchTree<Type>::dft(void (*process) (const Type & item))
{
   Stack<NodeP>stack;

   NodeP node;

   if (!empty())
   {
      stack.push(rootNode);
      while(!stack.empty())
      {
          node=stack.pop();
          process(node->data);

          if(node->rightNode!=NULL)
          {
              stack.push(node->rightNode);
           }
           if(node->leftNode!=NULL)
           {
              stack.push(node->leftNode);
            }
         }
       }
       return;
   }


I have been told that my above function is correct, but that my next function is close but slightly incorrect:

CODE

template <class Type>
void BinarySearchTree<Type>::bft(void (*process)(const Type & item))
{
   Queue(NodeP)queue;
   NodeP node;

   if(!empty())
   {
       queue.enqueue(rootNode);
       while(!queue.empty())
       {
           node=queue.dequeue();
           process(node->data);

           if(node->rightNode!=NULL)
           {
               queue.enqueue(node->rightNode);
            }
            if(node->leftNode!=NULL)
            {
                queue.enqueue(node->leftNode);
             }
         }
        }
        return;
    }



This second method compiles, but I've been told the ordering is wrong. Any help is much appreciated.

Jake





User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month