制作一个n个子目录树来存储计算机的目录
Making a tree of n children to store directories of computer
我正在制作一个由n个子项组成的树来存储计算机的目录。现在,概念是简单地创建一个树(当然这不是BT(,每个节点也将有子节点。考虑下面的代码,然后我将解释问题。首先考虑一下:
C/users/DeadCoder/Movies/Batman.
现在在我的main.cpp
中,我在一个向量中有所有的C、用户、DeadCoder、电影、蝙蝠侠,然后我在插入Func中发送两对。如果CCD_ 2;它只会插入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;
}
};
//在.cpp文件中;//初始化
template <class T>
tree<T>::tree() // Constructor Initialization.
{
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....
问题:已插入C
。插入Users
。现在,在第80行的搜索功能中,可以找到C的子项,它应该是Users,因为我已经将它插入了第40行。但相反,它说child==NULL。我已经调试了好几个小时了,我不知道为什么它会这么说。我希望每个人都能解决这个问题。现在我真的需要知道为什么关于C的child是NULL,它必须是用户。有人能看到问题出在哪里吗????救命!!!!
第42行什么都不做(我的意思是它没有副作用(。它只是把一个值放在一个临时变量中,然后离开。您可能希望您的temp
是对根的引用。类似于:Node<T> *&temp =
你确定insert
方法真的插入了这些元素吗?实现后置条件可能有助于验证您的方法是否真的履行了它们的约定(按约定设计(。通过这种方式,您将直接了解问题所在,在某些情况下,调试将是快速的或不必要的,因为您会收到日志消息,上面写着"此方法本应执行此操作,但未能执行",否则您将在数小时内查找问题的根源。
相关文章:
- 将字符串存储在c++中的稳定内存中
- std::原子加载和存储都需要吗
- C++:将控制台输出存储在宏中更好吗
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 如何从存储在std::映射中的std::集中删除元素
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 带结构的二维矢量:如何存储元素
- 如何将捕获的图像存储在OpenCV中(将图片保存到计算机)
- C++:了解我的计算机如何存储字节
- 'this'指针存储在计算机内存中的什么位置?
- 为什么我的 64 位计算机不能在 C++ 中存储双精度数字 2^1024?
- 制作一个n个子目录树来存储计算机的目录