CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
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.
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
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.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
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.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Data Structures AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
AVL Trees.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
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.
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
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
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
AVL Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
CSE 373 AVL trees read: Weiss Ch. 4, section
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures and Algorithms
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
AVL-Trees (Part 1).
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 11: Trees III

AVL Tree Motivation Observation: the shallower the BST the better For a BST with n nodes Average case height is (log n) Worst case height is (n) Simple cases such as insert(1, 2, 3, ..., n) lead to the worst case scenario: height (n) Strategy: Don't let the tree get lopsided Constantly monitor balance for each subtree Rebalance subtree before going too far astray

Balanced Tree Balanced Tree: a tree in which heights of subtrees are approximately equal unbalanced tree balanced tree

AVL trees AVL tree: a binary search tree that uses modified add and remove operations to stay balanced as items are added to and remove from it specifically, maintains a balance factor of each node of 0, 1, or -1 i.e. no node's two child subtrees differ in height by more than 1 invented in 1962 by two Russian mathematicians (Adelson-Velskii and Landis) one of several auto-balancing trees (others in book) balance factor, for a tree node n : height of n's right subtree minus height of n's left subtree BFn = Heightn.right - Heightn.left start counting heights at n

AVL tree examples Two binary search trees: (a) an AVL tree (b) not an AVL tree (unbalanced nodes are darkened)

More AVL tree examples -1 1

Not AVL tree examples -2 2 -1 -1 2 1 -2 -1

Which are AVL trees?

AVL Tree Height The height of an AVL Tree is (log n) Justification: Find n(h): the minimum number of nodes in an AVL tree of height h n(0) = 1 and n(1) = 2 For h >= 2, an AVL tree of height h contains the root node, an AVL subtree of height h - 1 and an AVL subtree of height h - 1 or h - 2 n(h) = 1 + n(h - 1) + n(h - 2) Solving this recurrence leads to (log n)

AVL Trees: search, insert, remove AVL search: Same as BST search. AVL insert: Same as BST insert, except you need to check your balance and may need to “fix” the AVL tree after the insert. AVL remove: Remove it, check your balance, and fix it.

Testing the Balance Property We need to be able to: 1. Track Balance Factor 2. Detect Imbalance 3. Restore Balance 10 5 15 2 9 20 We need to know a few things now: How do we track the balance? How do we detect imbalance? How do we restore balance? 7 30

Tracking Balance data 3 height children 2 1 1 10 10 5 20 2 9 15 30 7 height children 2 1 5 20 1 2 9 15 30 Here’s a revision of that tree that’s balanced. (Same values, similar tree) I also have here how we might store the nodes in the AVL tree. Notice that I’m going to keep track of height all the time. WHY? 7

Problem cases for AVL insert 1. LL Case: insertion into left subtree of node's left child 2. LR Case: insertion into right subtree of node's left child

Problem cases for AVL insert, cont. 3. RL Case: insertion into left subtree of node's right child 4. RR Case: insertion into right subtree of node's right child

Maintaining Balance Maintain balance using rotations The idea: locally reorganize the nodes of an unbalanced subtree until they are balanced, by "rotating" a trio of parent - leftChild – rightChild Maintaining balance will result in searches (contains) that take (log n)

Right rotation to fix Case 1 (LL) right rotation (clockwise): left child becomes parent; original parent demoted to right

Right rotation example

Right rotation, steps detach left child (7)'s right subtree (10) (don't lose it!) consider left child (7) be the new parent attach old parent (13) onto right of new parent (7) attach old left child (7)'s old right subtree (10) as left subtree of new right child (13)

Right rotation example

Code for right rotation private StringTreeNode rightRotate(StringTreeNode parent) { // 1. detach left child's right subtree StringTreeNode leftright = parent.left.right; // 2. consider left child to be the new parent StringTreeNode newParent = parent.left; // 3. attach old parent onto right of new parent newParent.right = parent; // 4. attach old left child's old right subtree as // left subtree of new right child newParent.right.left = leftright; parent.height = computeHeight(parent); newParent.height = computeHeight(newParent); return newParent; }