如何从嵌套类中访问指针
How do I access a pointer from within a nested class?
此类的目的是效仿二进制搜索树的功能。在以下代码中,我试图将其从结构和一堆功能中调整为称为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 中的简单链接列表
相关文章:
- C++ - 循环访问指针数组会导致错误
- 如何访问指针结构的成员变量?
- 从子线程访问指针
- 如何在C++中使用类对象访问指针数据成员
- 如何使用二维语法访问指针
- 我可以保证以负偏移访问指针吗?
- 是否访问指针元组和互斥锁线程安全
- 如何从嵌套类中访问指针
- C++ 如何访问指针的结构变量
- 从地图擦除后访问指针
- 会员在共享_ptr上访问指针
- 删除后访问指针
- 为什么在递增后使用 [] 运算符访问指针数组会返回错误地址的当前内存位置
- 如何在python脚本中使用Pybind11修改/访问C++指针
- 无法从另一个函数访问指针数组
- 无法访问指针C 的值
- 访问指针类成员 (C++)
- 访问指针数组中的子类方法
- 访问指针到结构中的向量
- 访问指针类型的静态属性