How to search for an object in a binary tree using a string?

  avl-tree, binary-tree, c++

I have an AVL tree and everything in it works, except for searching and I can’t seem to get it right. Could anyone tell me what I’m doing wrong?

The search function should be able to take a string(the name in this case) and find the object in the tree, then return that object.


#ifndef AVLTree_h
#define AVLTree_h

#include <iostream>
#include "Professor.h"

using namespace std;

class AVLTree {
    // TreeNode structure
    struct TreeNode {
        string name;
        Professor *prof;
        struct TreeNode* left;
        struct TreeNode* right;
    // Root of tree
    TreeNode* root;
    // Function declarations
    void insert(TreeNode *&nodePtr, TreeNode *&newNode);
    void destroySubTree(TreeNode *nodePtr);
    void displayInOrder(TreeNode *nodePtr) const;
    int height(TreeNode *nodePtr);
    int diff(TreeNode *nodePtr);
    void balance(TreeNode *&temp);
    TreeNode* l_rotation(TreeNode *parent) {
        TreeNode *temp;
        temp = parent->right;
        parent->right = temp->left;
        temp->left = parent;
        return temp;

    TreeNode* r_rotation(TreeNode *parent) {
        TreeNode *temp;
        temp = parent->left;
        parent->left = temp->right;
        temp->right = parent;
        return temp;

    TreeNode* lr_rotation(TreeNode *parent) {
        TreeNode *temp;
        temp = parent->left;
        parent->left = (l_rotation(temp));
        return r_rotation(parent);

    TreeNode* rl_rotation(TreeNode *parent) {
        TreeNode *temp;
        temp = parent->right;
        parent->right = (r_rotation(temp));
        return l_rotation(parent);
    AVLTree() { // Constructor
        root = NULL;
    ~AVLTree() { // Destructor
    void insertNode(string n, Professor* professor);
    Professor* searchNode(string);
    // Call private displayInOrder()
    void displayInOrder() const {


#endif /* AVLTree_h */

The search function:

Professor* AVLTree::searchNode(string s) {
    TreeNode *nodePtr = root;

    while (nodePtr) {
        if (nodePtr->name == s) {
          return nodePtr->prof;
        else if (s < nodePtr->name) {
            nodePtr = nodePtr->left;
        else {
            nodePtr = nodePtr->right;
    return NULL;

Source: Windows Questions C++