{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS 310 Unit 12 AVL Trees

# CS 310 Unit 12 AVL Trees - CS 310 Unit 12 AVL Trees Furman...

This preview shows pages 1–8. Sign up to view the full content.

CS 310 Unit 12 AVL Trees Furman Haddix Ph.D. Assistant Professor Minnesota State University, Mankato Spring 2008

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
AVL Trees Objectives Balanced Search Trees Rotation in Balanced Search Trees AVL Trees Definition Insertion Rebalance/Restructure Deletion Chapter 13, Exercise 13-3
Some Balanced Search Trees AVL binary tree Adelson-Velsky and Landis Generally accepted as first self-balancing search tree For each node, each subtree differs by at most one B-Trees Each node has a variable number of children, interior nodes having between m and m/2 nodes Insertions, Deletions, and Node Joins and Node Splits affect the number of children for each node Height is O(log m n) as opposed to O(lg n) for binary trees Red-Black binary tree Basic idea is that the distances between two leaves and the root cannot differ by a factor of more than two Limit is enforced by only counting black nodes and

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Balancing (Somewhat) Balanced Search Trees Principal general technique is rotation Left rotation moves object node to the left so that its right child becomes its parent Right rotation moves object node to the right so that its left child becomes its parent Rotation exchanges the roles of a parent and child without changing the order of the nodes In red-black trees, a specialized techique, recoloring, is also used. Node recoloring is used, sometimes in combination with rotation, to fix red-black trees following insertion or deletion.
Rotation Overview Rotation moves the rotation object node x with its child y. x is the root of the subtree in which the rotation takes place In left-rotate(x), the child y was x’s right child x becomes y’s left child y’s left subtree becomes x’s right subtree In right-rotate(x), the child y was x’s left child x becomes y’s right child y’s right subtree becomes x’s left subtree left-rotate() and right-rotate() are used with both insert() and delete() to the balance condition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Rotation Examples Left Rotation Disconnect x Prepare to reconnect x Disconnect y Prepare to reconnect y Reconnect y to x’s parent x as left child of y y’s left subtree to x Right Rotation Disconnect x Prepare to reconnect x Disconnect y Prepare to reconnect y Reconnect y to x’s parent x as right child of y y’s right subtree to x rs ty p x rs tx x y ls ty p x ls tx x rs ty y ls ty
Left Rotation Pseudocode leftRotate(type *x) { y = x->right(); // set y as right child of x x->setRight(y->left()); // y’s lc becomes x’s rc if(y->left() != null) y->left()->setP(x); // x becomes p of y’s rc y->setP(x->p()); // x’s parent becomes y’s if(x->p() == null) root = y; // if x was root, y root now

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

CS 310 Unit 12 AVL Trees - CS 310 Unit 12 AVL Trees Furman...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online