如何从嵌套类中访问指针

How do I access a pointer from within a nested class?

本文关键字:访问 指针 嵌套      更新时间:2023-10-16

此类的目的是效仿二进制搜索树的功能。在以下代码中,我试图将其从结构和一堆功能中调整为称为BST的包装类别。但是,我不确定的一件事是如何从节点结构内访问"根"。目前在BST类中声明root。

class bst
{
public:
struct Node
{
    public:
    int data;
    struct Node *left;
    struct Node *right;

    Node* FindMin(Node* root)
    {
        while(root->left != NULL) root = root->left;
        return root;
    }
    Node* Insert(Node *root,int data)
    {
        if(root == NULL) {
            root = new Node();
            root->data = data;
            root->left = root->right = NULL;
            //Update Height & Size
            bstHeight = 0;
            bstSize = 0;
        }
        else if(data <= root->data)
            root->left = Insert(root->left,data);
        else
            root->right = Insert(root->right,data);
        return root;
    }
    Node* Delete(struct Node *root, int data)
    {
        if(root == NULL) return root;
        else if(data < root->data) root->left = Delete(root->left,data);
        else if (data > root->data) root->right = Delete(root->right,data);
        //Value found
        else {
            // Case 1:  No child
            if(root->left == NULL && root->right == NULL)
            {
                delete root;
                root = NULL;
                //Update Height & Size
                bstHeight = 0;
                bstSize = 0;
            }
            //Case 2: One child
            else if(root->left == NULL)
            {
                struct Node *temp = root;
                root = root->right;
                delete temp;
                //Update Height & Size
                bstHeight = 0;
                bstSize = 0;
            }
            else if(root->right == NULL)
            {
                struct Node *temp = root;
                root = root->left;
                delete temp;
                //Update Height & Size
                bstHeight = 0;
                bstSize = 0;
            }
            // case 3: 2 children
            else
            {
                struct Node *temp = FindMin(root->right);
                root->data = temp->data;
                root->right = Delete(root->right,temp->data);
                //Update Height & Size
                bstHeight = 0;
                bstSize = 0;
            }
        }
        return root;
    }
    //# of Nodes in tree
    void size(Node *root)
    {
        //Check if end
        if(root == NULL) return;
        //Not end
        else
        {
            bstSize = bstSize + 1;
            size(root->left);       //Visit left subtree
            size(root->right);      // Visit right subtree
        }
    }
    void height(Node *root, int temp)
    {
        //Check if end
        if(root == NULL)
        {
            if(temp > bstHeight)
            {
                bstHeight = temp;
            }
            return;
        }
        //Not end
        else
        {
            temp = temp + 1;
            height(root->left, temp);       //Visit left subtree
            height(root->right, temp);      // Visit right subtree
        }
    }
    //Function to visit nodes in Inorder
    void show()
    {
        if(root == NULL) return;
        show(root->left);       //Visit left subtree
        printf("%d ",root->data);  //Print data
        show(root->right);      // Visit right subtree
    }
    void check(Node *root)
    {
        //End of a 'branch'
        if(root == NULL) return;
        int value = 0;
        value = root->data;
        //Checking left subtree
        if(value < root->left->data)
        {
            //Tree is NOT valid
            valid = 0;
        }
        //Checking right subtree
        if(value > root->right->data)
        {
            //Tree is NOT valid
            valid = 0;
        }
        check(root->left);       //Visit left subtree
        printf("%d ",root->data);  //Print data
        //check(root->right);      // Visit right subtree
    }
};
Node* root = NULL;
};

特别是在表演功能中。它并不像用其余功能将其放入节点那样简单,因为root需要是唯一的,而新节点至少被调用一次。节目不会以当前状态进行编译,我不确定从这里继续进行。

尽管评论说明了一切,但让我给出一个其他提示:

当您要保持代码与现在所拥有的代码尽可能相似时,请尝试将构造函数添加到节点类中,该类别期望指向指针或参考(优选(并检查root并检查,每次创建一个节点,您将根部赋予构造函数。

顺便说一句,查看C 中一些基于节点的数据结构实现的方法甚至是一种更好的方法,例如在线程

C 中的简单链接列表