C++ 使用智能指针实现AVL树第2部分
我在上发了一篇类似的帖子,但没有得到任何有用的反馈。因此,我尝试重新编写代码,看看这是否会产生任何好的结果。到目前为止,我的代码可以编译,但没有打印任何东西,我不知道为什么 我在int main()中有一个例子,它应该给我一棵树,看起来像:C++ 使用智能指针实现AVL树第2部分,c++,c++11,avl-tree,C++,C++11,Avl Tree,我在上发了一篇类似的帖子,但没有得到任何有用的反馈。因此,我尝试重新编写代码,看看这是否会产生任何好的结果。到目前为止,我的代码可以编译,但没有打印任何东西,我不知道为什么 我在int main()中有一个例子,它应该给我一棵树,看起来像: /* The constructed AVL Tree would be 9 / \ 1 10 / \ \ 0 5 11 / / \ -1 2 6
/* The constructed AVL Tree would be
9
/ \
1 10
/ \ \
0 5 11
/ / \
-1 2 6
*/
这是我的密码:
#include <algorithm>
#include <iostream>
#include <memory>
#include <utility>
#include <stack>
#include <queue>
struct Node {
int data;
int height;
std::unique_ptr<Node> left = nullptr;
std::unique_ptr<Node> right = nullptr;
Node(const int& x, const int& y, std::unique_ptr<Node>&& p = nullptr, std::unique_ptr<Node>&& q = nullptr) :
data(x),
height(y),
left(std::move(p)),
right(std::move(q)) {}
Node(const int& data) : data(data) {}
};
std::unique_ptr<Node> root = nullptr;
int height(std::unique_ptr<Node>& root) {
if (!root) return 0;
return root->height;
}
void fixHeight(std::unique_ptr<Node>& root) {
auto h1 = height(root->left);
auto h2 = height(root->right);
root->height = (h1 > h2 ? h1 : h2) + 1;
}
void rightRotate(std::unique_ptr<Node>& p) {
std::unique_ptr<Node> q = std::move(p->left);
p->left = std::move(q->right);
q->right = std::move(p);
fixHeight(p);
fixHeight(q);
}
void leftRotate(std::unique_ptr<Node>& q) {
std::unique_ptr<Node> p = std::move(q->left);
q->right = std::move(p->left);
p->left = std::move(q);
fixHeight(q);
fixHeight(p);
}
int heightDiff(std::unique_ptr<Node>& root) {
if (!root) return 0;
return height(root->left) - height(root->right);
}
void balance(std::unique_ptr<Node>& root) {
fixHeight(root);
if (heightDiff(root) == 2) {
if (heightDiff(root->right) < 0)
rightRotate(root->right);
leftRotate(root);
}
if (heightDiff(root) == -2) {
if (heightDiff(root->left) > 0)
leftRotate(root->left);
rightRotate(root);
}
}
void insert(std::unique_ptr<Node>& root, const int& theData) {
std::unique_ptr<Node> newNode = std::make_unique<Node>(theData);
if (!root) {
root = std::move(newNode);
return;
}
if (theData < root->data)
insert(root->left, theData);
else
insert(root->right, theData);
balance(root);
}
auto findMin(std::unique_ptr<Node>& root) {
while (root->left != nullptr) root = std::move(root->left);
return root.get();
}
void deleteNode(std::unique_ptr<Node>& root, const int& theData) {
// Step 1: Perform regular deletion for BST
if (!root) return;
else if (theData < root->data) deleteNode(root->left, theData);
else if (theData > root->data) deleteNode(root->right, theData);
else {
// Case 1: No child
if (root->left == nullptr && root->right == nullptr) {
root = nullptr;
}
// Case 2: One child
else if (root->left == nullptr) {
root = std::move(root->left);
}
else if (root->right == nullptr) {
root = std::move(root->right);
}
// Case 3: Two children
else {
auto temp = findMin(root->right);
temp->data = root->data;
deleteNode(root->right, temp->data);
}
}
if (!root) return;
// Step 2: Update height of the current node
root->height = 1 + std::max(height(root->left), height(root->right));
// Step 3: Get the balalce factor of the this node (to
// check whether this node became unbalanced)
int balance = heightDiff(root);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && heightDiff(root->left) >= 0)
rightRotate(root);
// Left Right Case
if (balance > 1 && heightDiff(root->left) < 0) {
leftRotate(root->left);
rightRotate(root);
}
// Right Right Case
if (balance < -1 && heightDiff(root->right) <= 0)
leftRotate(root);
// Right Left Case
if (balance < -1 && heightDiff(root->right) > 0) {
rightRotate(root->right);
leftRotate(root);
}
}
void inorderTraversal(std::unique_ptr<Node>& root) {
if (!root) {
inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->right);
}
}
void preorderTraversal(std::unique_ptr<Node>& root) {
if (root != nullptr) {
std::cout << root->data << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
void postorderTraversal(std::unique_ptr<Node>& root) {
if (root != nullptr) {
postorderTraversal(root->left);
postorderTraversal(root->right);
std::cout << root->data << " ";
}
}
void DFS(std::unique_ptr<Node>& root) {
if (!root) return;
std::stack<Node const *> s;
s.push(root.get());
while (!s.empty()) {
auto p = s.top();
s.pop();
if (p->right != nullptr) s.push(p->right.get());
if (p->left != nullptr) s.push(p->left.get());
std::cout << p->data << " ";
}
}
void BFS(std::unique_ptr<Node>& root) {
if (!root) return;
std::queue<Node const *> q;
q.push(root.get());
while (!q.empty()) {
auto p = q.front();
q.pop();
if (p->left != nullptr) q.push(p->left.get());
if (p->right != nullptr) q.push(p->right.get());
std::cout << p->data << " ";
}
}
bool exists(int d) {
auto temp = root.get();
while (temp != nullptr) {
if (temp->data == d) {
return true;
}
else {
if (d > temp->data) {
temp = temp->right.get();
}
else {
temp = temp->left.get();
}
}
}
return false;
}
int main() {
// 8
// / \
// 4 10
// / \ / \
// 2 6 9 12
//insert(root, 8);
//insert(root, 10);
//insert(root, 4);
//insert(root, 2);
//insert(root, 6);
//insert(root, 12);
//insert(root, 9);
/* Constructing tree given in the above figure */
insert(root, 9);
insert(root, 5);
insert(root, 10);
insert(root, 0);
insert(root, 6);
insert(root, 11);
insert(root, -1);
insert(root, 1);
insert(root, 2);
/* The constructed AVL Tree would be
9
/ \
1 10
/ \ \
0 5 11
/ / \
-1 2 6
*/
printf("Preorder traversal of the constructed AVL "
"tree is \n");
preorderTraversal(root);
//deleteNode(root, 10);
/* The AVL Tree after deletion of 10
1
/ \
0 9
/ / \
-1 5 11
/ \
2 6
*/
//printf("\nPreorder traversal after deletion of 10 \n");
//preorderTraversal(root);
/*inorderTraversal(root);
std::cout << "\n";
preorderTraversal(root);
std::cout << "\n";
postorderTraversal(root);
std::cout << "\n";
DFS(root);
std::cout << "\n";
BFS(root);
std::cout << "\n";
exists(4) ? std::cout << "Yes" << std::endl : std::cout << "No" << std::endl;*/
std::cin.get();
}
#包括
#包括
#包括
#包括
#包括
#包括
结构节点{
int数据;
内部高度;
std::unique_ptr left=nullptr;
std::unique_ptr right=nullptr;
节点(常数int&x,常数int&y,std::unique\u ptr&&p=nullptr,std::unique\u ptr&&q=nullptr):
数据(x),
高度(y),
左(标准::移动(p)),
右(std::move(q)){}
节点(const int&data):数据(data){}
};
std::unique_ptr root=nullptr;
内部高度(标准::唯一\u ptr和根){
如果(!root)返回0;
返回根->高度;
}
空隙固定高度(标准::唯一性和根){
自动h1=高度(根部->左侧);
自动h2=高度(根部->右侧);
根->高度=(h1>h2?h1:h2)+1;
}
void rightRotate(标准::唯一的\u ptr&p){
std::unique_ptr q=std::move(p->left);
p->left=std::move(q->right);
q->right=std::move(p);
固定高度(p);
固定高度(q);
}
void leftRotate(std::unique_ptr&q){
std::unique_ptr p=std::move(q->left);
q->right=std::move(p->left);
p->left=std::move(q);
固定高度(q);
固定高度(p);
}
int heightDiff(标准::唯一根){
如果(!root)返回0;
返回高度(根->左)-高度(根->右);
}
无效余额(标准::唯一性和根){
固定高度(根);
如果(高度差(根)==2){
如果(高度差(根->右)<0)
右旋转(根->右);
左旋转(根);
}
如果(高度差(根)=-2){
如果(高度差(根->左)>0)
左旋转(根->左);
右旋转(根);
}
}
无效插入(标准::唯一\u ptr和根、常量int和数据){
std::unique\u ptr newNode=std::make_unique(数据);
如果(!root){
root=std::move(newNode);
返回;
}
如果(数据<根->数据)
插入(根->左,数据);
其他的
插入(根->右,数据);
平衡(根);
}
自动findMin(std::unique\u ptr和root){
而(root->left!=nullptr)root=std::move(root->left);
返回root.get();
}
void deleteNode(std::unique\u ptr和root、const int和theData){
//步骤1:对BST执行定期删除
如果(!root)返回;
否则,如果(数据<根->数据)删除节点(根->左,数据);
否则,如果(数据>根->数据)删除节点(根->右,数据);
否则{
//案例1:没有儿童
if(root->left==nullptr&&root->right==nullptr){
root=nullptr;
}
//案例2:一名儿童
else if(root->left==nullptr){
根=标准::移动(根->左);
}
else if(root->right==nullptr){
根=标准::移动(根->右);
}
//案例3:两名儿童
否则{
自动温度=findMin(根->右);
临时->数据=根->数据;
删除节点(根->右侧,临时->数据);
}
}
如果(!root)返回;
//步骤2:更新当前节点的高度
根->高度=1+std::max(高度(根->左)、高度(根->右));
//步骤3:获取该节点的平衡因子(到
//检查此节点是否变得不平衡)
内部平衡=高度差(根);
//如果该节点变得不平衡,则有4种情况
//左-左案例
如果(平衡>1和高度差(根->左)>=0)
右旋转(根);
//左右格
如果(平衡>1和高度差(根->左)<0){
左旋转(根->左);
右旋转(根);
}
//正确的情况
如果(平衡<-1&&heightDiff(根->右)右)>0){
右旋转(根->右);
左旋转(根);
}
}
void inorderTraversal(std::unique_ptr&root){
如果(!root){
inorderTraversal(根->左);
std::cout数据权限);
}
}
void preorderTraversal(std::unique_ptr&root){
if(root!=nullptr){
std::cout数据(左);
预订单行程(根->右);
}
}
void postorderTraversal(std::unique_ptr&root){
if(root!=nullptr){
postorderTraversal(根->左);
postorderTraversal(根->右);
std::cout data right!=nullptr)s.push(p->right.get());
如果(p->left!=nullptr)s.push(p->left.get());
std::cout data left!=nullptr)q.push(p->left.get());
如果(p->right!=nullptr)q.push(p->right.get());
std::cout数据==d){
返回true;
}
否则{
如果(数据>温度->数据){
temp=temp->right.get();
}
否则{
temp=temp->left.get();
}
}
}
返回false;
}
int main(){
// 8
// / \
// 4 10
// / \ / \
// 2 6 9 12
//插入(根,8);
//插入(根,10);
//插入(根,4);
//插入(根,2);
//插入(根,6);
//插入(根,12);
//插入(根,9);
/*构建上图中给出的树*/
插入(根,9);
插入(根,5);
插入(根,10);
插入(根,0);
插入(根,6);
插入(根,11);
插入(根,-1);
插入(根,1);
插入(根,2);
/*构建的AVL树将是
9
/ \
1 10
/ \ \
0 5 11
/ / \
-1 2 6
*/
printf(“构造的AVL的前序遍历”
“树是\n”);
前序行程(根);
//deleteNode(根,10);
/*删除10后的AVL树
1.
/ \
0 9
/ / \
-1 5 11
/ \
2 6
*/
//printf(“\n删除10后的优先级遍历\n”);
//前
void insert(std::unique_ptr<Node>& root, const int& theData) {
if (!root) {
root.reset(new Node(theData,0));
}
else {
if (theData < root->data)
insert(root->left, theData);
else
insert(root->right, theData);
balance(root);
}
}