C++:二叉搜索树适用于整数,但当我尝试传递字符串时崩溃
C++: Binary search tree works for integers but crashes when I try and pass strings
我昨天发布了一个类似的问题,当时我的双参数二叉搜索树不断崩溃。
从那以后,我设法让它接受整数作为参数,但是,我真正需要做的是使用字符串作为参数,每次这样做时我仍然崩溃。
我将包括插入函数的代码以及我的节点类。
void insert (K k, V v) {
TreeNode<K,V> * treeNode = new TreeNode<K,V> (k,v);
TreeNode<K,V> *temp=NULL;
TreeNode<K,V> *prev=NULL;
temp = root;
while(temp) { // crashes in this loop, even if I remove loop and have it only activate once
prev = temp;
if (temp->key < treeNode->key) //MARKER
temp = temp->right; //MARKER
else
temp = temp->left;
}
if (prev==NULL)
root = treeNode;
else {
if (prev->key<treeNode->key)
prev->right = treeNode;
else
prev->left = 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);
};
谁能发现我在这里做错了什么?
如建议:完整代码的简化版本。
/* bst.h */
#ifndef __CSC116__BST_H__
#define __CSC116__BST_H__
#include <list>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
using namespace std;
unsigned int heightCount;
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);
};
// TreeNodes can output themselves to streams
template <class K, class V> std::ostream & operator << (std::ostream &s, const TreeNode<K,V> &n)
{
s << """ << n.key << ":" << n.value << """;
return s;
}
class key_not_found_exception { //Currently inactive
};
template <class K, class V> class BinarySearchTree {
public:
//
// Constructor
//
BinarySearchTree ()
{
sizeCount = 0;
}
~BinarySearchTree()
{
}
void insert (K k, V v) {
TreeNode<K,V> * treeNode = new TreeNode<K,V> (k,v);
TreeNode<K,V> *temp=NULL;
TreeNode<K,V> *prev=NULL;
temp = root;
while(temp) { // crashes in this loop, even if I remove loop and have it only activate once
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;
}
}
bool isEmpty() const
{
return root == NULL;
}
unsigned int size()
{
cout << sizeCount; //Currently inactive
return 0;
}
private:
unsigned int doHeight (TreeNode<K,V> *t) //Currently inactive
{
return -1;
}
void doDelete (TreeNode<K,V> * n )
{
}
TreeNode<K,V> *root;
unsigned int count;
unsigned int sizeCount;
template <class X, class Y> friend class tree_view;
template <class X, class Y> friend std::ostream & operator << (std::ostream &s, const
BinarySearchTree<X,Y> &t);
};
template <class K,class V> void do_inorder (std::ostream &s, const TreeNode<K,V> *n)
{
if (!n)
return;
do_inorder(s,n->left);
s << n->key << ":" << n->value << " ";
do_inorder(s,n->right);
}
// Output the tree to a stream by doing an in-order traversal
template <class K, class V> std::ostream & operator << (std::ostream &s, const
BinarySearchTree<K,V> &t)
{
s << "{ ";
do_inorder(s,t.root);
s << "}";
return s;
}
#endif
/bst_tester.cpp/
// bst_tester.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "bst.h"
#include "tree_view.h"
using namespace std;
class bst_tester_exception
{
public:
bst_tester_exception (const string & msg, unsigned int line) : _msg(msg), _line(line) {}
string what() const
{
ostringstream s;
s << _line;
return _msg + " line number: " + s.str();
}
private:
bst_tester_exception();
string _msg;
unsigned int _line;
};
void test_insert_size_height()
{
BinarySearchTree<string,string> t;
if (t.height() != 0)
throw bst_tester_exception(__func__,__LINE__);
if (t.size() != 0 )
throw bst_tester_exception(__func__,__LINE__);
t.insert("bob", "bobdata");
t.insert("abe", "abedata");
t.insert("jane", "janedata");
if (t.height() != 2)
throw bst_tester_exception(__func__,__LINE__);
if (t.size() != 3 )
throw bst_tester_exception(__func__,__LINE__);
}
int main ()
{
unsigned int tests_passed = 0;
try
{
test_insert_size_height();
tests_passed++;
}
catch (bst_tester_exception &e)
{
cout << "Failed test case: " << e.what() << std::endl;
}
catch (...)
{
cout << "Caught unhandled exception." << std::endl;
}
cout << "Passed: " << tests_passed << endl;
return tests_passed;
}
您
永远不会在二叉搜索树中初始化root
,因此当您第一次进入insert
时,root
不会像应有的那样设置为 NULL。 如果它对整数"有效",你可能只是在某种程度上很幸运。
相关文章:
- 为什么我的结构在包含字符串时崩溃?
- 当 getline 用于获取整个字符串时,程序崩溃
- 删除动态字符串时程序崩溃
- 当 JSON 值应为 INT 但以字符串类型发送时,RapidJSON 崩溃
- 从数组比较2个字符串时,程序崩溃
- C 通过char读取文件字符串的文件char;崩溃
- 使用字符串而不是字符数组时程序崩溃
- 返回向量<对<字符串,字符串>>会导致 gcc 编译代码 (curlcpp) 崩溃
- 矢量尝试添加字符串时会崩溃
- 从CSV文件读取时,试图将值插入一个字符串中时崩溃
- 清理所拥有的(!)字符串成员时,析构函数偶尔崩溃
- 程序使用SCANF读取字符串时会崩溃
- 在执行字符串时更改字符会使程序崩溃
- 程序在输入 10 个字符串后崩溃
- o字符串流对象创建导致我的程序崩溃
- p = new String [0]和p = new Int [0]之后,为什么当删除[] p时字符串版本崩溃
- C++:使用 LSD 基数排序字符串排序崩溃
- jvm在尝试将TCHAR类型发送到接受字符串作为参数的java函数时崩溃
- Google protobuf在解析字符串数据时在Android中崩溃
- 崩溃:字符串问题