Thread: I would love some input on my BST tree.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I couldn't help but notice that you never destroy from anywhere but the root.
    Code:
    template <class T> struct node
    {
            T element;
            node<T> * left;
            node<T> * right;
    };
    
    template <class T> class BST
    {
    public:     
            BST();
            ~BST();
            //display(node<T> * = root);  
                 // -- consider returning an std::string or having it take a stream reference as a parameter
            void destroy(node<T> *& troot = root);
            void insert(T newElement, node<T> *& troot = root);
            int remove(T oldElement, node<T> *& troot = root);
            const int population() { return num_in_tree; }
    private:
            node<T> * root;
            int num_in_tree;
            node<T> * inorderSuccessor(node<T> *& troot);
            int children(node<T> * troot);
           
    };
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CodeMonkey
    I couldn't help but notice that you never destroy from anywhere but the root.
    Incorrect.
    The recursive calls that pass in troot->left and troot->right, actually modify troot->left and troot->right, and being that they are the root of their own subtree it can delete from anywhere.
    I'm not saying the the rest of the function is totally correct though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input Spatial Data for BSP Tree
    By thetinman in forum Game Programming
    Replies: 2
    Last Post: 10-03-2007, 01:26 PM
  2. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM