数据结构—二叉平衡排序树的删除
原理:参考趣学数据结构
代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct avlTree {int data;struct avlTree *lchild, *rchild;int height;
}avlTree;int height(avlTree* AVLTree);
void updateHeight(avlTree* &AVLTree);avlTree* LL(avlTree* &AVLTree) {avlTree* temp = AVLTree->lchild;AVLTree->lchild = temp->rchild;temp->rchild = AVLTree;updateHeight(temp);updateHeight(AVLTree);return temp;
}
avlTree* RR(avlTree* &AVLTree) {avlTree* temp = AVLTree->rchild;AVLTree->rchild=temp->lchild;temp->lchild = AVLTree;updateHeight(temp);updateHeight(AVLTree);return temp;
}
avlTree* LR(avlTree* &AVLTree) {AVLTree->lchild=RR(AVLTree->lchild);return LL(AVLTree);
}
avlTree* RL(avlTree* &AVLTree) {AVLTree->rchild = LL(AVLTree->rchild);return RR(AVLTree);
}
int height(avlTree* AVLTree) {int m, n;if (!AVLTree) {return 0;}else {m = height(AVLTree->lchild);n = height(AVLTree->rchild);if (m > n) {return m + 1;}else {return n + 1;}}
}
void updateHeight(avlTree* &AVLTree) {if (AVLTree) {AVLTree->height = height(AVLTree);updateHeight(AVLTree->lchild);updateHeight(AVLTree->rchild);}
}
avlTree* insertAVLTree(avlTree* &AVLTree,int e) {if (!AVLTree) {AVLTree = (avlTree*)malloc(sizeof(avlTree));AVLTree->data = e;AVLTree->lchild = AVLTree->rchild = NULL;AVLTree->height = 1;return AVLTree;}else {if (AVLTree->data == e) {printf("已经存在元素%d,不需要插入\n", e);return AVLTree;}if (e < AVLTree->data) {AVLTree->lchild = insertAVLTree(AVLTree->lchild, e);if (height(AVLTree->lchild) - height(AVLTree->rchild) == 2) {if (e > AVLTree->lchild->data) {AVLTree =LR(AVLTree);}else {AVLTree =LL(AVLTree);}}}else {AVLTree->rchild = insertAVLTree(AVLTree->rchild, e);if (height(AVLTree->rchild) - height(AVLTree->lchild) == 2) {if (AVLTree->rchild->data > e) {AVLTree =RL(AVLTree);}else {AVLTree =RR(AVLTree);}}}}updateHeight(AVLTree);return AVLTree;
}
avlTree * adjustAVLTree(avlTree * &AVLTree) {if (!AVLTree) {return NULL;}if (height(AVLTree->lchild) - height(AVLTree->rchild) == 2) {if (height(AVLTree->lchild->lchild) >= height(AVLTree->lchild->rchild)) {AVLTree = LL(AVLTree);}else {AVLTree = LR(AVLTree);}}else if (height(AVLTree->rchild) - height(AVLTree->lchild) == 2) {if (height(AVLTree->rchild->rchild) >= height(AVLTree->rchild->lchild)) {AVLTree = RR(AVLTree);}else {AVLTree = RL(AVLTree);}}updateHeight(AVLTree);return AVLTree;
}
avlTree * deleteElemAVL(avlTree * &AVLTree,int data) {if (AVLTree == NULL) {return AVLTree;}if (AVLTree->data == data) {if (!AVLTree->rchild) {avlTree* temp = AVLTree;AVLTree = AVLTree->lchild;delete temp;}else {avlTree* p = AVLTree->rchild;while (p->rchild) {p = p->lchild;}AVLTree->data = p->data;AVLTree->rchild = deleteElemAVL(AVLTree->rchild, AVLTree->data);updateHeight(AVLTree);}return AVLTree;}else if (data < AVLTree->data) {AVLTree->lchild = deleteElemAVL(AVLTree->lchild, data);}else {AVLTree->rchild = deleteElemAVL(AVLTree->rchild, data);}if (AVLTree->lchild) {AVLTree->lchild = adjustAVLTree(AVLTree->lchild);}if (AVLTree->rchild) {AVLTree->lchild = adjustAVLTree(AVLTree->rchild);}if (AVLTree) {AVLTree = adjustAVLTree(AVLTree);}return AVLTree;
}
void prePrint(avlTree* T) {if (T) {printf("%d ", T->data);prePrint(T->lchild);prePrint(T->rchild);}
}
int main() {avlTree* T = NULL;int count, data;printf("开始构造avl:\n输入avl结点的数目:");scanf_s("%d", &count);int counts = 0;while (count--) {counts += 1;printf("输入avl的第%d个结点:", counts);scanf_s("%d", &data);insertAVLTree(T, data);}printf("前序遍历avl\n");prePrint(T);printf("\n");printf("删除avl的结点:");scanf_s("%d", &count);deleteElemAVL(T, count);printf("删除avl的结点后的前序遍历\n");prePrint(T);printf("\n");system("pause");return 0;
}
测试截图:
时间复杂度:O(logn),空间复杂度O(logn)
如果存在什么问题,欢迎批评指正!谢谢!