COP3502_31_AVLTrees1 - AVL Trees: Insertion Computer...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida AVL Trees: Insertion COP 3502 – Computer Science I
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: Insertion page 2 Motivation for AVL Trees Recall the basics of Binary Search Trees The goal of a BST is to provide O(log n) lookup, insertion, deletion, etc. However, this goal is only accomplished on a “complete” binary tree a tree where all levels are filled with the possible exception of the last level, which is filled from left to right Given a complete BST, the height of the tree is approximately log n, where n is the number of nodes Remember: If a BST is not complete, the height is NOT necessarily logn
Background image of page 2
AVL Trees: Insertion page 3 Motivation for AVL Trees Recall the basics of Binary Search Trees The height of a BST depends on the order of insertion Example: Inserting values 1, 2, 3, 4, 5, 6, and 7 into an initially empty BST results in what? Each new values ends up going to the “right” of the previous value So we end up with a completely right-skewed tree This “tree” has degenerated into a linked list with respect to the running time of operations
Background image of page 3

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

View Full DocumentRight Arrow Icon
AVL Trees: Insertion page 4 Motivation for AVL Trees Recall the basics of Binary Search Trees (a) An unbalanced BST (b) A balanced BST
Background image of page 4
AVL Trees: Insertion page 5 Motivation for AVL Trees This “tree” is just a linked list in binary tree clothing. It takes 2 tests to locate 12, 3 to locate 14, and 8 to locate 52. Hence, the search effort for this binary tree is O(n).
Background image of page 5

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

View Full DocumentRight Arrow Icon
AVL Trees: Insertion page 6 Motivation for AVL Trees Balanced BST We want to maintain balance in our BSTs Is there a way, regardless of the insertion order of elements, to maintain this balance? To guarantee a height of log(n)? Basically, can we keep this balance? Short answer: yes! AVL Trees: G.M. Adelson-Velskii and E.M. Landis Published their algorithm in 1962 in a paper entitled "An algorithm for the organization of information."
Background image of page 6
AVL Trees: Insertion page 7 AVL Trees AVL Tree Definition: An AVL tree is a BST in which the heights of the subtrees, of any given node, differ by no more than 1 For EVERY node in a BST, you must check the height of the left and right subtree of that node If the height of those subtrees differ by no more than 1, then that BST is an AVL tree Thus, an AVL tree is a balanced BST
Background image of page 7

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

View Full DocumentRight Arrow Icon
AVL Trees: Insertion page 8 AVL Trees AVL Tree This BST is an AVL tree. It takes 2 tests to locate 18, 3 to locate 12, and 4 to locate 8. Hence, the search effort for this binary tree is O(log 2 n).
Background image of page 8
AVL Trees: Insertion page 9 AVL Trees AVL Tree For a tree with 1000 nodes, the worst case for a completely unbalanced tree is 1000 tests. Again, degenerating to a linked list
Background image of page 9

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

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

This document was uploaded on 09/21/2011.

Page1 / 57

COP3502_31_AVLTrees1 - AVL Trees: Insertion Computer...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online