AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。 查找、插入和删除在平均和最坏情况下都是O(log n)。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。 AVL树得名于它的发明者G.M. Adelson-Velsky和E.M. Landis,他们在1962年的论文《An algorithm for the organization of information》中发表了它。
#ifndef _AVL_H #define _AVL_H typedefstructnode *Position; typedef Position AVLtree;
typedefstructnode { int height; AVLtree left; AVLtree right; int data; }node;
intHeight(Position p); //return the height of a node Position SingleRotateWithRight(Position k2); //left rotate when in RR operation Position SingleRotateWithLeft(Position k2); // LL Position DoubleRotateWithLeft(Position k3); // LR Position DoubleRotateWithRight(Position k3); // RL Position Rotate(Position k); AVLtree insert(AVLtree root, int data); //Insert operation Position search(AVLtree root, int data); AVLtree delete(AVLtree root, int data); voidinorder_print(AVLtree root); voiddispose(AVLtree root); //free the tree #endif
intmain(int argc, charconst *argv[]) { AVLtree root = NULL; char choice; printf("Welcome to the AVL tree program\n"); while (1) { printf("Please make a choice\n"); printf("----------------------\n"); printf("i. insert an element\n"); printf("p. print the tree\n"); printf("d. delete an element from the tree\n"); printf("f. find an element \n"); printf("q. quit the program\n"); printf("----------------------\n");
/*get rid of '\n' in buffer*/ if ((choice = getchar()) == '\n') choice = getchar();
int number; Position p; switch (choice) { case'i': printf("Which number to insert?\n"); scanf("%d", &number); root = insert(root, number); break; case'p': printf("The tree is \n"); printf("######################\n"); inorder_print(root); printf("######################\n"); break; case'd': printf("Which number to delete?\n"); scanf("%d", &number); root = delete(root, number); break; case'f': printf("Which number to find?\n"); scanf("%d", &number); if (p = search(root, number)) { printf("%d is found\n", number); } else printf("Not found\n"); break; case'q': printf("Byebye!\n"); dispose(root); goto QUIT; default: printf("Wrong choice! Please try again\n"); break; } } QUIT: return0; }
Position DoubleRotateWithLeft(Position k3){ k3->left = SingleRotateWithRight(k3->left); return SingleRotateWithLeft(k3); }
Position DoubleRotateWithRight(Position k3){ k3->right = SingleRotateWithLeft(k3->right);
return SingleRotateWithRight(k3); }
Position Rotate(Position k){ if (Height(k->left) - Height(k->right) == 2) { if (Height(k->left->left) >= Height(k->left->right)) { k = SingleRotateWithLeft(k); } else { k = DoubleRotateWithLeft(k); } }
if (Height(k->right) - Height(k->left) == 2) { if (Height(k->right->right) >= Height(k->right->left)) { k = SingleRotateWithRight(k); } else { k = DoubleRotateWithRight(k); } }
return k; }
AVLtree insert(AVLtree root, int data){ if (root == NULL) { /*if the tree is empty then create a node*/ root = (node*)malloc(sizeof(node)); root->left = root->right = NULL; root->data = data; root->height = 0; }
A B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which:
the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree
all leaves are on the same level
all non-leaf nodes except the root have at least ceil(m / 2) children
the root is either a leaf node, or it has from two to m children