简体   繁体   中英

Displaying Path to a Node in a Binary Search Tree

I'm working on trying to display the path from the root node of a BST to a target node. The function I have works fine for the first two layers, but then messes up after that. For example, the test numbers are 6, 9, 4, 11, 10(inserted in that order). If I search for 6, 9, or 4, it works (ex: "6 9"). But if I try 11 or 10, it displays them both, and out of order. I'm kind of stumped as to why. Any ideas would be awesome!

template <class T>
void BST<T>::displayPath(T searchKey, BST<T> *node)
{
    if (searchKey == node->mData)
    {
        cout << node->mData << " ";
    }

    else if (searchKey < node->mData )
    {
        cout << node->mData << " ";
        displayPath(searchKey, node->mLeft);
    }
    else// (searchKey > node->mData)
    {
        cout << node->mData << " ";
        displayPath(searchKey, node->mRight);
    }
}

This is the insert function. The numbers are inserted in the order above.

template <class T>
void BST<T>::insert(BST<T> *&node, T data)
{
   // If the tree is empty, make a new node and make it 
   // the root of the tree.
   if (node == NULL)
   { 
      node = new BST<T>(data, NULL, NULL);
      return;
   }

   // If num is already in tree: return.
   if (node->mData == data)
      return;

   // The tree is not empty: insert the new node into the
   // left or right subtree.
   if (data < node->mData)
          insert(node->mLeft, data);
   else
      insert(node->mRight, data);
}

Your code confirms my suspicion. Your method is fine - this is the tree you built from inserting 6, 9, 4, 11, 10 in this order:

first (6):

 6

second (9)

 6
  \
   9

3rd (4)

  6
 / \
4   9

4th (11):

   6
  / \
 /   \
4     9
       \
        \
         11

5th (10):

   6
  / \
 /   \
4     9
       \
        \
         11
         /
        /
       10

So searching for 10, will give you the path (6,9,11,10).

Note that the path from root to an element in a BST is NOT guaranteed to be sorted - if that was what you expected. In fact, it will be sorted only if the node is on the path to the rightest leaf in the tree.


Another issue in the code: searching for 7 (or any element that does not exist in the tree) will give you some bogus path.

The code will only work if the input value is in the tree. When you search for a value that isn't there, you're looking for a node that isn't in the tree

template <class T>
void BST<T>::displayPath(T searchKey, BST<T> *node)
{
    if (searchKey == node->mData)
    {
    cout << node->mData << " ";
    }

    else if (searchKey < node->mData )
    {
        cout << node->mData << " ";
        if (node->mLeft == NULL) // When trying to access a node that isn't there
            cout << "Not Found\n";
        else
        displayPath(searchKey, node->mLeft);
    }
    else // (searchKey > node->mData)
    {
        cout << node->mData << " ";
        if (node->mRight == NULL)
            cout << "Not Found\n";
        else
            displayPath(searchKey, node->mRight);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM