You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++ AVL树作业疑问:类的private与public访问权限问题咨询

Hey there, let's break down what's going on with your AVL tree code and the feedback you got about private/public access control—this is a super common OOP pitfall when starting out with C++ classes!

First, the critical syntax bug

You've defined the Dictionary class twice in your code. This will immediately throw a "redefinition of class Dictionary" compile error—you need to delete one of the duplicate class definitions entirely, leaving only a single, complete class.

The core issue with private/public misuse

In C++, class members are private by default, which is correct for your node_t struct and root pointer—these are internal implementation details that external code shouldn't touch directly. The problem lies in how you've set permissions for your member functions:

  • You made dozens of internal helper functions (like getHeight, leftRotate, balancing, and all the deleteXXXNode functions) public. These functions are meant to handle the AVL tree's internal logic (rotations, balancing, node cleanup) and should never be called by external code—this breaks the principle of encapsulation.
  • Even worse: Many of these public functions use node_t* as a parameter or return type. Since node_t is a private nested struct, external code can't see this type at all. Any attempt to call these functions outside the class will fail to compile, because the caller has no way to provide a node_t* argument or use the return value.

How to fix this

Split your class members into two clear categories:

  1. Public Interface: Only keep functions that external code needs to interact with your dictionary. These are the "user-facing" methods like constructors/destructors, addNode, searchNode, deleteNodeByKey, printTree, and deleteTree.
  2. Private Helpers: Move all internal implementation functions (rotations, height calculation, balancing, recursive deletion, node printing) to private. These are for the class's own use only—no external caller needs to know they exist.

Modified code example

Here's your class with corrected access permissions and duplicate definitions removed:

#include <iostream> // Don't forget necessary headers!
using namespace std;

template<typename Key, typename Info>
class Dictionary{
    // Internal node structure (private by default, can explicitly mark private: for clarity)
    struct node_t{
        Key key;
        Info info;
        node_t *left;
        node_t *right;
        node_t(Key KEY, Info INFO, node_t* LEFT, node_t* RIGHT) 
            : key(KEY), info(INFO), left(LEFT), right(RIGHT) { }
    };

    node_t* root;

    // All internal helper functions go here (private)
    int getHeight(node_t *node);
    node_t *leftRotate(node_t *root, node_t *node, node_t *parent, int direction);
    node_t *rightRotate(node_t *root, node_t *node, node_t *parent, int direction);
    node_t *rightLeftRotate(node_t *root, node_t *node, node_t *parent, int direction);
    node_t *leftRightRotate(node_t *root, node_t *node, node_t *parent, int direction);
    node_t *balancing(node_t *root, node_t *node, node_t *parent, int direction, int *branch, int num_branch);
    node_t *deleteNoChildNode(node_t *root, node_t *node, node_t *parent);
    node_t *deleteOneChildNode(node_t *root, node_t *node, node_t * child);
    node_t *deleteTwoChildNode(node_t *root, node_t *node, int *branch, int *num_branch);
    void deleteNodeRecursively(node_t *root);
    void printNode(node_t *root, int depth);

public:
    // User-facing public interface
    Dictionary(){ root = nullptr; }
    ~Dictionary(){ deleteTree(); }
    bool searchNode(Key key);
    void deleteTree();
    bool addNode(Key key, Info info);
    void printTree();
    // Note: Returning node_t* from deleteNodeByKey is useless externally—consider returning bool (success/failure) instead
    bool deleteNodeByKey(Key key); 
};

A quick tweak for deleteNodeByKey

Your current deleteNodeByKey returns a node_t*, but since node_t is private, external code can't do anything with that pointer. A better design is to have it return a bool: true if the node was found and deleted, false if the key didn't exist. This makes the interface much more usable for external callers.

Why this matters

Encapsulation is one of the core ideas of OOP: hiding internal complexity so users only interact with a simple, stable interface. If you later need to tweak your AVL tree logic (like adjusting rotation steps), you can do so without breaking any code that uses your Dictionary class—you just keep the public methods the same. This makes your code more robust and easier to maintain.

内容的提问来源于stack exchange,提问作者Sotaro Suzuki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:47:43