C++ 尝试将二元搜索树调整为两个参数,导致崩溃(C+;+;)
对于赋值,我需要实现二进制搜索树的几个函数。我得到的似乎是逻辑代码,然而,每当我尝试实现插入节点函数时,我都会遇到程序崩溃。以下是我的插入函数代码:C++ 尝试将二元搜索树调整为两个参数,导致崩溃(C+;+;),c++,string,binary-search-tree,C++,String,Binary Search Tree,对于赋值,我需要实现二进制搜索树的几个函数。我得到的似乎是逻辑代码,然而,每当我尝试实现插入节点函数时,我都会遇到程序崩溃。以下是我的插入函数代码: void insert (K k, V v) { TreeNode<K,V> * treeNode = NULL; TreeNode<K,V> *temp=NULL; TreeNode<K,V> *prev=NULL; temp =
void insert (K k, V v)
{
TreeNode<K,V> * treeNode = NULL;
TreeNode<K,V> *temp=NULL;
TreeNode<K,V> *prev=NULL;
temp = root;
while(temp) //This is the loop that causes a crash, even if I remove the loop.
{
prev = temp;
if (temp->key < treeNode->key)
temp = temp->right;
else
temp = temp->left;
}
if (prev==NULL)
root = treeNode;
else
{
if (prev->key<treeNode->key)
prev->right = treeNode;
else
prev->left = treeNode;
}
}
void插入(K,V)
{
TreeNode*TreeNode=NULL;
TreeNode*temp=NULL;
TreeNode*prev=NULL;
温度=根;
while(temp)//这是导致崩溃的循环,即使我删除了该循环。
{
prev=温度;
如果(临时->按键<树节点->按键)
温度=温度->右侧;
其他的
温度=温度->左侧;
}
if(prev==NULL)
根=三烯醇;
其他的
{
如果(上一个->键)
上->右=树节点;
其他的
上一个->左=树节点;
}
}
我还将包括TreeNode类:
template <class K, class V> class TreeNode
{
public:
TreeNode(K k, V v): key(k), value(v), left(0), right(0) {}
K key;
V value;
TreeNode<K,V> *left;
TreeNode<K,V> *right;
template <class X, class Y> friend std::ostream & operator
<< (std::ostream &s,const TreeNode<X,Y> &t);
};
模板类树节点
{
公众:
树节点(kk,vv):键(K),值(V),左(0),右(0){}
K键;
V值;
TreeNode*左;
TreeNode*对;
模板友元std::ostream和运算符
是否将treeNode
分配到循环之前的任何位置?它看起来像treeNode
是NULL
,因此在条件中取消引用它时会引发异常:
if (temp->key < treeNode->key) // This throws an exception if treeNode is not set
^^^^^^^^^^^^^
if(temp->keykey)//如果未设置treeNode,则会引发异常
^^^^^^^^^^^^^
下面的示例实现,需要C++ 11支持它编译(因为 NulLPTR< /Cult>,只需替换<代码> null <代码>以供C++兼容)。
#包括
使用名称空间std;
模板
标准::奥斯特雷姆和运营部)
温度=温度->右侧;
其他的
温度=温度->左侧;
}
if(prev==nullptr)
根=三烯醇;
否则{
如果(上一个->键)
上->右=树节点;
其他的
上一个->左=树节点;
}
}
};
int main(){
树t;
t、 插入(1,2);
}
我只对K
和V
模板参数使用int
进行了测试,因为它避免了与复制构造函数相关的任何问题。您可能希望所有方法都引用K
和V
而不是纯值
遇到了一个问题:
NULL
initializedtreeNode
变量在insert
代码中可能存在的问题:
- 未初始化
树
类中的根
你的意思可能是:`TreeNode*TreeNode=newtreenode(k,v)`或者类似于插入的第一行中的内容。谢谢各位的回复,但是不要。我用TreeNode*TreeNode=新的TreeNode(k,v)替换了有问题的行;这没有什么区别,在我尝试运行insert()时仍然崩溃。
#include <iostream>
using namespace std;
template <template<class K, class V> class TreeNode, class K, class V>
std::ostream & operator<< (std::ostream &s,const TreeNode<K,V> &t);
template <class K, class V>
class TreeNode
{
public:
typedef TreeNode<K,V> SelfType;
TreeNode(K k, V v): key(k), value(v), left(nullptr), right(nullptr) {}
K key;
V value;
SelfType *left;
SelfType *right;
friend std::ostream & operator<< <>(std::ostream &s,const SelfType &t);
};
template <class K, class V>
struct Tree {
typedef TreeNode<K,V> NodeType;
NodeType *root;
Tree() : root(nullptr){}
void insert (K k, V v)
{
NodeType * treeNode = new NodeType(k,v);
NodeType *temp = nullptr;
NodeType *prev = nullptr;
temp = root;
while(temp) {
prev = temp;
if (temp->key < treeNode->key)
temp = temp->right;
else
temp = temp->left;
}
if (prev==nullptr)
root = treeNode;
else {
if (prev->key<treeNode->key)
prev->right = treeNode;
else
prev->left = treeNode;
}
}
};
int main(){
Tree<int,int> t;
t.insert(1,2);
}