二分查找树

#include<iostream>
using namespace std;

/*AVL DataStructs*/
typedef struct AVLNode *AVLTree;
struct AVLNode
{
int data;
AVLTree left;
AVLTree right;
};

/*insert AVLTree*/
AVLTree insertAVL(AVLTree t, int data)
{
if (!t)
{
t = (AVLTree)malloc(sizeof(struct AVLNode));
t->data = data;
t->left = NULL;
t->right = NULL;
}
else if (data < t->data)
{
t->left = insertAVL(t->left, data);
}
else if (data >= t->data)
{
t->right = insertAVL(t->right, data);
}
return t;
}

void printTree(AVLTree t,AVLTree maxPoint1)
{
if(t)
{
printTree(t->left,maxPoint1);
if(t == maxPoint1)
{cout<<t->data;}
else
{cout<<t->data<<" ";}
printTree(t->right,maxPoint1);
}
}

AVLTree maxPoint(AVLTree t)
{
while(t->right)
{
t=t->right;
}
return t;
}

int main()
{
int num;
cin >> num;
AVLTree  t = NULL;
for (int i = 0; i < num; i++)
{
int data;
cin >> data;
t = insertAVL(t, data);
}
printTree(t,maxPoint(t));

return 0;
}

AVL树

#include<iostream>
using namespace std;

/*AVL DataStructs*/
typedef struct AVLNode *AVLTree;
struct AVLNode
{
int data;
AVLTree left;
AVLTree right;
int height;
};

int MAX(int a,int b)
{
return a>b?a:b;
}

/*height of tree*/
int getHeight(AVLTree t)
{
if (!t)
{
return 0;
}
else
{
int hL = 0, hR = 0;
if (t->left) hL = getHeight(t->left);
if (t->right) hR = getHeight(t->right);
return MAX(hL, hR) + 1;
}

}

/*LL*/
AVLTree singleLRotation(AVLTree A)
{
AVLTree B = A->left;
A->left = B->right;
B->right = A;
A->height = MAX(getHeight(A->left), getHeight(A->right)) + 1;
B->height = MAX(getHeight(B->left), getHeight(B->right)) + 1;
return B;
}
/*RR*/
AVLTree singleRRotation(AVLTree A)
{
AVLTree B = A->right;
A->right = B->left;
B->left = A;
A->height = MAX(getHeight(A->left), getHeight(A->right)) + 1;
B->height = MAX(getHeight(B->left), getHeight(B->right)) + 1;
return B;
}

/*LR*/
AVLTree doubleLRRotation(AVLTree A)
{
A->left = singleRRotation(A->left);
return singleLRotation(A);
}

/*RL*/
AVLTree doubleRLRotation(AVLTree A)
{
A->right = singleLRotation(A->right);
return singleRRotation(A);
}

/*insert AVLTree*/
AVLTree insertAVL(AVLTree t, int data)
{
if (!t)
{
t = (AVLTree)malloc(sizeof(struct AVLNode));
t->data = data;
t->height = 0;
t->left = NULL;
t->right = NULL;
}
else if (data < t->data)
{
t->left = insertAVL(t->left, data);
if (getHeight(t->left) - getHeight(t->right) == 2)
{
if (data < t->left->data)
{
/*LL*/
t = singleLRotation(t);
}
else
{
/*LR*/
t = doubleLRRotation(t);
}
}
}
else if (data >= t->data)
{
t->right = insertAVL(t->right, data);
if (getHeight(t->right) - getHeight(t->left) == 2)
{
if (data >= t->right->data)
{
/*RR*/
t = singleRRotation(t);
}
else
{
/*RL*/
t = doubleRLRotation(t);
}
}
}

t->height = MAX(getHeight(t->left), getHeight(t->right)) + 1;
return t;
}

void printTree(AVLTree t,AVLTree maxPoint1)
{
if(t)
{
printTree(t->left,maxPoint1);
if(t == maxPoint1)
{cout<<t->data;}
else
{cout<<t->data<<" ";}
printTree(t->right,maxPoint1);
}
}

AVLTree maxPoint(AVLTree t)
{
while(t->right)
{
t=t->right;
}
return t;
}

int main()
{
int num;
cin >> num;
AVLTree  t = NULL;
for (int i = 0; i < num; i++)
{
int data;
cin >> data;
t = insertAVL(t, data);
}
printTree(t,maxPoint(t));

return 0;
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
05-26
03-30 276
07-02 5674
07-12 1120
05-21
07-20

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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