Thread: Set traversal in Binary Search Tree

  1. #1
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20

    Set traversal in Binary Search Tree

    Hi, guys, need a little bit help with Binary Search Tree. I have to write a function for traversal through the binary tree in preorder, postorder or inorder. What I understood, this function should call the private print function and print the tree in selected order. My problem is, I don't understand what I need to put in function definition for "void setTraverse" function. In the program I have now it gives me error: "note: candidate expects 2 arguments, 1 provided".
    Thank you in advance for any help or advise

    Here is my header file
    Code:
    #ifndef BST__H
    #define BST__H
    
    
    #include "NodeTypeBST.h"
    #include <iostream>
    
    
    enum TravType {PRE, IN, POST};
    
    
    template<class T>
    class BST
    {
    public:
       BST();
       BST(const BST<T>&);
       virtual ~BST();
       bool empty() const;
       void erase();
       void erase(const T&);
       bool find(const T&) const;
       void insert(const T&);
       size_t size() const;
       void setTraverse (TravType);
       const BST<T>& operator= (const BST<T>&);
       template <class U>
       friend std::ostream& operator << (std::ostream&, const BST<U>&);
    
    
     protected:
       NodeTypeBST<T>* root;
       size_t count;
       TravType trav;
    
    
     private:
       void erase(const T&, NodeTypeBST<T>*&);
       bool find(const T&, NodeTypeBST<T>*&) const;
       void insert(const T&, NodeTypeBST<T>*&);
       void copyTree(NodeTypeBST<T>*&, NodeTypeBST<T>*);
       void destroy(NodeTypeBST<T>*&);
       const T& predecessor(NodeTypeBST<T>*);
       void print(std::ostream&, NodeTypeBST<T>*) const;
    };
    
    
    template<class T>
    BST<T>::BST()
    {
       count = 0;
       root = NULL;
       trav = IN;
    }
    
    
    template<class T>
    BST<T>::BST(const BST<T>& src)
    {
       copyTree(root, src.root);
       count = src.count;
       trav = src.trav;
    }
    
    
    template<class T>
    BST<T>::~BST()
    {
       destroy(root);
    }
    
    
    template<class T>
    bool BST<T>::empty() const
    {
       return root==NULL;
    }
    
    
    template<class T>
    void BST<T>::erase()
    {
       destroy(root);
       root ==NULL;
       count = 0;
       trav = IN;
    }
    
    
    template<class T>
    void BST<T>::erase(const T& item)
    {
      erase(item, root);
    }
    
    
    template<class T>
    void BST<T>::erase(const T& item, NodeTypeBST<T>*& node)
    {
      if(node !=NULL)
      {
         if(item == node->item)
         {
            NodeTypeBST<T>*temp = node;
            if(node->right==NULL)
            {
               node = node->left;
               delete temp;
               --count;
            }
          else if (node->left == NULL)
          {
             node = node->right;
             delete temp;
             --count;
          }
          else
          {
             node->item = predecessor(node->left);
             erase(node->item, node->left);
          }
         }
       else if (item < node-> item)
       {
          erase(item, node->left);
       }
       else
       {
          erase(item,node->right);
       }
      }
    }
    
    
    template<class T>
    bool BST<T>::find(const T& item) const
    {
       return find(item, root);
    }
    
    
    template<class T>
    bool BST<T>::find(const T& item, NodeTypeBST<T>*& node) const
    {
       if(node!=NULL)
       {
          if(item == node->item)
          {
             return true;
          }
          else if(item < node->item)
          {
             return find(item, node->left);
          }
          else
          {
             return find(item, node->right);
          }
       }
       else
          return false;
    }
    
    
    template<class T>
    void BST<T>::insert(const T& item)
    {
       insert(item, root);
    }
    
    
    template<class T>
    void BST<T>::insert(const T& item, NodeTypeBST<T>*& node)
    {
       if(node == NULL)
       {
          node = new NodeTypeBST<T>;
          node->item = item;
          node->left = node->right = NULL;
          ++count;
       }
       else if (item < node->item)
       {
          insert(item, node->left);
       }
       else if (item > node->item)
       {
          insert(item, node->right);
       }
    }
    
    
    template<class T>
    void BST<T>::setTraverse (TravType node)
    {
      if(node!=NULL)
       {
          print(node);
       }
    }
    
    
    template<class T>
    size_t BST<T>::size() const
    {
       return count;
    }
    
    
    template<class T>
    const BST<T>& BST<T>::operator= (const BST<T>& src)
    {
       if(this!=&src)
       {
          destroy(root);
          copyTree(root, src.root);
          count = src.count;
          trav = src.trav;
       }
    }
    
    
    template<class T>
    void BST<T>::copyTree(NodeTypeBST<T>*& srcnode, NodeTypeBST<T>* destnode)
    {
       if(srcnode!=NULL)
       {
          destnode = new NodeTypeBST<T>;
          destnode->item = srcnode->item;
          copyTree(destnode->left, srcnode->left);
       }
    }
    
    
    template<class T>
    void BST<T>::destroy(NodeTypeBST<T>*& node)
    {
       if(node!=NULL)
       {
          destroy(node->left);
          destroy(node->right);
          delete node;
          node = NULL;
       }
    }
    
    
    template<class T>
    const T& BST<T>::predecessor(NodeTypeBST<T>* node)
    {
       while(node->right!=NULL)
       {
          node = node->right;
       }
       return node->item;
    }
    
    
    template<class T>
    void BST<T>::print(std::ostream& out, NodeTypeBST<T>* node) const
    {
       if(node!=NULL)
       {
          switch(trav)
          {
          case PRE:
             out << node->item << " ";
             print(out, node->left);
             print(out, node->right);
             break;
          case IN:
             print(out, node->left);
             out << node->item << " ";
             print(out, node->right);
             break;
          case POST:
             print(out, node->left);
             print(out, node->right);
             out << node->item << " ";
          }
       }
    }
    
    
    template <class U>
    std::ostream& operator << (std::ostream& out, const BST<U>& t)
    {
       t.print(out, t.root, 1);
       return out;
    }
    
    
    #endif // BST__H

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    setTraverse will need to be recursive, just like insert or find. The only difference between pre, post, and in-order is what order you print the left subtree, right subtree, and current node in.
    That should be enough of a hint for you to get started.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20
    Quote Originally Posted by iMalc View Post
    setTraverse will need to be recursive, just like insert or find. The only difference between pre, post, and in-order is what order you print the left subtree, right subtree, and current node in.
    That should be enough of a hint for you to get started.
    Thank you, I did sort this out. All I needed was the following:
    Code:
     template<class T>
    void BST<T>::setTraverse (TravType node)
    {
      if(node!=NULL)
       {
          trav = node;
       }
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh I see, you already had the logic within your print function. I was looking in the wrong place.
    Yeah that seems fine. Could probably even do without the null check.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive vs nonrecursive binary tree traversal
    By gaurav9991 in forum C Programming
    Replies: 4
    Last Post: 05-05-2011, 12:28 PM
  2. binary tree traversal
    By nimitzhunter in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2011, 07:13 PM
  3. Constructing Binary Tree From Traversal
    By bennyscuba in forum C Programming
    Replies: 15
    Last Post: 09-24-2009, 04:47 PM
  4. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM