-1

I pulled this code from a previous program of mine where I was using AVL Trees without using a struct. I think the problem is how I am using the struct when using getLeft() and getRight().

#ifndef BINARYTREE_H
#define BINARYTREE_H

#include <iostream>
using namespace std;


class BinaryTree
{
private:
    struct TreeNode
    {
        string name;
        int krabbyPatty;
        TreeNode* left;
        TreeNode* right;
    };
    
    TreeNode* root;
    
    // Private member functions
    void insert(TreeNode *&, TreeNode *&);
    void destroySubTree(TreeNode *);
    //void deleteNode(string n, TreeNode *&);
    //void makeDeletion(TreeNode *&);
    void displayInOrder(TreeNode *) const;
    void display(TreeNode *ptr, int level);
    void getTotal(TreeNode *nodePtr, int &total);
    void getMost(TreeNode *nodePtr, string& n, int& mostPatties);
    void getLeast(TreeNode *nodePtr, string& n, int& leastPatties);

I am getting errors right here saying expression must be a modifiable lvalue for left and right.

public:
    // Constructor
    BinaryTree()
    { 

        root = NULL; 
        left = NULL;
        right = NULL;
    }

    // Destructor
    ~BinaryTree()
    { 
        destroySubTree(root); 
    }

    // Binary tree operations
    void insertNode(string, int);
    int searchNode(string);
    //void remove(string);

    void displayInOrder() const
    {  
        displayInOrder(root); 
    }
    
    void getLeastNumPatties(string&, int&);
    void getMostNumPatties(string&, int&);
    int getTotalNumPatties();
    void displayTree();

The other errors are right here, I assume I am handling the struct wrong.

    TreeNode* getLeft()
    {
        return left;
    }
    TreeNode* getRight()
    {
        return right;
    }
    void setLeft(TreeNode* nodePtr)
    {
        left = nodePtr;
    }
    void setRight(TreeNode* nodePtr)
    {
        right = nodePtr;
    }

};

#endif
  • 1
    It's a bit hard to tell because of the incomplete code. But it looks to me that the problem is that you declared left and right in the `TreeNode` struct, but your code assumes that they are in the `BinaryTree`. You should ask yourself what does it mean to get the 'left' of a tree? Seems pretty meaningless to me. – john Nov 14 '20 at 17:55
  • left and right now belong to TreeNode and should be handled by its constructor (or inlined). – Surt Nov 14 '20 at 18:00
  • I am using the get left and right for rotation functions `rl_rotation(TreeNode *parent) { TreeNode *temp; temp = parent->getRight(); parent->setRight(r_rotation(temp)); return l_rotation(parent); }` –  Nov 14 '20 at 18:01

1 Answers1

0

Welcome to a new level

** Program complexity increase by one **

You must now split the code in the separate concerns of TreeNode and BinaryTree, so does it make sense that the user calls setLeft or should BinaryTree do that and who should actually do it Tree or Node? The code you present should be a member of TreeNode as it doesn't have two pointers, a parent and the new node.

void TreeNode::setLeft(TreeNode* nodePtr) {
    left = nodePtr;
}

So now this should work

auto rl_rotation(TreeNode *parent) {    
  TreeNode *temp;     
  temp = parent->getRight();     
  parent->setRight(r_rotation(temp));     
  return l_rotation(parent); 
}
Surt
  • 15,501
  • 3
  • 23
  • 39