{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COP3502_31_AVLTrees1

# COP3502_31_AVLTrees1 - AVL Trees Insertion Computer Science...

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

Computer Science Department University of Central Florida AVL Trees: Insertion COP 3502 – Computer Science I

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

View Full Document
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
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

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

View Full Document
AVL Trees: Insertion page 4 Motivation for AVL Trees Recall the basics of Binary Search Trees (a) An unbalanced BST (b) A balanced BST
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).

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

View Full Document
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."
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

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

View Full Document
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).
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 However, the worst case for a balanced tree is 10 tests.

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 ]}