C++ Programming:. Program Design Including

Slides:



Advertisements
Similar presentations
Rizwan Rehman Centre for Computer Studies Dibrugarh University
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.
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 (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.
One more definition: A binary tree, T, is balanced if T is empty, or if abs ( height (leftsubtree of T) - height ( right subtree of T) )
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
CS202 - Fundamental Structures of Computer Science II
1 AVL Trees Drozdek Section pages
AVL Trees Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
1 Theory I Algorithm Design and Analysis (4 – AVL trees: deletion) Prof. Th. Ottmann.
AVL Trees / Slide 1 Delete Single rotation Deletion.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
1 Theory I Algorithm Design and Analysis (3 - Balanced trees, AVL trees) Prof. Th. Ottmann.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
AVL Tree Definition: Theorem (Adel'son-Vel'skii and Landis 1962):
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
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 Using Java1 Chapter 10 Binary Trees.
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.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
AVL Tree.
Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee.
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,
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
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.
AVL Tree: Balanced Binary Search Tree 9.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Data Structure and Algorithms
AA Trees.
Binary Search Tree (BST)
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees binary tree for every node x, define its balance factor
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.
Data Structures Using C++ 2E
Data Structures Using Java
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms
Binary Search Trees Chapter 7 Objectives
CS202 - Fundamental Structures of Computer Science II
Data Structures Lecture 21 Sohail Aslam.
CS202 - Fundamental Structures of Computer Science II
Data Structures Using C++ 2E
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

C++ Programming:. Program Design Including C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: AVL Trees

Objectives In this section you will: Learn about AVL trees

AVL Trees Definition: A perfectly balanced binary tree is a binary tree such that: i. The heights of the left and right subtrees of the root are equal. ii. The left and right subtrees of the root are perfectly balanced binary trees.

Let T be a binary tree and x be a node in T. Then the height of the left subtree of x is the same as the height of the right subtree of x. It can be proved that if T is a perfectly balanced binary tree of height h, then the number of nodes in T is 2h1. If the number of items in the data set is not equal to 2h1, for some nonnegative integer h, then we cannot construct a perfectly balanced binary tree. Definition: An AVL tree (or height-balanced tree) is a binary search tree such that: i. The height of the left and right subtrees of the root differ by at most 1. ii. The left and right subtrees of the root are AVL trees.

Theorem: Let T be an AVL tree and x be a node in T. Then xr − xl  1, where xr − xl denotes the absolute value of xr − xl. Let x be a node in the AVL tree T. 1. If xl > xr, we say that x is left high. In this case, xl = xr + 1. 2. If xl = xr, we say that x is equal high. If xr > xl, we say that x is right high. In this case, xr = xl + 1. Definition: The balance factor of x, written bf(x), is defined by bf(x) = xr − xl. Let x be a node in the AVL tree T. Then: 1. If x is left high, then bf(x) = −1. 2. If x is equal high, then bf(x) = 0. 3. If x is right high, then bf(x) = 1.

Definition: Let x be a node in a binary tree Definition: Let x be a node in a binary tree. We say that the node x violates the balance criteria if xr − xl  1, that is, if the height of the left and right subtrees of x differ by more than 1. The following defines the node of an AVL tree:

To insert an item in an AVL tree, first we search the tree and find the place where the new item is to be inserted. Because an AVL tree is a binary search tree, to find the place for the new item we can search the AVL tree using a search algorithm similar to the one designed for binary search trees. If the item to be inserted is already in the tree, then the search ends at a nonempty subtree. Because duplicates are not allowed, in this case we can output an appropriate error message. Suppose that the item to be inserted is not in the AVL tree. Then the search ends at an empty subtree and we insert the item in that subtree.

After inserting the new item in the tree, the resulting tree might not be an AVL tree. Thus, we must restore the tree’s balance criteria. This is accomplished by traveling the same path (back to the root node) that was followed when the new item was inserted in the AVL tree. The nodes on this path (back to the root node) are visited and either their balance factors are changed, or we might have to reconstruct part of the tree.

The reconstruction procedure is called rotating the tree. There are two types of rotations, left rotation and right rotation. Suppose that the rotation occurs at node x. If it is a left rotation, then certain nodes from the right subtree of x move to its left subtree; the root of the right subtree of x becomes the new root of the reconstructed subtree. If it is a right rotation at x, certain nodes from the left subtree of x move to its right subtree; the root of the left subtree of x becomes the new root of the reconstructed subtree.

In Figure AVL-16, subtrees T1, T2, and T3 are of equal height, say h In Figure AVL-16, subtrees T1, T2, and T3 are of equal height, say h. The dotted rectangle shows an item insertion in T1, causing the height of the subtree T1 to increase by 1. The subtree at node a is still an AVL tree, but the balance criteria is violated at the root node. We note the following in this tree. Because the tree is a binary search tree: Every key in T1 is smaller than the key in node a. Every key in T2 is larger than the key in node a. Every key in T2 is smaller than the key in node b. Therefore: We make T2 (the right subtree of node a) the left subtree of node b. We make node b the right child of node a. Node a becomes the root node of the reconstructed tree, as shown in Figure AVL-16.

In Figure AVL-18, the dotted rectangle shows that a new item is inserted in the subtree, T2 or T3, causing the subtree to grow in height. All keys in T3 are smaller than the key in node c. All keys in T3 are larger than the key in node b. All keys in T2 are smaller than the key in node b. All keys in T2 are larger than the key in node a. After insertion, the subtrees with root nodes a and b are still AVL trees. The balance criteria is violated at the root node, c, of the tree. The balance factors of node c, bf(c) = −1, and node a, bf(a) = 1, are opposite. This is an example of double rotation. One rotation is required at node a and another rotation is required at node c.

If the balance factor of the node where the tree is to be reconstructed and the balance factor of the higher subtree are opposite, that node requires a double rotation. First we rotate the tree at node a and then at node c. Now the tree at node a is right high, so we make a left rotation at a. Next, because the tree at node c is left high, we make a right rotation at c. Figure AVL-18 shows the resulting tree (which is to the right of the tree after insertion). Figure AVL-19, however, shows both rotations in sequence.

Suppose that the tree is to be reconstructed, by rotation, at node x Suppose that the tree is to be reconstructed, by rotation, at node x. Then the subtree with the root node x requires either a single or a double rotation. 1. Suppose that the balance factor of the node x and the balance factor of the root node of the higher subtree of x have the same sign, that is, both positive or both negative. If these balance factors are positive, make a single left rotation at x. If these balance factors are negative, make a single right rotation at x. 2. Suppose that the balance factor of the node x and the balance factor of the higher subtree of x are opposite in sign. To be specific, suppose that the balance factor of the node x prior to insertion was −1 and suppose that y is the root node of the left subtree of x. After insertion, the balance factor of the node y is 1. That is, after insertion, the right subtree of node y grew in height. In this case, we require a double rotation at x. First we make a left rotation at y (because y is right high). Then we make a right rotation at x.

The following steps describe the function insertIntoAVL: Create a node and assign the item to be inserted to the info field of this node. Search the tree and find the place for the new node in the tree. Insert the new node in the tree. Backtrack the path, which was constructed to find the place for the new node in the tree, to the root node. If necessary, adjust the balance factors of the nodes, or reconstruct the tree at a node on the path. The function insertIntoAVL also uses a bool member variable, isTaller, to indicate to the parent whether the subtree grew in height or not.

To delete an item from an AVL tree, first we find the node containing the item to be deleted. The following four cases arise: Case 1: The node to be deleted is a leaf. Case 2: The node to be deleted has no right child, that is, its right subtree is empty. Case 3: The node to be deleted has no left child, that is, its left subtree is empty. Case 4: The node to be deleted has a left child and a right child.

Consider all the possible AVL trees of height h Consider all the possible AVL trees of height h. Let Th be an AVL tree of height h such that Th has the fewest number of nodes. Let Thl denote the left subtree of Th and Thr denote the right subtree of Th. Then where |Th| denotes the number of nodes in Th.

Because Th is an AVL tree of height h such that Th has the fewest number of nodes, it follows that one of the subtrees of Th is of height h − 1 and the other is of height h − 2. To be specific, suppose that Thl is of height h − 1 and Thr is of height h − 2. From the definition of Th, it follows that Thl is an AVL tree of height h − 1 such that Thl has the fewest number of nodes among all AVL trees of height h − 1. Similarly, Thr is an AVL tree of height h −2 that has the fewest number of nodes among all AVL trees of height h − 2. Thus, Thl is of the form Th-1 and Thr is of the form Th-2. Hence,