C++ 用n个子树存储计算机目录
我正在制作一个n个子目录树来存储计算机的目录。现在,概念是简单地创建一棵树(当然不是BT),每个节点也将有子节点。考虑下面的代码,然后我会解释这个问题。 首先考虑这一点:C++ 用n个子树存储计算机目录,c++,data-structures,tree,C++,Data Structures,Tree,我正在制作一个n个子目录树来存储计算机的目录。现在,概念是简单地创建一棵树(当然不是BT),每个节点也将有子节点。考虑下面的代码,然后我会解释这个问题。 首先考虑这一点: C/users/DeadCoder/Movies/Batman. 现在,在我的main.cpp中,我将所有的C、用户、死编码器、电影、蝙蝠侠放在一个向量中,然后我在insert Func中发送两对。如果root==NULL;它只需插入C。下次C和用户将离开。它会找到C,然后偶尔插入用户。现在让我们看看代码 template
C/users/DeadCoder/Movies/Batman.
现在,在我的main.cpp
中,我将所有的C、用户、死编码器、电影、蝙蝠侠放在一个向量中,然后我在insert Func中发送两对。如果root==NULL
;它只需插入C。下次C和用户将离开。它会找到C,然后偶尔插入用户。现在让我们看看代码
template <class T>
struct Node;
template <class T>
class tree
{
Node<T> *root;
public:
tree();
~tree();
int insert(T str, T str1);
Node<T> *getRoot();
Node<T> *search(T item, Node<T> *tempPtr);
};
template <class T>
struct Node{
T n;
Node<T> *sibling;
tree<T> children; // SEE my each node has children.
Node(T N){
this->n = N;
this->sibling = NULL;
}
};
模板
结构节点;
模板
类树
{
节点*根;
公众:
树();
~tree();
int插入(T-str,T-str1);
Node*getRoot();
节点*搜索(T项,节点*临时PTR);
};
模板
结构节点{
T n;
节点*兄弟;
树状子节点;//查看我的每个节点都有子节点。
节点(tn){
这个->n=n;
此->同级=空;
}
};
//在.cpp文件中;
//起爆剂
template <class T>
tree<T>::tree() // Constructor Initialization.
{
root=NULL;
}
模板
tree::tree()//构造函数初始化。
{
root=NULL;
}
//插入函数
template <class T>
int tree<T>::insert(T push, T find)
{
Node<T> *rPtr = root;
if (rPtr==NULL){
//ROOT is NULL. C needs to be inserted which is in find.
Node<T> *pusPtr = new Node<T>(find);
root = pushPtr;
root->sibling=NULL;
return 0;
}
else if(rPtr!=NULL){
Node<T> *pushPtr = new Node<T>(push);
Node<T> *temp2 = search(find, root);
Node<T> *temp = temp2->children.getRoot(); // say it LINE_40.
if (temp==NULL){
temp = pushPtr;
temp->sibling=NULL;
return 1;
}
// children are already present.
else if(temp!=NULL){
// You don't need to know code for this part.
}
}//if.
template <class T>
Node<T> *tree<T>::search(T data, treeNode<T>* N)
{
if (N->n==data){ // where n represent directory.
return N; // data found.
}//if....
else{
Node<T> *child = N->children.getRoot();
// This is where i get Segmentation fault,
// because child is ==NULL; but you see in LINE_40 I did insert the child for C.
if(child!=NULL){ // say it line 80.
search(data, child);
}//if...
if(child->sibling!=NULL){
search(data, child->sibling);
}
}
}// search....
模板
int-tree::insert(T-push,T-find)
{
节点*rPtr=根;
如果(rPtr==NULL){
//根为空。需要插入查找中的C。
节点*pusPtr=新节点(查找);
根=pushPtr;
根->同级=空;
返回0;
}
else if(rPtr!=NULL){
节点*pushPtr=新节点(推送);
节点*temp2=搜索(查找,根);
Node*temp=temp2->children.getRoot();//说第40行。
如果(temp==NULL){
温度=pushPtr;
temp->sibling=NULL;
返回1;
}
//孩子们已经在场了。
否则如果(温度!=NULL){
//您不需要知道此部件的代码。
}
}//如果。
}
//搜索功能
template <class T>
int tree<T>::insert(T push, T find)
{
Node<T> *rPtr = root;
if (rPtr==NULL){
//ROOT is NULL. C needs to be inserted which is in find.
Node<T> *pusPtr = new Node<T>(find);
root = pushPtr;
root->sibling=NULL;
return 0;
}
else if(rPtr!=NULL){
Node<T> *pushPtr = new Node<T>(push);
Node<T> *temp2 = search(find, root);
Node<T> *temp = temp2->children.getRoot(); // say it LINE_40.
if (temp==NULL){
temp = pushPtr;
temp->sibling=NULL;
return 1;
}
// children are already present.
else if(temp!=NULL){
// You don't need to know code for this part.
}
}//if.
template <class T>
Node<T> *tree<T>::search(T data, treeNode<T>* N)
{
if (N->n==data){ // where n represent directory.
return N; // data found.
}//if....
else{
Node<T> *child = N->children.getRoot();
// This is where i get Segmentation fault,
// because child is ==NULL; but you see in LINE_40 I did insert the child for C.
if(child!=NULL){ // say it line 80.
search(data, child);
}//if...
if(child->sibling!=NULL){
search(data, child->sibling);
}
}
}// search....
模板
节点*树::搜索(T数据,树节点*N)
{
如果(N->N==data){//,其中N表示目录。
返回N;//找到数据。
}//如果。。。。
否则{
Node*child=N->children.getRoot();
//这就是我犯错误的地方,
//因为child=NULL;但是您可以看到,在第40行中,我插入了C的child。
如果(child!=NULL){//说它第80行。
搜索(数据、儿童);
}//如果。。。
如果(子->同级!=NULL){
搜索(数据,子->同级);
}
}
}//搜索。。。。
问题:C
已插入<代码>用户已插入。现在,在第80行的搜索功能中,它来查找C的子项。它应该是用户,正如我在第40行中插入的那样。但它却表示child==NULL。我已经调试了几个小时,我不知道为什么它会这么说。我希望每个人都明白这个问题。
现在我真的需要知道为什么C child是空的,它必须是用户。有人能看出问题出在哪里吗????救命啊 第42行没有任何作用(我的意思是它没有副作用)。它只是将一个值放入一个临时变量,然后离开。
您可能希望您的temp
成为根目录的引用。类似于:Node*&temp=
第42行没有任何作用(我的意思是它没有副作用)。它只是将一个值放入一个临时变量,然后离开。
您可能希望您的
temp
成为根目录的引用。类似于:Node*&temp=
您确定insert
方法实际插入了这些元素吗?
实现后条件可能会有所帮助,以便验证您的方法是否真正履行了它们的约定(按约定设计)。
通过这种方式,您将直接得到错误信息,并且在某些情况下调试将很快或不必要,因为您将收到日志消息说“此方法本应执行此操作,但执行失败”,否则您将查找问题的根源。您确定
insert
method确实插入了这些元素吗?
实现后条件可能会有所帮助,以便验证您的方法是否真正履行了它们的约定(按约定设计)。
通过这种方式,您将直接得到错误信息,在某些情况下,调试将很快或不必要,因为您将收到日志消息,上面会显示“此方法本应执行此操作,但执行失败”,否则您将在数小时内查找问题出在何处。仍在等待他人响应。。。。。。。。。。。!!!!!!!仍在等待有人回应。。。。。。。。。。。!!!!!!!如何使其引用根。我也认为它可能不起作用。你能把密码贴出来吗。我只是一个学习者。我不太明白你说的话事实上,你的代码中有很多东西需要澄清,很难给你指明方向。例如,您的“insert”:如果您希望它在其他地方而不是在子元素上起作用,那么您应该为它提供一个路径,而不仅仅是一个元素。此外,您的方法具有非常通用的名称和参数,如“str”、“str1”。这违背了任何命名逻辑,它们的目的是什么?如果没有明确的名称,没有人能够真正提供帮助。这就是为什么你的代码也很难处理,当你重新阅读它的真正功能时,你无法理解自己。因此,请澄清您的代码,您的bug将告诉您如何将其引用到root。我也认为它可能不起作用。你能把密码贴出来吗。我只是一个学习者。我不太明白你说的话事实上,你的代码中有很多东西需要澄清,很难给你指明方向。例如,您的“insert”:如果您希望它在其他地方而不是在子元素上起作用,那么您应该为它提供一个路径,而不仅仅是一个元素。此外,您的方法具有非常通用的名称和参数,如“str”、“str1”。这违背了任何命名逻辑,它们的目的是什么?如果没有明确的名称,没有人能够真正提供帮助。这就是为什么你的代码也很难处理,当你重新阅读它的真正功能时,你无法理解自己。所以,澄清你的代码,你的bug就会消失