380 likes | 520 Views
Kielce 2006. Data Structures and Algorithms. AVL and Red and Black trees. Introduction - Trees. Definition A set of nodes A set of directed edges. Edge. 6. Node. 5. 8. 8. 4. 7. 11. 5. 5. 7. 11. Introduction - Trees. Properties One node is the root .
E N D
Kielce 2006 Data Structures and Algorithms AVL and Red and Black trees
Introduction - Trees • Definition • A set of nodes • A set of directed edges Edge 6 Node 5 8 8 4 7 11 5 5 7 11
Introduction - Trees • Properties • One node is the root. • Every nodec, except the root is connected by an edge to exactly one other p. • p is c’s father. • c is one of the p’s children. • The node with no children is a leaf. • Only one path transverses from the root to each node. • The number of edges is the path length. p c
Introduction – Binary Trees • Properties • Binary tree is a tree that each node cannot have more than two children. We can call it Left and Right Root 6 Leaf 4 8 5 7 11
Introduction – Binary Trees • Properties • Is a tree that each node can not have more than two children. We can call it Left and Right Is this a tree? 6 4 8 11 5 7 11
Introduction – Binary Search Trees • Properties • Satisfies the search tree order property. • For every node X, all keys in left sub tree have smaller values than in X. • All keys in right sub tree of X have larger values than in X. 6 4 8 5 7 11
Introduction – Binary Search Trees • Insert elements. Insert 6 6 Insert 8 Insert 4 4 8 Insert 5 Insert 11 5 7 11 Insert 7
Introduction – Binary Search Trees • Remove elements. 6 Remove 6 4 8 5 7 11
Introduction – Binary Search Trees • Complexity • Best case (in average) - O(log n) • Worst case – O(n) 1 2 3 4 5 6
AVL Trees • The binary trees being O(log n) in average, the worse case is O(n). • Motivation: we want to ALWAYS guarantee O(log n) of complexity in the operations to insert, to remove and to search. • Idea: keep the tree balanced tree after each operation. • Solution: AVL (Adelson-Velskii e Landis 1962)
Definition • An empty search binary tree is balanced AVL. • A non empty binary search tree, A={r,Lt,Rt} is balanced AVL if ether Lt and Rt are balanced and • |hl–hr|<=1 were hl is the height of Lt and hr is the height of Rt • Resume: in each knot of the tree the height of the left and right sub-trees differ at most 1.
Example 6 6 4 8 4 8 5 1 1 5 7 11 7 11 Not an AVL tree AVL tree 3 3 2
Search, insert and remove • A search in a AVL tree is the same as in a binary search tree. • An insertion or a remove in a AVL tree is the same as in a binary search tree, with the exception that we might have to balance it after an insertion or remove.
Insert • Let x be the deepest knot where the unbalancing occurs. • 4 cases can happen. The insertion is at: • Left sub-tree from the left son of x • Right sub-tree from the left son of x • Left sub-tree from the right son of x • Right sub-tree from the right son of x
Insert • Cases 1 and 4 are resolved with a simple rotation • Cases 2 and 3 are resolved with a double rotation
Example of Insert 15 5 20 15 17 3 10 21 3 20 2 4 17 2 5 21 1 1 4 10
Simple Rotation height h+2 before insert a X < b < Y < a < Z b Z h height h+2 after insert X Y b h h -1 h+1 a X Z h+1 Y h h
Cases 2 and 3 a height h+2 before insert X < b < Y < a < Z h -1 b Z b h X Y h a h+1 X h Z Y h h+1 a simple rotation fails
Example of Double Rotation 1st step 15 15 8 17 8 17 16 4 10 6 16 10 4 6 3 3 5 5
Example of Double Rotation 2nd step 15 15 8 17 17 6 6 16 10 4 8 16 4 3 5 10 3 5
Double Rotation height h+2 before insert a b Z h 0 c h W height h+2 after insert h c X Y h-1 h b a X Y Z W h-1 h h h W < b < X < c Y < a < Z
Complexity • find has complexity O(log n) because the height of the tree is always O(log n). • insert has complexity O(log n) because it is made a find and then it is possible to visit all the knots back to the root and do two simple rotations (O(1) each) to reorganize the tree. • removeO(log n) – why?
Remove • You can save the path until the knot to remove and then do the path backwards, checking if each knot saved is balanced. 15 15 32 8 8 28 28 45 4 remove 32 16 4 30 30 45
Pseudo code Simple rotation to the right public BinaryNode rotationRR(BinaryNode k1) { BinaryNode k2 = k1.right; k1.right = k2.left; K2.left = k1; return k2; } k1 k2 A B C
Pseudo code Simple rotation to the left public BinaryNode rotationLL(BinaryNode k2) { BinaryNode k1 = k2.left; k2.left = k1.right; K1.right = k2; return k1; } k2 k1 C A B
Pseudo code Double rotation to the right public BinaryNode rotationRL(BinaryNode k1) { K1.right = rotationLL(k1.right); return rotationRR(k1); } k1 k3 A K2 D B C
Pseudo code Double rotation to the left public BinaryNode rotationRL(BinaryNode k3) { K3.left = rotationLR(k3.left); return rotationLL(k3); } K3 k1 D k2 A B C
Red and Black Trees • Motivation: • They are another type of balanced trees.
Definition • It is a binary search tree. • All the knots are colored of redor black. • The root is black. • If a knot is redall his children are black. • A redfather never has a red children. • All the paths from a knot to all the leafs of each branch have to contain the same number of black knots.
Example • All knots are black or red. • The root is black. • If a knot is redthen his father is black. • All the paths that goes to a leaf has the same number of black knots.
Insertion and removal • Any element is inserted as in the binary search tree and it is set with the color red. • Any element is removed as in the binary search tree (minimum of the right sub-tree). • After the insertion or removing of the knot, the tree is reorganized (if some of the previous rules are violated) running all the knots in the path of the tree branch: • Starts with the father of the inserted or removed element • It finishes in the root of the tree, modifying the color of the knots or performing simple or double rotations.
Rules – Case 1 • Change C to red and A and D to black. • In case C is the root, back to black Change of colors
Rules – Case 2 • Transforms in the case 3. Left Rotation
Rules – Case 3 • Rotate a change colors at C and B. • Balanced! There are no more rule violations. Right Rotation
Complexity • O(log n)
Exercise • Insert from an empty tree: 10, 85, 15, 70, 20, 60, 40.
Bibliography • Weiss, M. A. “Data Structures and Problem Solving Using Java” (second edition), Addison-Wesley Publishing, 2001. • Neto, J. P. “Programação, Algoritmos e Estruturas de Dados”. Escolar Editora, 2004.