Lecture10

# Lecture10 - AVL Trees EECS 233 2 Height and Balance of...

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

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

View Full Document

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

View Full Document

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.

Unformatted text preview: AVL Trees EECS 233- 2- Height and Balance of Trees The height of a tree is the length of the longest path from the root node to a leaf node. What is the height of the tree with root=12? The height of a tree with only one node (a leave)? Empty tree? balance of a tree (with node N as the root): balance (N) = height(N's right subtree) – height(N's left subtree) balance(node 26) = 0 – 2 = -2 Balance(node 4) = 0 – (-1) = 1- 3- AVL Trees (Adelson-Velsky & Landis ’ 62) An AVL tree is a variant of a binary search tree that takes special measures to ensure that the tree is balanced. Binary tree Balanced: balance of all nodes are -1, 0, or 1 If a newly inserted node would cause the tree to go out of balance, the nodes are rearranged to restore balance. Challenge: the steps taken to restore balance must: maintain the search-tree inequalities have a worst-case time complexity of O (log n)- 4- Rotation Operations A rotation rearranges the nodes in a tree while maintaining the search-tree inequalities. Right rotation on (around) y: Left rotation on y:- 5- Example Rotations Right rotation on node 26 Left rotation on node 33- 6- Implementation of Rotations AVL Tree Representation in Java public class AVLTree { private class Node { private int key; private String data; private Node left; // reference to left child private Node right; // reference to right child private Node parent; // reference to parent node private int balance;...
View Full Document

{[ snackBarMessage ]}

### Page1 / 25

Lecture10 - AVL Trees EECS 233 2 Height and Balance of...

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

View Full Document
Ask a homework question - tutors are online