此代码可以正常删除叶子节点,但删除根节点时就出错。
出错截图如下
代码如下
#pragma once
#ifndef BT2
#define BT2
#include <iostream>
#include <queue>
template<class T>
class BT;
template<class T>
class Element
{
public:
Element()
{
key = 0;
}
T key;
};
template<class T>
class BTNode
{
friend class BT<T>;
private:
BTNode()
{
data.key = 0;
left = right = NULL;
}
Element<T> data;
BTNode<T> *left;
BTNode<T> *right;
void display(int i)
{
std::cout << "Position " << i << ": data.key= " << data.key << "\n";
if (left) left->display(2 * i);
if (right) right->display(2 * i + 1);
}
};
//二叉树类
template<class T>
class BT
{
public:
BT(BTNode<T> *init = 0) :root(init) {};
~BT()
{
delete root;
}
void Insert(const Element<T> &x);
void Search(const Element<T> &x);
void InOrder();
void PreOrder();
void PostOrder();
void LevelOrder();
void Delete(const Element<T> &x);
void visit(BTNode<T> *);
void display()
{
std::cout << "\n";
if (root)
{
std::cout << "根为:" << root->data.key << "\n";
root->display(1);
}
else
{
std::cout << "This is a empty tree!" << "\n";
}
}
BTNode<T> *miniMum(BTNode<T> *);
BTNode<T> *RemoveMin(BTNode<T> *);
private:
BTNode<T> *root;
void InOrder(BTNode<T> *a);
void PreOrder(BTNode<T> *a);
void PostOrder(BTNode<T> *a);
BTNode<T> *Delete(BTNode<T> *, const Element<T> &);
};
//二叉树结点类
template<class T>
BTNode<T> *BT<T>::miniMum(BTNode<T> *a)
{
if (a->left == NULL)
{
return a;
}
a->left = miniMum(a->left);
return a;
}
template<class T>
BTNode<T> *BT<T>::RemoveMin(BTNode<T> *a)
{
if (a->left == NULL)
{
BTNode<T> *leftnode1 = a->right;
delete a;
return leftnode1;
}
a->left = RemoveMin(a->left);
return a;
}
template<class T>
void BT<T>::visit(BTNode<T> *a)
{
std::cout << a->data.key << " ";
}
//插入节点
template<class T>
void BT<T>::Insert(const Element<T> &x)
{
BTNode<T> *p = root;
BTNode<T> *q = 0;
while (p)
{
q = p;
if (x.key == p->data.key) return;
else if (x.key < p->data.key)
{
p = p->left;
}
else
{
p = p->right;
}
}
p = new BTNode<T>;
p->data.key = x.key;
if (!root) root = p;
else if (x.key < q->data.key) q->left = p;
else q->right = p;
}
//查找节点
template<class T>
void BT<T>::Search(const Element<T> &x)
{
BTNode<T> *current = root;
bool a = false;
while (current)
{
if (x.key == current->data.key)
{
a = true;
std::cout << "已找到!" << "\n";
break;
}
else if (x.key < current->data.key)
{
current = current->left;
}
else
{
current = current->right;
}
}
if (!a)
{
if (current == root)
{
std::cout << "树为空!" << "\n";
}
else
{
std::cout << "未找到!" << "\n";
}
}
}
//中序遍历
template<class T>
void BT<T>::InOrder()
{
InOrder(root);
}
template<class T>
void BT<T>::InOrder(BTNode<T> *a)
{
if (a->left) InOrder(a->left);
visit(a);
if(a->right) InOrder(a->right);
}
//前序遍历
template<class T>
void BT<T>::PreOrder()
{
PreOrder(root);
}
template<class T>
void BT<T>::PreOrder(BTNode<T> *a)
{
visit(a);
if (a->left) PreOrder(a->left);
if (a->right) PreOrder(a->right);
}
//后序遍历
template<class T>
void BT<T>::PostOrder()
{
PostOrder(root);
}
template<class T>
void BT<T>::PostOrder(BTNode<T> *a)
{
visit(a);
if (a->left) PostOrder(a->left);
if (a->right) PostOrder(a->right);
}
//层序遍历
template<class T>
void BT<T>::LevelOrder()
{
std::queue<BTNode<T> *> b;
BTNode<T> *a = root;
while (a)
{
visit(a);
if (a->left) b.push(a->left);
if (a->right) b.push(a->right);
if (b.empty()) return;
a = b.front();
b.pop();
}
}
//删除节点
template<class T>
void BT<T>::Delete(const Element<T> &x)
{
if (root)
{
root = Delete(root, x);
}
}
template<class T>
BTNode<T> *BT<T>::Delete(BTNode<T> *a, const Element<T> &x)
{
if (a == NULL) return NULL;
if (x.key < a->data.key)
{
a->left = Delete(a->left, x);
return a;
}
else if (x.key > a->data.key)
{
a->right = Delete(a->right, x);
return a;
}
else
{
if (a->left == NULL)
{
BTNode<T> *rightnode = a->right;
delete a;
return rightnode;
}
if (a->right == NULL)
{
BTNode<T> *leftnode = a->left;
delete a;
return leftnode;
}
}
BTNode<T> *dela = a;
BTNode<T> *successor = miniMum(a->right);
successor->left = a->left;
successor->right = RemoveMin(a->right);
delete dela;
return successor;
}
#endif // !BT2
#include "树2.h"
int main()
{
Element<int> a, b, c, d, e, f;
a.key = 5;
b.key = 8;
c.key = 12;
d.key = 1;
e.key = 3;
f.key = 24;
BT<int> m;
m.Insert(a);
m.Insert(b);
m.Insert(c);
m.Insert(d);
m.Insert(e);
m.Delete(a);**//删除b节点时正常运行,删除根节点(a)时出错!**
m.display();
return 0;
}