





















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This will help to understand better about AVL trees
Typology: Summaries
1 / 61
This page cannot be seen from the preview
Don't miss anything!






















































Unit II : Balanced Trees : AVL Trees: Maximum Height of an AVL Tree, Insertions and Deletions. 2-3 Trees: Insertion, Deletion, Priority Queues , Binary Heaps: Implementation of insert and delete min, creating heap.
Tree: Tree is non-linear data structure that consists of root node and potentially many levels of additional nodes that form a hierarchy. A tree can be empty with no nodes called the null or empty tree. A tree is a structure consisting of one node call the root and one or more subtrees. Descendant: - A node reachable by repeated proceeding form parent to child. Ancestor: - a node reachable by repeated proceeding from child to parent. Degree:- the number of sub-trees of a node, means the degree of an element (node) is the number of children it has. The degree of a leaf node is always 0(zero). Siblings:- Nodes with the same parent. Height:- number of nodes which must be traversed from the root to the reach a leaf of a tree. Examples Tree associated with a document
Binary Tree: - A binary tree is a tree data structure in which each node has at most two children, which referred as the left and right child. Full Binary Tree or Complete Trees: A binary tree of height is ‘h’ and contains exactly “ 2 h-1” elements is called full binary tree.
H=4 (levels+1 of root node) nnumber elements= 2 h-1= (Another definition of full binary tree is, each leaf is same distance from the root)
Linked list representation of binary tree:
// Binary tree node structure struct BinaryTreeNode { int data; BinaryTreeNode *left, right; }temp;
AVL Trees: Introduction: An AVL tree (Adelson-Velskii and Landis' tree, named after the inventors) is a self-balancing binary search tree, invented in 1962 Definition: An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference b/w the heights of the node’s left & right sub trees is either 0 or +1 or -. Balance factor = ht of left sub tree – ht of right sub tree. Where ht=height
Example:
Structure or pseudo code for avl tree: struct node node *rotateright(node *); { int data; node *rotateleft(node *); struct node left,right; node *RR(node *); int ht; node *LL(node *); }node; node *LR(node *); node *RL(node *); node *insert(node *,int); int height( node *); node *Delete(node *,int); int BF(node *);
Inserting and Deleting on AVL Trees Problem: After insert/delete: load balance might be changed to +2 or -2 for certain nodes. _ re-balance load after each step
Requirements: re-balancing must have O (log n) worst-case complexity Solution: Apply certain “rotation” operations
AVL tree insertion: After inserting a node, it is necessary to check each of the node's ancestors for consistency with the rules of AVL. The balance factor is calculated as follows: balanceFactor = height (left subtree) - height(right subtree). If insertions are performed serially, after each insertion, at most one of the following cases needs to be resolved to restore the entire tree to the rules of AVL.
Let the node that needs rebalancing be . 4 possible situations to insert in a tree
Page 10 of 27
Page 11 of 27
Page 13 of 27
Page 14 of 27
Example of Insertion of 1, 2, 3, 4, 5, 0, 7, 6 into an AVL Tree All insertions are right-right and so rotations are all single rotate from the right. All but two insertions require re-balancing:
All insertions are right-left and so double rotations take place form left-right and right-to left
node * insert(node T,int x) { if(T==NULL) { T=(node)malloc(sizeof(node)); T->data=x; T->left=NULL; T->right=NULL; } else if(x > T->data) // insert in right sub-tree { T->right=insert(T->right,x); if(BF(T)==-2) if(x>T->right->data) T=RR(T); else T=RL(T); } else if(x<T->data) // insert in left sub-tree { T->left=insert(T->left, x); if(BF(T)==2) if(x < T->left->data) T=LL(T); else T=LR(T); } T->ht=height(T); return(T); }
int height(node *T) { int lh,rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; if(T->right==NULL) rh=0; else rh=1+T->right->ht; if(lh>rh) return(lh); return(rh); } int BF(node *T) { int lh, rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; if(T->right==NULL) rh=0; else rh=1+T->right->ht; return(lh-rh); }
node * RR(node *T) { T=rotateleft(T); return(T); }
node * LL(node *T) { T=rotateright(T); return(T); }
node * LR(node *T) { T->left=rotateleft(T->left); T=rotateright(T); return(T); }
node * RL(node *T) { T-
right=rotateright(T- right); T=rotateleft(T); return(T); }
node * rotateleft(node *x) { node *y; y=x->right; x->right=y->left; y->left=x; x->ht=height(x); y->ht=height(y); return(y); }
node * rotateright(node *x) { node *y; y=x->left; x->left=y->right; y->right=x; x->ht=height(x); y->ht=height(y); return(y); }
Search Operation in avl tree:
Search operation of avl tree is same as the search operation of binary search tree. Means given element is checked with the root element,
If the given element is match with the root element then return the value If the given element is less than the root element then the searching operation is continued at left sub-tree of the tree. If the given element is greater than the root element then the searching operation is continued at right sub-tree of the tree.
node *search(node *root, int key, node **parent) { node *temp; temp = root; while (temp != NULL) { if (temp->data == key) { printf("\nThe %d Element is Present", temp->data); return temp; } *parent = temp; if (temp->data > key) temp = temp->lchild; else temp = temp->rchild; } return NULL; }
Delete 60 (case 3)
Delete 55 (case 3)
Delete 50 (case 3)