Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:34:43+00:00 2026-06-09T00:34:43+00:00

I posted a question related to this topic earlier, but I’m having trouble figuring

  • 0

I posted a question related to this topic earlier, but I’m having trouble figuring this out as well. I’m trying to search an unordered binary tree by a key value and return its associated value through a recursive function.

The class has the form:

  Class Node
  {
     private:
       Node *leftChild;
       Node *rightChild;
       int key;
       int value;
 }

With each variable having associated get methods. So I basically want to search through the binary tree and return its value once I get to the correct Node.

Here is my attempt thus far, I think I’m pretty close:

int preOrder(Node *node, int key)
{
   if(node->getKey() == key)
     return node->getValue();

  Node* leftNode = node->getLeft();

  if(leftNode != NULL)
  {
    return preOrder(leftNode, key);
  }

  Node* rightNode = node->getRight();

  if(rightNode != NULL)
  {
    return preOrder(rightNode, key);
  }

  //I know a return statement needs to be placed here
  //in case both pointers are NULL in order to return to the previous
  //node in the tree, but I'm not sure how to do this...
}

Does anyone have any advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:34:45+00:00Added an answer on June 9, 2026 at 12:34 am

    Here you go. This includes the code to answer your last question, modified to support Key/Value nodes instead of just Value nodes. Also, with the changes it made sense to return a pointer to the node rather than the value it contained, so I updated lowest to do it that way as well.

    template <typename KeyT, typename ValueT>
    class Node
    {
    public:
        Node(KeyT k, ValueT v)
        {
            key = k;
            value = v;
            right = NULL;
            left = NULL;
        }
    
        Node<KeyT, ValueT> * lowest()
        {
            Node<KeyT, ValueT> * v = this;
    
            if (right != NULL)
                if (v->value > left->value) v = left;
            if (left  != NULL)
                if (v->value > right->value) v = right;
    
            return v;
        }
    
        Node<KeyT, ValueT> * searchByKey(KeyT k)
        {
            if (key == k)
                return this;
    
            Node<KeyT, ValueT> * n = NULL;
    
            if (left != NULL)
                n = left->searchByKey(k);
            if (n != NULL) return n;
            if (right!= NULL)
                n = right->searchByKey(k);
            if (n != NULL) return n;
    
            return NULL;
        }
    
        Node<KeyT, ValueT> * getRight()
        {
            return right;
        }
    
        Node<KeyT, ValueT> * getLeft()
        {
            return left;
        }
    
        void setRight(Node<KeyT, ValueT> * nright)
        {
            right = nright;
        }
    
        void setLeft(Node<KeyT, ValueT> * nleft)
        {
            left = nleft;
        }
    
        KeyT getKey()
        {
            return key;
        }
    
        ValueT getValue()
        {
            return value;
        }
    
    private:
        KeyT   key;
        ValueT value;
    
        Node<KeyT, ValueT> * right;
        Node<KeyT, ValueT> * left;
    };
    

    Look at example output: http://ideone.com/l5ZNc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a related question to one I posted earlier... I'm trying to sum
This is a related question to one I posted earlier today, I was initially
This is somewhat related to the question posed in this question but I'm trying
This question is related to another question that I posted on SO earlier How
in a related question I have posted this code. It almost works, but the
I had posted before a question related to this, but the solutions didn´t solve
This is related to a question i posted earlier.I am still stuck on it.Heres
This question is related to another question I just posted . I'm prepping for
I posted a related question to no response but here I'll be less narrow.
I posted a related but still different question regarding Protobuf-Net before, so here goes:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.