二叉搜索树实现(c++)
Binary Search Tree Implementation(C++)
大部分代码来自Weiss的"c++中的数据结构和算法分析",在我在CodeBlock(gnu,gcc)上键入代码后,编译器报告没有错误,但随后我试图创建一个实例并测试一些函数,它出错了。BST()或insert()似乎有问题,因为程序在运行后立即卡住。有人能帮我找出解决这个问题的方法吗?好谢谢! !
#include <iostream>
using namespace std;
struct TreeNode
{
int element;
TreeNode*left,*right;
TreeNode(const int&e,TreeNode*le=NULL,TreeNode*rt=NULL):element(e),left(le),right(rt){};
};
class BST
{
public:
BST(){root=NULL;};
~BST(){ makeEmpty(root); };
// use public member function to call private member functions.
void insert(const int & x){ insert(x, root); }
void remove(const int & x){ remove(x, root); }
TreeNode*findMin() const{ return findMin(root); }
bool contain(const int & x) const{ return contain(x,root); }
void printNodes() const{ printNodes(root); }
private:
TreeNode*root;
void makeEmpty(TreeNode*&);
TreeNode* findMin(TreeNode*) const;
void insert(const int &, TreeNode*) const;
void remove(const int &, TreeNode*) const;
bool contain(const int &, TreeNode*) const;
void printNodes(TreeNode*) const;
};
void BST::makeEmpty(TreeNode*&t)
{
if(t!=NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
TreeNode* BST::findMin(TreeNode*t) const
{
if(t->left==NULL) return t;
return findMin(t->left);
}
void BST::insert(const int & x, TreeNode* t) const
{
if(t==NULL) t=new TreeNode(x,NULL,NULL);
else if(x < t->element) insert(x,t->left);
else if(x > t->element) insert(x,t->right);
else; /// duplicate, do nothing
}
void BST::remove(const int & x, TreeNode* t) const
{
if(t==NULL) return;
if(t->element > x) remove(x, t->left);
else if(t->element < x) remove(x, t->right);
else if(t->left!=NULL && t->right!=NULL)
{
t->element=findMin(t->right)->element;
remove(t->element,t->right);
}
else
{
TreeNode*oldNode=t;
t=(t->left==NULL)?t->right:t->left;
delete oldNode;
}
}
bool BST::contain(const int & x, TreeNode*t) const
{
if(t==NULL) return false;
else if(x<t->element) return contain(x,t->left);
else if(x>t->element) return contain(x,t->right);
else return true;
}
void BST::printNodes(TreeNode*t) const
{
if(t==NULL) return;
cout<<t->element<<" ";
printNodes(t->left);
printNodes(t->right);
cout<<endl;
};
下面是我写的测试类BST的代码:
int main()
{
BST BinarySearchTree;
int element,node;
for(int i=0; i<5; i++)
{
cin>>element;
BinarySearchTree.insert(element);
}
BinarySearchTree.printNodes();
cout<<BinarySearchTree.findMin()->element<<endl;
cin>>node;
if(BinarySearchTree.contain(node)){ cout<<"item "<<node<<" is in BST."<<endl; }
BinarySearchTree.remove(node);
BinarySearchTree.printNodes();
return 0;
}
类BST
只有一个成员变量。TreeNode*root
。(没有问题)
您的insert
和remove
函数可能修改 BST
,因此您需要修改与root
相关的。
(你的编译器会很快指出这些函数不应该是const
,因为同样的原因。)
void BST::makeEmpty(TreeNode*&t)
{
if(t==NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
这个代码是错误的,如果t是NULL,那么你做t->左和t->右。这就是所谓的解引用NULL指针,很可能使程序立即崩溃。
应该是
void BST::makeEmpty(TreeNode*&t)
{
if (t!=NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
您没有在insert()
中使用root,因此它什么也不做(除了内存泄漏)。
如果您想交叉检查您的解决方案,您可以在这里找到https://siddharthjain094.wordpress.com/2014/07/13/binary-search-tree-implementation-in-ccpp/
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 在C++中,如何在类和函数(可能是模板化的)的头中编写完整的实现
- std::random_device是如何实现的