C++:二叉搜索树适用于整数,但在我尝试传递字符串时崩溃

标签 c++ string templates binary-search-tree

<分区>

我发布了 a similar question昨天,当我的双参数二叉搜索树不断崩溃时。

我已经设法让它接受整数作为参数,但是,我真正需要做的是使用字符串作为参数,但每次这样做时我仍然会崩溃。

我将包含插入函数的代码以及我的节点类。

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就像它应该的那样。如果它对整数“有效”,那么您可能只是在某种程度上走运了。

关于C++:二叉搜索树适用于整数,但在我尝试传递字符串时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27233504/

相关文章:

c++ - 禁用 rtti 的后果?

c++ - 在链表中删除

python - 如何使列表接受/n,以便在写入文件时不让所有内容都在同一行?

c++ - 如何将方法指针类型转换为函数指针类型

c++ - 在 C++ 模板中省略空 <>

c++ - 将非特化类作为模板参数传入

C++ std::string 和 NULL const char*

c++ - std::string 到 SecByteBlock 的转换

c - 关于 C 中字符串处理的帮助

c - 我的 strcpy 中的段错误