AVL Trees. See Section 19.4of the text, p

Similar documents
CS350: Data Structures AA Trees

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

COMS 3137 Class Notes

Binary Search Trees. See Section 11.1 of the text.

CS 310: Tree Rotations and AVL Trees

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Algorithms. AVL Tree

Data Structures in Java

CISC 235: Topic 4. Balanced Binary Search Trees

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Balanced Search Trees. CS 3110 Fall 2010

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

CS 261 Data Structures. AVL Trees

CSI33 Data Structures

Binary Search Trees. Analysis of Algorithms

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Spring 2018 Mentoring 8: March 14, Binary Trees

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Balanced Binary Search Trees

3137 Data Structures and Algorithms in C++

Data Structures and Algorithms

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees

A dictionary interface.

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

AVL Trees Heaps And Complexity

Balanced Binary Search Trees

COMP Analysis of Algorithms & Data Structures

Data Structures Lesson 7

CSI33 Data Structures

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Lesson 21: AVL Trees. Rotation

COMP Analysis of Algorithms & Data Structures

Ch04 Balanced Search Trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Week 3 Web site:

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Module 4: Dictionaries and Balanced Search Trees

lecture17: AVL Trees

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi

Data Structures in Java

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

LECTURE 18 AVL TREES

COMP171. AVL-Trees (Part 1)

COMP Analysis of Algorithms & Data Structures

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

CMPE 160: Introduction to Object Oriented Programming

Trees. (Trees) Data Structures and Programming Spring / 28

Self-Balancing Search Trees. Chapter 11

Implementations II 1

More BSTs & AVL Trees bstdelete

CS350: Data Structures Tree Traversal

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Part 2: Balanced Trees

Binary search trees (chapters )

8. Binary Search Tree

CS350: Data Structures AVL Trees

Trees. A tree is a directed graph with the property

Lecture 13: AVL Trees and Binary Heaps

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Advanced Tree Data Structures

CSE 373: AVL trees. Michael Lee Friday, Jan 19, 2018

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Binary search trees (chapters )

CSCI2100B Data Structures Trees

CS Transform-and-Conquer

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Second Exam SAMPLE Computer Programming 338 Dr. St. John Lehman College City University of New York Tuesday, 5 April 2011

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

CS350: Data Structures Red-Black Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Implementations II 1

Algorithms and Data Structures

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Module 4: Priority Queues

INF2220: algorithms and data structures Series 1

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CS 315 Data Structures mid-term 2

CS102 Binary Search Trees

Analysis of Algorithms

n 1 i = n + i = n + f(n 1)

Balanced Binary Search Trees. Victor Gao

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

CS350: Data Structures Binary Search Trees

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

COMP : Trees. COMP20012 Trees 219

Binary Search Tree - Best Time. AVL Trees. Binary Search Tree - Worst Time. Balanced and unbalanced BST

Transcription:

AVL Trees See Section 19.4of the text, p. 706-714.

AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that the remains a logarithm of the number of nodes. No matter what order we insert the nodes, we can search an AVL in O( log(n) ) time. AVL trees were the first such self-balancing trees. They were invented by Georgy Adelson-Velski and Evgenii Landis 1962.

Definition: An AVL tree is a Binary Search Tree with the additional property that for every node in the tree, the left and right subtrees have that differ by at most 1. We say that the of a null tree is -1 and the of a single node is 0; the of any other node is 1 more than the max of the s of its children. Here are some AVL trees 0 1 2 0 1 0 0 0

Here is a tree that is not an AVL tree; the children of the root have s that differ by 2: 3 2 0 1 1 0 0

One issue with implementing AVL trees is having access to the of each node. We change the Node class so it has fields E data; int ; Node left, right;

We also change the insert() and remove() methods to adjust the at each node on the path between the root and the modified node. The recursive version of the insert method is easy to modify. After we come back from the recursive call we reevaluate the :

private Node insert(e x, Node t) { if (t == null) { Node s = new Node(x); s. = 0; return s; } else { int comparison = x.compareto(t.data); if (comparison == 0) t.data = x; else if (comparison < 0) t.left = insert(x, t.left); else t.right = insert(x, t.right); t. = 1+ max( (t.left), (t.right)); return t; } }

This much is easy; the interesting part of AVL trees comes lies in the adjustments we need to do when a tree becomes imbalanced. Consider inserts. We have a balanced tree and insert a node and the tree is then unbalanced. Since inserting can add at most one to the of a subtree this must mean that we have a node on the path between the root and the inserted node where the of one child is 2 more than the of the other. There are 4 possible cases:

a) The insertion was in the left subtree of the left child of b) The insertion was in the right subtree of the left child of c) The insertion was in the left subtree of the right child of d) The insertion was in the right subtree of the right child of

Consider case (a): h h+1 h Suppose that after the insert the left child of Node has h+2, while the right child has h.

We modify this as follows: b a h h+1 a b h+1 h h h

Note that we produce this new tree by just reassigning a few pointers, so it is quick to produce. It is easy to check that this is a BST tree, and it now satisfies the AVL property: the two children of the top node now have h+1. Note that the top node has h+2, which is what it had before the insertion, so no further changes are needed in the tree.

Here is code for this: Node rotatewithleftchild( Node ) { Node =.left;.left =.right;.right = ; return ; }

Example: 23 17 17 45 9 23 9 21 32 61 4 13 21 45 4 13 22 2 22 32 61 2

There is a symmetric case for inserting into the right subtree of the right child of : A h B A B h+1 h h+1 h h

The code for this is: Node rotatewithrightchild( Node ) { Node =.right;.right =.left;.left = ; return ; }

We started out with 4 cases: a) Insertion in the left subtree of the left child of b) Insertion in the right subtree of the left child of c) Insertion in the left subtree of the right child of d) Insertion in the right subtree of the right child of Our simple rotations have fixed cases (a) and (d). The other cases are a bit more complex and require a double rotation.

Consider this example, where we have just inserted 20. The left child of node 23 has 3, the right child has 1. 23 23 17 45 21 45 9 21 32 61 17 22 32 61 4 13 22 19 9 19 20 4 13 We first do a rotation from node 21 to node 17, but that doesn't fix the problem; the left child of 23 has 3, the right child 1. 20

We then rotate node 21 to node 23 and this does fix the problem: 23 21 21 45 17 23 17 22 32 61 9 19 22 45 9 19 4 13 20 4 13 20 32 61

In general, if we have a situation like this, where we have inserted into the right child of the left child of, we first rotate node to : D D A A B C B C

We then rotate node to : D A B C A B C D Now both children of the root have h+1.

The code for this is simple: Node doublerotationwithleftchild( node ) { Node =.left; Node =.right;.left = rotatewithrightchild(); return rotatewithleftchild(); } Of course there is a symmetric method: Node doublerotationwithrightchild( node ) { Node =.right; Node =.left;.right = rotatewithleftchild(); return rotatewithrightchild(); }

In Lab 6 we have slightly different notation for this. Let be the unbalanced node, let be 's tallest child, and let be 's tallest child. Now introduce three new variables a, b, c where a is the one of,, with the smallest value, b the one with the middle value, and c the one with the largest value.

For example, in this tree h h+1 h We have a is, b is, and c is.

In this tree a is, b is and c is.

We then let t1, t2, t3 and t4 be the left-to-right children of nodes a, b and c. It turns out that all four cases of our rotation can be reduced to building the tree b a c t1 t2 t3 t4

For example, a t1 b t2 c t3 t4 b c a t1 t2 t3 t4

Here is another example; the remaining two cases are symmetrical to these a t1 c b t4 a b c t1 t2 t3 t4 t2 t3

In Lab 6, once you have identified nodes a, b, and c, and their children t1 through t4, code that is given to you completes the rotation by building the resulting tree in terms of these 7 variables.