Fundamental Algorithms



Similar documents
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

How To Create A Tree From A Tree In Runtime (For A Tree)

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

From Last Time: Remove (Delete) Operation

Converting a Number from Decimal to Binary

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Analysis of Algorithms I: Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

6 March Array Implementation of Binary Trees

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

Binary Search Trees CMPSC 122

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Full and Complete Binary Trees

TREE BASIC TERMINOLOGIES

Cpt S 223. School of EECS, WSU

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

CSE 326: Data Structures B-Trees and B+ Trees

M-way Trees and B-Trees

Symbol Tables. Introduction

Persistent Binary Search Trees

Exam study sheet for CS2711. List of topics

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

An Evaluation of Self-adjusting Binary Search Tree Techniques

Binary Heaps. CSE 373 Data Structures

Ordered Lists and Binary Trees

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Big Data and Scripting. Part 4: Memory Hierarchies

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

M(0) = 1 M(1) = 2 M(h) = M(h 1) + M(h 2) + 1 (h > 1)

A Comparison of Dictionary Implementations

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Binary Heap Algorithms

Laboratory Module 6 Red-Black Trees

Binary Search Trees 3/20/14

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

CS711008Z Algorithm Design and Analysis

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

IMPLEMENTING CLASSIFICATION FOR INDIAN STOCK MARKET USING CART ALGORITHM WITH B+ TREE

Data Structures and Algorithms

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

The Goldberg Rao Algorithm for the Maximum Flow Problem

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Trees and Huffman Encoding Binary Search Trees

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

KDDCS: A Load-Balanced In-Network Data-Centric Storage Scheme for Sensor Networks

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Analysis of Algorithms I: Optimal Binary Search Trees

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Binary Search Trees (BST)

1/1 7/4 2/2 12/7 10/30 12/25

Randomized Binary Search Trees

External Memory Geometric Data Structures

Lecture 4: Balanced Binary Search Trees

Classification/Decision Trees (II)

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Algorithms and Data Structures

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Chapter 14 The Binary Search Tree

Persistent Data Structures and Planar Point Location

Lecture Notes on Binary Search Trees

Algorithms and Data Structures

Lecture Notes on Binary Search Trees

Mathematical Induction. Lecture 10-11

Databases and Information Systems 1 Part 3: Storage Structures and Indices

Lecture 1: Course overview, circuits, and formulas

GRAPH THEORY LECTURE 4: TREES

Simple Balanced Binary Search Trees

Data storage Tree indexes

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Section IV.1: Recursive Algorithms and Recursion Trees

The Cost of Offline Binary Search Tree Algorithms and the Complexity of the Request Sequence

Merkle Hash Trees for Distributed Audit Logs

Algorithms Chapter 12 Binary Search Trees

Why Use Binary Trees?

6.852: Distributed Algorithms Fall, Class 2

PES Institute of Technology-BSC QUESTION BANK

Data Structure with C

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Optimal Binary Search Trees Meet Object Oriented Programming

Transcription:

Fundamental Algorithms Chapter 6: AVL Trees Michael Bader Winter 2011/12 Chapter 6: AVL Trees, Winter 2011/12 1

Part I AVL Trees Chapter 6: AVL Trees, Winter 2011/12 2

Binary Search Trees Summary Complexity of Searching: worst-case complexity depends on height of the search trees O(log n) for balanced trees Inserting and Deleting: insertion and deletion might change balance of trees question: how expensive is re-balancing? Test: Inserting/Deleting into a (fully) balanced tree strict balancing (uniform depth for all leaves) too strict Chapter 6: AVL Trees, Winter 2011/12 3

Height Balance Definition (height balance) Let h(x) be the height of a binary tree x. Then, the height balance b(x.key) of a node x.key is defined as b(x.key) := h(x.rightchild) h(x.leftchild) i.e. the difference of the heights of the two subtrees of x.key. Example: Chapter 6: AVL Trees, Winter 2011/12 4

AVL Trees Definition (AVL tree) A binary search tree x is called an AVL tree, if: 1. b(x.key) { 1, 0, 1}, and 2. x.leftchild and x.rightchild are both AVL trees. = the height balance of every node must be -1, 0, or 1 Chapter 6: AVL Trees, Winter 2011/12 5

Number of Nodes in an AVL Tree Crucial Question for Complexity: What is the maximal and minimal number of nodes that can be stored in an AVL tree of given height h? Maximal number: a full binary tree has a height balance of 0 for every node thus: full binary trees are AVL trees number of nodes: 2 h 1 nodes. Chapter 6: AVL Trees, Winter 2011/12 6

Minimal Number of Nodes in an AVL Tree Minimal number: A minimal AVL tree of height h consists of a root node one subtree that is a minimal AVL tree of height h 1 one subtree that is a minimal AVL tree of height h 2 leads to recurrence: N minavl (h) = 1 + N minavl (h 1) + N minavl (h 2) In addition, we know that a minimal AVL tree of height 1 has 1 node: N minavl (1) = 1 a minimal AVL tree of height 2 has 2 nodes: N minavl (2) = 2 Chapter 6: AVL Trees, Winter 2011/12 7

Minimal Number of Nodes in an AVL Tree (2) Solve recurrence: N minavl (h) = 1 + N minavl (h 1) + N minavl (h 2) N minavl (1) = 1 N minavl (2) = 2 Compare with Fibonacci numbers: f n = f n 1 + f n 2, f 0 = f 1 = 1 h 1 2 3 4 5 6 7 8 f h 1 2 3 5 8 13 21 34 N minavl (h) 1 2 4 7 12 20 33 54 Claim: N minavl (h) = f h+1 1 (proof by induction) Chapter 6: AVL Trees, Winter 2011/12 8

Minimal Number of Nodes in an AVL Tree (2) Minimum number of nodes: N minavl (h) = f h+1 1 inequality for Fibonacci numbers: 2 n/2 f n 2 n 2 (h+1)/2 1 N minavl (h) 2 h+1 1 thus: an AVL tree that contains n nodes will be of height Θ(log n) Corollaries: Searching in an AVL tree has a time complexity of Θ(log n) Inserting, or deleting a single element in an AVL tree has a time complexity of Θ(log n) BUT: standard inserting/deleting will probably destroy the AVL property. Chapter 6: AVL Trees, Winter 2011/12 9

Part II Algorithms on AVL Trees Chapter 6: AVL Trees, Winter 2011/12 10

Inserting and Deleting on AVL Trees General Concept: insert/delete via standard algorithms after insert/delete: load balance b(node) might be changed to +2 or 2 for certain nodes re-balance load after each step Requirements: re-balancing must have O(log n) worst-case complexity Solution: apply certain rotation operations Chapter 6: AVL Trees, Winter 2011/12 11

Left Rotation Situation: height balance of the node is +2 (or larger), and height balance of the right subtree is 0, or +1 can reduce height of the subtree and require further rotations Chapter 6: AVL Trees, Winter 2011/12 12

Left Rotation Implementation LeftRotAVL ( x : BinTree ) { } x := ( x. r i g h t C h i l d. key, ( x. key, x. l e f t C h i l d, x. r i g h t C h i l d. l e f t C h i l d ), x. r i g h t C h i l d. r i g h t C h i l d ) ; Chapter 6: AVL Trees, Winter 2011/12 13

Right Rotation Situation: height balance of the node is -2 (or smaller), and height balance of the left subtree is 0, or -1 can reduce height of the subtree and require further rotations Chapter 6: AVL Trees, Winter 2011/12 14

Right Rotation Implementation RightRotAVL ( x : BinTree ) { } x := ( x. l e f t C h i l d. key, x. l e f t C h i l d. l e f t C h i l d, ( x. key, x. l e f t C h i l d. r i g h t C h i l d, x. r i g h t C h i l d ) ) ; Chapter 6: AVL Trees, Winter 2011/12 15

Right-Left Rotation Situation: height balance of the node is +2, and height balance of the right subtree is -1 left rotation successful? left rotation is not sufficient to restore the AVL property Chapter 6: AVL Trees, Winter 2011/12 16

Solution: Two Successive Rotations Chapter 6: AVL Trees, Winter 2011/12 17

Right-Left Rotation Implementation RightRotLeftRot ( x : BinTree ) { RightRotAVL ( x. r i g h t C h i l d ) ; LeftRotAVL ( x ) ; } In a single procedure: RightLeftRotAVL ( x : BinTree ) { x := ( x. r i g h t C h i l d. l e f t C h i l d. key, ( x. key, x. l e f t C h i l d, x. r i g h t C h i l d. l e f t C h i l d. l e f t C h i l d ), ( x. r i g h t C h i l d. key, x. r i g h t C h i l d. l e f t C h i l d. r i g h t C h i l d, x. r i g h t C h i l d. r i g h t C h i l d ) ) ; } Chapter 6: AVL Trees, Winter 2011/12 18

Left-Right Rotation Situation: height balance of the node is -2, and height balance of the right subtree is +1 Analogous to Right-Left Rotation: again, right rotation is not sufficient to restore the AVL property right rotation followed by a left rotation Chapter 6: AVL Trees, Winter 2011/12 19

Left-Right Rotation Scheme Chapter 6: AVL Trees, Winter 2011/12 20

Left-Right Rotation Implementation LeftRotRightRot ( x : BinTree ) { LeftRotAVL ( x. l e f t C h i l d ) ; RightRotAVL ( x ) ; } In a single procedure: LeftRightRotAVL ( x : BinTree ) { x := ( x. l e f t C h i l d. r i g h t C h i l d. key, ( x. l e f t C h i l d. key, x. l e f t C h i l d. l e f t C h i l d, x. l e f t C h i l d. r i g h t C h i l d. l e f t C h i l d ), ( x. key, x. l e f t C h i l d. r i g h t C h i l d. r i g h t C h i l d, x. r i g h t C h i l d ) ) ; } Chapter 6: AVL Trees, Winter 2011/12 21

Effect of Rotation Operations Observations: LeftRightRot and RightLeftRot always reduce the height of the (sub-)tree by 1. LeftRotAVL and RightRotAVL reduce the height by at most 1 (might keep it unchanged) thus: AVL property can be violated in a parent node! After inserting one node into an AVL tree: at most one rotation required to restore AVL property Chapter 6: AVL Trees, Winter 2011/12 22

Effect of Rotation Operations (2) After inserting one node into an AVL tree: at most one rotation required to restore AVL property After deleting one node of an AVL tree: up to log n rotations can be required to restore AVL property For insertion and deletion: imbalance might occur on a much higher level thus: AVL property has to checked in the entire branch of the tree (up to the root) Corollary: Time complexity for deleting, inserting, and searching in an AVL tree is O(log n) Chapter 6: AVL Trees, Winter 2011/12 23