CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.
Chapter 4: Trees Part II - AVL Tree
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
CS 261 – Winter 2010 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting.
CSE 326: Data Structures AVL Trees
AVL Trees ITCS6114 Algorithms and Data Structures.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
AVL Trees Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignments? zYour minute essay last.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Week 7 - Friday CS221.
CSIT 402 Data Structures II
Introduction Applications Balance Factor Rotations Deletion Example
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 201 Data Structures and Algorithms
CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
AVL Tree Mohammad Asad Abbasi Lecture 12
Trees (Chapter 4) Binary Search Trees - Review Definition
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
AVL Tree A Balanced Binary Search Tree
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Copyright © Aiman Hanna All rights reserved
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Tree Rotations and AVL Trees
AVL Trees CSE 373 Data Structures.
Binary Search Trees.
CS223 Advanced Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
CSC 143 Binary Search Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
CSE 373 Data Structures Lecture 8
CS 261 – Data Structures Binary Search Trees.
1 Lecture 13 CS2013.
CS 261 – Data Structures AVL Trees.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CS 261 – Data Structures AVL Trees

Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to the node being manipulated In a well balanced tree, the length of the longest path is roughly log n –E.g.: 1 million entries  longest path is log 2 1,048,576 = 20. For a thin, unbalanced tree, operations become O(n): –E.g.: elements are added to tree in sorted order BALANCE IS IMPORTANT!

Requiring Complete Trees (Bad Idea) Recall that a complete binary tree has the shortest overall path length for any binary tree –The longest path in a complete binary tree with n elements is guaranteed to be no longer than ceiling(log n) –If we can keep our tree complete, we’re set for fast search times Very costly to maintain a complete binary tree

Height Balanced Trees Instead, use height-balanced binary trees: for each node, the height difference between the left and right subtrees is at most one Trees are locally balanced, but globally they can be slightly more unbalanced

Height-Balanced Trees Mathematically, the longest path in a height-balanced tree is, at worst, 44% longer than log n This is just a constant multiplier (so it is not significant in terms of asymptotic complexity) Therefore, algorithms on height-balanced trees that run in time proportional to the path length are still O(log n) (Proof is sort of cute, ties heights to Fibonocci numbers)

AVL Trees Named after the inventors’ initials Maintain the height balanced property of a BST AVL trees add an integer height field to each node: –null node has a height of –1 –A node is unbalanced when the absolute height difference between the left and right subtrees is greater than one How does it maintain the height balanced property? –When unbalanced, performs a rotation to balance the tree 1 (2) Node data Height field 3 (0) 2 (1) 1 (2) 3 (0) 1 (0) 2 (1) Rotate left Unbalanced node

AVL Implementation: AVLNode - heights struct avlNode { // Inner node class. EleType value;... int height; }; int h(struct avlNode * current) { if (current == nil) return -1; return current->height; } void setHeight(struct avlNode * current) { int lch = h(current->leftChild); int rch = h(current->rightChild); if (lch height = 1 + rch; else current->height = 1 + lch; }

AVL Implementation: Rotations struct avlNode * rotateLeft(struct avlNode * n) { // Rotate node left. struct avlNode *newtop = n->right; // Right child becomes new “top” node. n->right = newtop->left; // Old right child’s left child becomes new right child. newtop->left = n; // new top left child is old top. setHeight(n); // Reset the height of the interior node. setHeight(newtop); // Reset the height of the top node. return newtop; } struct avlNode * rotateRight (struct avlNode * n) {.. // is similar } 3 (0) 2 (1) 1 (2) 3 (0) 1 (0) 2 (1) Rotate left

AVL Trees: Rotation Pseudocode Pseudocode to rotate current (“top”) node left: 1.New top node is the current node’s right child 2.Current node’s new right child is the new top node’s (old right child’s) left child 3.New top’s left child is the current node 4.Set height of current node 5.Set height of new top node 6.Return new top node 1 (0) 2 (3) 6 (0) 3 (0) 4 (2) 5 (1) 1 (0) 2 (1) 6 (0) 3 (0) 4 (2) 5 (1) Rotate left Current “top” node New “top” node

AVL Trees: Double Rotation Sometimes a single rotation will not fix the problem : –Happens when an insertion is made on the left (or right) side of a node that is itself a heavy right (or left) child 2 (0) 1 (2) Unbalanced “top” node 3 (1) “Heavy” right child 2 (0) 3 (2) 1 (1) Rotate left Doesn’t work!!!

AVL Trees: Double Rotation Sometimes a single rotation will not fix the problem : –Happens when an insertion is made on the left (or right) side of a node that is itself a heavy right (or left) child Fortunately, this case is easily handled by rotating the child before the regular rotation: 1.First rotate the heavy right (or left) child to the right (or left) 2.Rotate the “top” node to the left (or right) 2 (0) 1 (2) Unbalanced “top” node 3 (1) “Heavy” right child Rotate heavy child right 3 (0) 2 (1) 1 (2) 3 (0) 1 (0) 2 (1) Rotate top node left

AVL Trees: Balacing Pseudocode Balancing pseudocode (to rebalance an unbalanced node) : If left child is tallest: If left child is heavy on the right side: // Double rotation needed. Rotate the left child to the left Rotate unbalanced (“top”) node to the right Else: // Right child is the tallest. If right child is heavy on the left side: // Double rotation needed. Rotate the right child to the right Rotate unbalanced (“top”) node to the left Return new “top” node

3 (3) 9 (0) 1 (0) 8 (2) 2 (1) 4 (0) 5 (1) 6 (0) AVL Trees: Double Rotation Example Add data: 7 Balanced Tree “Heavy” left child Unbalanced Tree 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (2) 7 (0) 6 (1) Added to right side of heavy left child Unbalanced “top” node

Tree Still Unbalanced 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (2) 7 (0) 6 (1) Single rotation AVL Trees: Double Rotation Example Unbalanced Tree 3 (4) 1 (0) 2 (1) 7 (0) 6 (1) 9 (0) 8 (2) 5 (3) 4 (0) Unbalanced “top” node (still)

Tree Still Unbalanced, but … Rotate heavy child AVL Trees: Double Rotation Example “Heavy” left child 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (1) 7 (0) 6 (2) 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (2) 7 (0) 6 (1) Unbalanced Tree

Tree Now Balanced Rotate top node AVL Trees: Double Rotation Example Unbalanced Tree (after 1 st rotation) 3 (3) 1 (0) 2 (1) 7 (0) 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (1) 7 (0) 6 (2) Unbalanced “top” node 4 (0) 5 (1) 6 (2) 9 (0) 8 (1)

AVL Trees head to head with skip lists How do they compare to skip lists? Usually faster And, they use less memory There are other types of height balanced trees. The JAVA library uses red/black trees (class TreeSet). Similar idea, slightly faster still.

AVL Trees: Tree Sort Deja Vu An AVL tree can easily implement SortAlgorithm : 1.Copy the values of the data into the tree 2.Copy them out using an in-order traversal Execution time  O(n log n): –Matches that of quick sort in benchmarks –Unlike quick sort, AVL trees don’t have problems if data is already sorted or almost sorted (which degrades quick sort to O(n 2 )) However, requires extra storage to maintain both the original data buffer (e.g., a dyArray ) and the tree structure

Now Your Turn Any Questions Going to do a quick overview of a few other types of trees we won’t be looking at, Then do worksheet Start by showing AVL insertions of 1 to 7 into an empty tree