完全二叉树

#include
#include
using namespace std;
template
struct BTNode
{
T _val;
BTNode* _left;
BTNode* _right;
BTNode(const T& val = T())
:_val(val), _left(nullptr), _right(nullptr)
{
}
};
template
class BSTree
{
public:
typedef BTNode Node;
public:
Node* find(const T& val)
{
Node* cur=_root;
while (cur)
{
if (cur->_val == val)
{
return cur;
}
else if (cur->_val < val)
{
cur=cur->_right;
}
else
{
cur=cur->_left;
}
}
return nullptr;
}
bool insert(const T& val)
{
if (_root == nullptr)
{
_root=new Node(val);
return true;
}
Node* parent=nullptr;
Node* cur=_root;
while (cur)
{
parent=cur;
if (cur->_val == val)
{
return false;
}
else if (cur->_val > val)
{
cur=cur->_left;
}
else
{
cur=cur->_right;
}
}
cur=new Node(val);
if (cur->_val > parent->_val)
{
parent->_right=cur;
}
else
{
parent->_left=cur;
}
return true;
}
void inorder()
{
Node* cur=_root;
look(cur);
}
void look(Node* cur)
{
if (cur == nullptr)
{
return;
}
look(cur->_left);
cout<_val;
look(cur->_right);
}
bool erase(const T& val)
{
if (_root == nullptr)
{
return false;
}
Node* parent;
Node* cur = _root;
while (cur)
{
if (cur->_val == val)
{
break;
}
else if (cur->_val > val)
{
parent = cur;
cur = cur->_left;
}
else
{
parent = cur;
cur = cur->_right;
}
}
if (cur == nullptr)
{
return false;
}
if (cur->_left == nullptr && cur->_right == nullptr)
{
if (cur == _root)
{
_root = nullptr;
}
else if (parent->_left == cur)
{
parent->_left = nullptr;
delete cur;
}
else
{
parent->_right = nullptr;
delete cur;
}
}
else if (cur->_left == nullptr)
{
if (cur == _root)
{
_root = _root->_right;
}
else if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
delete cur;
}
else if (cur->_right == nullptr)
{
if (cur == _root)
{
_root = cur->_left;
}
else if (parent->_left = cur)
{
parent->_left = cur->_left;
}
else if (parent->_right = cur)
{
parent->_right = cur->_left;
}
delete cur;
}
else
{
Node* leftMostNode = cur->_right;
Node* par = cur;
while (leftMostNode->_left)
{
par = leftMostNode;
leftMostNode = leftMostNode->_left;
}
cur->_val = leftMostNode->_val;
if (par->_left == leftMostNode)
{
par->_left = leftMostNode->_right;
}
else
{
par->_right = leftMostNode->_right;
}
delete leftMostNode;
}
}
~BSTree()
{
destroy(_root);
}
void destroy(Node* root)
{
if (root == nullptr)
{
return;
}
destroy(root->_left);
destroy(root->_right);
delete root;
}
BSTree()
{
}
BSTree(const BSTree& bs)
{
_root=copy(bs._root);
}
Node* copy(Node* cur)
{
if (cur == nullptr)
{
return nullptr;
}
Node* root=new Node(cur->_val);
root->_left=copy(cur->_left);
root->_right=copy(cur->_right);
return root;
}
BSTree& operator = (const BSTree bs)
{
swap(_root,bs._root);
return this;
}
private:
Node
_root=nullptr;
};
void test()
{
BSTreebs;
bs.insert(1);
bs.insert(3);
bs.insert(6);
bs.insert(2);
bs.insert(8);
bs.insert(9);
bs.inorder();
BSTreebs2(bs);
cout<<endl;
BSTreebs3=bs;
bs2.inorder();
bs3.inorder();
}
int main()
{
test();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值