







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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Avl and red-black search trees, two types of self-balancing binary search trees. Avl trees use recursive algorithms to maintain height-balance, while red-black trees use color attributes to maintain balance. Both trees ensure efficient access to data and prevent degenerate trees.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!
Binary search trees are designed for efficient location of an element. AVL and red-black trees balance a binary search tree so it more nearly resembles a complete tree.
(a) (b) (c)
In each node of an AVL tree, the difference between the heights of its left and right subtrees does not exceed
The AVLTree class implements the Collection interface and builds an AVL tree. It has the same public methods as the STree class.
An AVLNode contains the node value, references to the node's children, and the balance factor of the node.
Insert 65 along path 40 - 50 - 60 40 (- 1 ) 60 ( 0 ) 20 ( 1 ) 55 ( 0 ) 65 ( 0 ) 40 ( 0 ) 60 ( 0 ) 45 ( 0 ) 50 ( 0 ) 20 ( 1 ) 10 ( 0 ) Insert 55 along path 40 - 50 - 60 40 (- 1 ) 60 ( 1 ) 50 (- 1 ) 55 ( 0 ) 45 ( 0 ) 50 (- 1 ) 10 ( 0 ) 45 ( 0 ) 20 ( 1 ) 10 ( 0 )
The recursive addNode() algorithm moves to the insertion point using the usual rules for a binary search tree. The addition of an element may cause the tree to be out of balance. The recursive addNode() algorithm reorders nodes as it returns from function calls. The addition of an element may cause the tree to be out of balance. The recursive addNode() algorithm reorders nodes as it returns from function calls.
Single Right Rotation
When the new element enters the subtree of an outside grandchild, a single rotation exchanges the parent and child node. A rotation is either to the left or the right.
Single Left Rotation
When the new element enters the subtree of an inside grandchild, a double rotation corrects the imbalance. A double rotation consists of two single rotations.
The add() Method
The add() method assures that item is not in the tree, calls addNode() to insert it, and then increments treeSize and modCount.
24 12 5 12 (^524) Insert 24 12 5 Single Rotate Right (P= 24 )