Deletion from an AVL Tree

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)

From Last Time: Remove (Delete) Operation

TREE BASIC TERMINOLOGIES

Fundamental Algorithms

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

Analysis of Algorithms I: Binary Search Trees

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

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

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

Full and Complete Binary Trees

Ordered Lists and Binary Trees

Binary Search Trees CMPSC 122

Chapter 14 The Binary Search Tree

Binary Search Trees 3/20/14

Symbol Tables. Introduction

Data Structures Fibonacci Heaps, Amortized Analysis

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

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

Data Structures and Algorithms

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

Laboratory Module 6 Red-Black Trees

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

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

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

Algorithms and Data Structures

Binary Heaps. CSE 373 Data Structures

Binary Search Trees. Ric Glassey

Converting a Number from Decimal to Binary

Persistent Binary Search Trees

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

Algorithms Chapter 12 Binary Search Trees

Binary Search Trees (BST)

A Comparison of Dictionary Implementations

6 March Array Implementation of Binary Trees

Why Use Binary Trees?

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

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:

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

Big Data and Scripting. Part 4: Memory Hierarchies

Binary Trees and Huffman Encoding Binary Search Trees

- 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

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

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

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

GRAPH THEORY LECTURE 4: TREES

The ADT Binary Search Tree

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

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

M-way Trees and B-Trees

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

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

Lecture Notes on Binary Search Trees

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

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

Classification/Decision Trees (II)

Algorithms and Data Structures

Any two nodes which are connected by an edge in a graph are called adjacent node.

An Immediate Approach to Balancing Nodes of Binary Search Trees

Data Structure with C

Rotation Operation for Binary Search Trees Idea:

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

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

Lecture Notes on Binary Search Trees

An Evaluation of Self-adjusting Binary Search Tree Techniques

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

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

DATABASE DESIGN - 1DL400

Data Structure [Question Bank]

Binary Coded Web Access Pattern Tree in Education Domain

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

Optimal Binary Search Trees Meet Object Oriented Programming

International Journal of Software and Web Sciences (IJSWS)

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

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

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

CS711008Z Algorithm Design and Analysis

Physical Data Organization

Structural Design Patterns Used in Data Structures Implementation

Efficient representation of integer sets

Section IV.1: Recursive Algorithms and Recursion Trees

Binary Heap Algorithms

Introduction to data structures

Simple Balanced Binary Search Trees

External Memory Geometric Data Structures

A Randomized Self-Adjusting Binary Search Tree

Analysis of Algorithms I: Optimal Binary Search Trees

Alex. Adam Agnes Allen Arthur

Introduction to Data Structures and Algorithms

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

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)

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

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

Vector storage and access; algorithms in GIS. This is lecture 6

Data Structures UNIT III. Model Question Answer

BIRCH: An Efficient Data Clustering Method For Very Large Databases

Lecture 4: Balanced Binary Search Trees

Spatial Data Management over Flash Memory

State History Storage in Disk-based Interval Trees

OPTIMAL BINARY SEARCH TREES

Transcription:

Deletion from an AVL Tree First we will do a normal binary search tree delete. Note that structurally speaking, all deletes from a binary search tree delete nodes with zero or one child. For deleted leaf nodes, clearly the heights of the children of the node do not change. Also, the heights of the children of a deleted node with one child do not change either. Thus, if a delete causes a violation of the AVL Tree height property, this would HAVE to occur on some node on the path from the parent of the deleted node to the root node. Thus, once again, as above, to restructure the tree after a delete we will call the restructure method on the parent of the deleted node. One thing to node: whereas in an insert there is at most one node that needs to be unbalanced, there may be multiple nodes in the delete that need to be rebalanced. Technically speaking, at any point in the restructuring algorithm ONLY one node will ever be unbalanced. But, what may happen is when that node is fixed, it may propogate an error to an ancestor node. But, this is NOT a problem because our restructuring algorithm goes all the way to the root node. F Let's trace through a couple examples each for inserts and deletes, using the code handout.

AVL Tree Examples 1) Consider inserting 46 into the following AVL Tree: 36 44 52 60 \ 46, inserted here Initially, using the standard binary search tree insert, 46 would go to the right of 44. Now, let's trace through the rebalancing process from this place. First, we call the method on this node. Once we set its height, we check to see if the node is balanced. (This simply looks up the heights of the left and right subtrees, and decides if the difference is more than 1.) In this case, the node is balanced, so we march up to the parent node, that stores 44. We will trace through the same steps here, setting the new height of this node (this is important!) and determining that this node is balanced, since its left subtree has a height of -1 and the right subtree has a height of 0. Similarly, we set the height and decide that the nodes storing 40 and 48 are balanced as well. Finally, when we reach the root node storing, we realize that our tree is imbalanced.

Now, we finally get to execute the code inside the if statement in the rebalance method. Here we set xpos to be the tallest grandchild of the root node. (This is the node storing 40, since its height is 2.) Thus, the restructuring occurs on the nodes containing the, 48 and 40. Using the method described from last lecture, we will restructure the tree as follows: 40 48 16 36 44 56 \ 8 24 46 52 60 Using the variables from the last lecture, the node storing 40 is B, the node storing is A, and the node storing 48 is C. T 0 is the subtree rooted at 16, T 1 is the subtree rooted at 36, T 2 is the subtree rooted at 44, and T 3 is the subtree rooted at 56. 2) Now, for the second example, consider inserting 61 into the following AVL Tree: / 4 36 44 52 60 / 61, inserted

Tracing through the code, we find the first place an imbalance occurs tracing up the ancestry of the node storing 61 is at the noce storing 56. This time, we have that node A stores 56, node B stores 60, and node C stores 62. Using our restucturing algorithm, we find the tallest grandchild of 56 to be 62, and rearrange the tree as follows: 8 24 40 60 / 4 36 44 56 62 / 52 58 61 T 0 is the subtree rooted at 52, T 1 is the subtree rooted at 58, T 2 is the subtree rooted at 61, and T 3 is a null subtree. 3) For this example, we will delete the node storing 8 from the AVL tree below: \ 28 36 44 52 60 Tracing through the code, we find that we must first call the rebalance method on the parent of the deleted node, which

stores 16. This node needs rebalancing and gets restructured as follows: 24 48 16 28 40 56 36 44 52 60 Notice that all four subtrees for this restructuring are null, and we only use the nodes A, B, and C. Next, we march up to the parent of the node storing 24, the node storing. Once again, this node is imbalanced. The reason for this is that the restructuring of the node with a 16 reduced the height of that subtree. By doing so, there was in INCREASE in the difference of height between the subtrees of the old parent of the node storing 16. This increase could propogate an imbalance in the AVL tree. When we restructure at the node storing the, we identify the node storing the 56 as the tallest grandchild. Following the steps we've done previously, we get the final tree as follows:

48 56 24 40 52 60 16 28 36 44 4) The final example, we will delete the node storing 4 from the AVL tree below: / 4 36 44 52 60 When we call rebalance on the node storing an 8, (the parent of the deleted node), we do NOT find an imbalance at an ancestral node until we get to the root node of the tree. Here we once again identify the node storing as node A, the node storing 48 as node B and the node storing 56 as node C. Accordingly, we restructure as follows: 48 56 16 40 52 60 8 24 36 44