如何从嵌套类中访问指针?

时间:2018-04-02 03:24:50

标签: c++

该类的目的是模拟二叉搜索树的功能。在下面的代码中,我试图将它从一个struct和一堆函数改编成一个名为BST的包装类。然而,我不确定的一件事是如何从节点结构中访问“root”。 Root目前在BST类中声明。

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;
};

具体来说,在show函数中。它并不像使用其他函数将其放入Node那么简单,因为root需要是唯一的,并且新Node至少被调用一次。 Show不会在当前状态下编译,我不知道从哪里开始。

1 个答案:

答案 0 :(得分:0)

虽然评论说明了所有内容,但我还要提一下:

当你想让你的代码尽可能地与你现有的代码保持相似时,尝试向Node类添加一个构造函数,该构造函数需要一个指针或引用(最好)到root并检查,每次你创建一个节点,您将根提供给构造函数。

顺便说一下,在C ++中查看一些简单的基于Node的数据结构实现甚至可能是更好的方法,例如在线程中

Simple linked list in C++