AVL树(二叉平衡树)笔记

来源:互联网 发布:张佳玮 詹姆斯 知乎 编辑:程序博客网 时间:2024/05/21 02:34

先上代码 以后补充


avl树插入操作还好,删除操作比较麻烦--


#include<stdio.h>#include<stdlib.h>#include<math.h>typedef struct TreeNode* T;struct TreeNode{int bf;int height;int data;T leftchild;T rightchild;T parent;};T root = NULL;T findDelPosition(T root, int key);//function prototypeint getHeight(T node){if (node) return node->height;else return -1;}int max(int lc, int rc){return (lc > rc ? lc : rc);}int reComHeight(T root){root->height = max(getHeight(root->leftchild), getHeight(root->rightchild)) + 1;root->bf = getHeight(root->leftchild) - getHeight(root->rightchild);return root->bf;}T LLclockRotation(T x){T y = x->leftchild;x->leftchild = y->rightchild;if (y->rightchild) y->rightchild->parent = x;y->rightchild = x;y->parent = x->parent;x->parent = y;reComHeight(x);reComHeight(y);return y;}T RRantiRotation(T x){T y = x->rightchild;x->rightchild = y->leftchild;if (y->leftchild) y->leftchild->parent = x;y->leftchild = x;y->parent = x->parent;x->parent = y;reComHeight(x);reComHeight(y);return y;}T LRanti_clockRotation(T x){x->leftchild = RRantiRotation(x->leftchild);return LLclockRotation(x);}T RLclock_antiRotation(T x){x->rightchild = LLclockRotation(x->rightchild);return RRantiRotation(x);}T reBalance(T curr, int bf){if (abs(bf)<2) return curr;else{if (bf == -2){if (curr->rightchild->bf>0) return RLclock_antiRotation(curr);else return RRantiRotation(root);}else if (bf == 2){if (curr->leftchild->bf < 0)return LRanti_clockRotation(root);else return LLclockRotation(root);}}}T insert(T curr, T prev, int num){int x = 0;if (!curr){curr = (T)malloc(sizeof(TreeNode));curr->data = num;curr->bf = 0;curr->height = 0;curr->parent = prev;curr->leftchild = curr->rightchild = NULL;x = num;}else if (num<(curr->data)) curr->leftchild = insert(curr->leftchild, curr, num);else if (num>(curr->data)) curr->rightchild = insert(curr->rightchild, curr, num);curr=reBalance(curr,reComHeight(curr));return curr;}void toPrint(T root, int depth = 0){if (root->rightchild)toPrint(root->rightchild, depth + 1);for (int i = 0; i< depth; i++) printf("\t");printf("%d(%d)\n", root->data, root->bf);if (root->leftchild)toPrint(root->leftchild, depth + 1);}T FindMin(T least){if (!least->leftchild)return least;else FindMin(least->leftchild);}T clearTarget(T root){T par = root->parent, temp = root;if (root->leftchild&&root->rightchild){temp = FindMin(root->rightchild);root->data = temp->data;root->rightchild = findDelPosition(root->rightchild, root->data);}else{if (root->leftchild){root->leftchild->parent = par;if (par->leftchild == root) par->leftchild = root->leftchild;else if (par->rightchild == root) par->rightchild = root->leftchild;root = root->leftchild;}else if (root->rightchild){root->rightchild->parent = par;//认可问题if (par->leftchild == root) par->leftchild = root->rightchild;else if (par->rightchild == root) par->rightchild = root->rightchild;root = root->rightchild;}else root = NULL;//都为NULLfree(temp);}if (root) reComHeight(root);return root;}T findDelPosition(T root, int key){if (key == root->data) root = clearTarget(root);else if (key<root->data) root->leftchild = findDelPosition(root->leftchild, key);else if (key>root->data) root->rightchild = findDelPosition(root->rightchild, key);if (root) root = reBalance(root, reComHeight(root));return root;}int main(){int temp[9] = { 40, 60, 20, 80, 50, 30, 10, 70, 25 };int i = 0;for (i = 0; i < 9; i++) root = insert(root, root, temp[i]);//findDelPosition(root, 40);toPrint(root);return 0;}

例子输出:

删除结点40之前:


删除结点40之后:




0 0
原创粉丝点击