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

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

View Full Document Right Arrow Icon
CS 310 Unit 12 AVL Trees Furman Haddix Ph.D. Assistant Professor Minnesota State University, Mankato Spring 2008
Background image of page 1

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

View Full DocumentRight Arrow Icon
AVL Trees Objectives Balanced Search Trees Rotation in Balanced Search Trees AVL Trees Definition Insertion Rebalance/Restructure Deletion Chapter 13, Exercise 13-3
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2008 for the course CS 310 taught by Professor Furmanhaddix during the Spring '08 term at Minnesota State University, Mankato.

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 Right Arrow Icon
Ask a homework question - tutors are online