我目前正在开发一个二进制搜索树的实现,但是当我尝试删除带有双子节点的节点时,我遇到了错误。但这不是分段错误。
错误导致中止delete curr
所以我的意思是,它一直沿着案例并通过重新分配节点,然后当我尝试删除它时就会中止。想法?这是我的BST实施
编辑:
后发生错误cout << "here2" << endl;
删除中的
#ifndef CTREE_H
#define CTREE_H
#include <iostream>
template<class T> class CTree
{
public:
CTree() {m_root = 0;}
~CTree() {delete m_root;}
//
// class Node
// The nested node class for objects in our tree.
//
class Node
{
public:
friend class CTree;
Node() {m_ll = 0; m_rl = 0;m_active = true;}
Node(const T &t) {m_payload = t; m_ll = 0; m_rl = 0;}
~Node() {delete m_ll; delete m_rl;}
T &Data() {return m_payload;}
const T &Data() const {return m_payload;}
private:
Node *m_ll;
Node *m_rl;
T m_payload;
bool m_active;
};
/********************
/********************
*
* Insert an element
*
* ******************/
void Insert(const T &t) {
Node * tree = new Node;
Node * parent = new Node;
tree->m_payload = t;
if(m_root == NULL){
// cout << "creating root" << endl;
m_root = tree;
tree->m_ll=tree->m_rl = NULL;
}
else{
// cout << "else insert" << endl;
Node * curr;
curr = m_root;
while(curr){
parent = curr;
if(tree->Data() > curr->Data())
curr = curr->m_rl;
else
curr = curr->m_ll;
}
if (tree->Data() < parent->Data())
parent->m_ll = tree;
else
parent->m_rl = tree;
}
}
/**************************************
*
* put data from tree in preorder form
*
***********************************/
void Preorder(std::ostream &p_str) {
Node * curr = new Node;
curr = m_root;
preorder(curr, p_str);
}
/***********************
*
* Find Depth of Tree
*
* ********************/
int Depth() {
Node * curr = new Node;
curr = m_root;
int depth = maxdepth(curr);
}
/*****************************
*
* find node with given payload
*
* ***************************/
const Node *Find(const T &t) const {}
Node *Find(const T &t) {
Node * curr = new Node;
Node * parent = new Node;
curr = m_root;
bool flag = false;
while(curr != NULL) //if empty it will just skip this step
{
if(curr->Data() == t){
flag = true; //found
break;
}
else{
parent = curr;
if( curr->Data() < t) {
curr = curr->m_rl;
}
else{
curr = curr->m_ll;
}
}
}
if(flag == true){
return curr;
}
else{
Node * temp = new Node;
temp = NULL;
return temp; //not found return blank node
}
}
/****************************
*
* Delete an element without loss of parents
*
* ************************/
void Delete(const T &t)
{
Node * curr = new Node;
Node * parent = new Node;
curr = m_root;
bool flag = false;
while(curr != NULL) //if empty it will just skip this step
{
if(curr->Data() == t){
flag = true; //found
break;
}
else{
parent = curr; //set new parent
if( curr->Data() < t) {
curr = curr->m_rl; //traverse right
}
else{
curr = curr->m_ll; //traverse left
}
}
}
if(curr != NULL){
//delete single leaf node
if(curr->m_ll == NULL && curr->m_rl == NULL){
if(parent->m_ll == curr){
delete curr;
parent->m_ll = NULL;
return;
}
else{
delete curr;
parent->m_rl = NULL;
return;
}
}
//single child
if((curr->m_ll == NULL && curr->m_rl != NULL) || ((curr->m_ll == NULL && curr->m_rl != NULL))){
//curr is left node of parent
if(parent->m_ll == curr){
parent->m_ll = curr->m_rl;
delete curr;
return;
}
//curr is right node of parent
else{
parent->m_rl = curr->m_rl;
delete curr;
return;
}
}
//two children
if((curr->m_ll != NULL && curr->m_rl != NULL)){
cout << "two children" << endl;
//two cases curr is m_ll of parent and curr is m_rl of parent
if(parent->m_ll == curr){
cout << "left child" << endl;
//check if curr->m_rl has child node if not, easy
if(curr->m_rl->m_rl == NULL){
//good
cout << "Good" << endl;
parent->m_ll = curr->m_rl;
cout << "here" << endl;
curr->m_rl->m_ll = curr->m_ll;
cout << "here2" << endl;
delete curr;
return;
}
else{
Node * temp = new Node;
temp = curr->m_rl;
//find least child of curr's left child
while(temp->m_ll != NULL){
temp = temp->m_ll;
}
//connect temp to curr's left child
temp->m_ll = curr->m_ll;
parent->m_ll = temp;
return;
}
}
}
else{
//check if curr->m_ll has child node if not, easy
if(curr->m_ll->m_rl == NULL){
//good
parent->m_rl = curr->m_ll;
curr->m_ll->m_rl = curr->m_rl;
delete curr;
}
else{
Node * temp = new Node;
temp = curr->m_ll;
while(temp->m_rl != NULL){
temp = temp->m_rl;
}
temp->m_rl = curr->m_rl;
parent->m_rl = temp;
}
}
}
else{
// cout << "No Node found " << endl;
return;
}
}
我要包含我的驱动程序和输出
驱动:
using namespace std;
#include <iostream>
#include "Tree.h"
int main(){
CTree<int> x;
x.Insert(4);
x.Insert(5);
x.Insert(3);
x.Insert(40);
x.Insert(35);
x.Insert(37);
x.Insert(34);
x.Preorder(cout);
x.Delete(35);
x.Preorder(cout);
}
来自驱动程序的输出:
4 3 5 40 35 34 37 two children
left child
Good
here
here2
*** glibc detected *** a.out: double free or corruption (fasttop): 0x00000000016d6190 ***
======= Backtrace: =========
lib/libc.so.6(+0x71e16)[0x7f33efa75e16]
/lib/libc.so.6(cfree+0x6c)[0x7f33efa7ab8c]
a.out[0x401179]
a.out[0x40118e]
a.out[0x401012]
a.out[0x400aac]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f33efa22c8d]
a.out[0x4008f9]
======= Memory map: ========
Abort