From Last Time: Remove (Delete) Operation



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

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

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

Binary Heaps. CSE 373 Data Structures

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

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

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

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

Ordered Lists and Binary Trees

Analysis of Algorithms I: Binary Search 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

Binary Heap Algorithms

6 March Array Implementation of Binary Trees

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

TREE BASIC TERMINOLOGIES

Converting a Number from Decimal to Binary

Why Use Binary Trees?

Data Structures and Algorithms

Heaps & Priority Queues in the C++ STL 2-3 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

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

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

A Comparison of Dictionary Implementations

CS711008Z Algorithm Design and Analysis

Algorithms and Data Structures

Fundamental Algorithms

Full and Complete Binary Trees

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

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

Data Structures Fibonacci Heaps, Amortized Analysis

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

Review of Hashing: Integer Keys

Binary Search Trees CMPSC 122

Big Data and Scripting. Part 4: Memory Hierarchies

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

Cpt S 223. School of EECS, WSU

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

Binary Search Trees (BST)

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

PES Institute of Technology-BSC QUESTION BANK

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

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

Binary Search Trees 3/20/14

Data storage Tree indexes

Rotation Operation for Binary Search Trees Idea:

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:

Chapter 14 The Binary Search Tree

M-way Trees and B-Trees

Lecture Notes on Binary Search Trees

International Journal of Software and Web Sciences (IJSWS)

An Evaluation of Self-adjusting Binary Search Tree Techniques

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

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

GRAPH THEORY LECTURE 4: TREES

Analysis of Algorithms I: Optimal Binary Search Trees

Classification/Decision Trees (II)

Algorithms Chapter 12 Binary Search Trees

Lecture Notes on Binary Search Trees

Binary search algorithm

Physical Data Organization

Optimal Binary Search Trees Meet Object Oriented Programming

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

Data Structures For IP Lookup With Bursty Access Patterns

Alex. Adam Agnes Allen Arthur

Binary Search Trees. Ric Glassey

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

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

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

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

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

Algorithms and Data Structures

Persistent Binary Search Trees

R-trees. R-Trees: A Dynamic Index Structure For Spatial Searching. R-Tree. Invariants

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)

Single machine models: Maximum Lateness -12- Approximation ratio for EDD for problem 1 r j,d j < 0 L max. structure of a schedule Q...

CS473 - Algorithms I

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

External Memory Geometric Data Structures

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

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Algorithms and Data Structures Written Exam Proposed SOLUTION

Lecture 1: Course overview, circuits, and formulas

Structure for String Keys

Symbol Tables. Introduction

Data Structure with C

The ADT Binary Search Tree

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

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

DATABASE DESIGN - 1DL400

Persistent Data Structures and Planar Point Location

10CS35: Data Structures Using C

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

Analysis of a Search Algorithm

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Laboratory Module 6 Red-Black Trees

Transcription:

CSE 32 Lecture : More on Search Trees Today s Topics: Lazy Operations Run Time Analysis of Binary Search Tree Operations Balanced Search Trees AVL Trees and Rotations Covered in Chapter of the text From Last Time: Remove (Delete) Operation Removing a node containing X:. Find the node containing X 2. Replace it with: If it has no children, with NULL If it has child, with that child If it has 2 children, with the node with the smallest value in its right subtree, (or largest value in left subtree) 3. Recursively remove node used in 2 and 3 Worst case: Recursion propagates all the way to a leaf node time is O(depth of tree) 5 2 2

Laziness in Data Structures A lazy operation is one that puts off work as much as possible in the hope that a future operation will make the current operation unnecessary Data Struct -ures 3 Lazy Deletion Idea: Mark node as deleted; no need to reorganize tree Skip marked nodes during Find or Insert Reorganize tree only when number of marked nodes exceeds a percentage of real nodes (e.g. 5%) Constant time penalty only due to marked nodes depth increases only by a constant amount if 5% are marked undeleted nodes (N nodes max N/2 marked) Modify Insert to make use of marked nodes whenever possible e.g. when deleted value is re-inserted Can also use lazy deletion for Lists

Run Time Analysis of BST operations All BST operations (except MakeEmpty) are O(d), where d is the depth of the accessed node in the tree MakeEmpty takes O(N) for a tree with N nodes frees all nodes We know: log N d N- for a binary tree with N nodes What is the best case tree? What is the worst case tree? Best Case Running Time of Insert/Remove/etc. =? Worst Case Running Time =? Average Case Running Time =? 5 The best, the worst, and the average For a binary tree with N nodes, depth d of any node satisfies: log N d N- So, best case running time of BST operations is O(log N) Worst case running time is O(N) Average case running time = O(average value of d) = O(log N) Can prove that average depth over all nodes = O(log N) if all insertion sequences equally likely. See Chap. in textbook for proof

Can we do better? Worst case running time of BST operations is O(N) E.g. What happens when you Insert elements in ascending (or descending) order? Insert 2,,, 8,, 2 into an empty BST Problem: Lack of balance Tree becomes highly asymmetric Idea: Can we restore balance by re-arranging tree according to depths of left and right subtrees? Goal: Get depth down from O(N) to O(log N) Idea #: Achieving the perfect balance First try at balancing trees: Perfect balance Re-arrange to get a complete tree after every operation Recall: A tree is complete if there are no holes when scanning from top to bottom, left to right Problem: Too expensive to re-arrange E.g. Insert 2 in the example shown Need a looser constraint 5 8 Insert 2 & make tree 5 complete 2 8 8

Idea #2: Leave it to the professionals Many efficient algorithms exist for balancing trees in order to achieve faster running times for the BST operations Adelson-Velskii and Landis (AVL) trees (2) Splay trees and other self-adjusting trees (8) B-trees and other multiway search trees (2) AVL Trees AVL trees are height-balanced binary search trees Balance factor of a node = height(left subtree) - height(right subtree) 3 2 Heights An AVL tree can only have balance factors of,, or at every node For every node, heights of left and right subtree differ by no more than Height of an empty subtree = - - 8 Balance factors -(-) = 2 Implementation: Store current heights in each node (-)- = - 8

Which of these are AVL trees? 5 8 5 8 3 5 8 AVL Trees: Examples and Non-Examples Balance factors - Not AVL -(-) = 2 (-)- = - 8 AVL - 5 AVL AVL 5 8 2 5-2 8-3 Not AVL 2

The good news about AVL Trees Can prove: Height of an AVL tree of N nodes is always O(log N) How? Can show: Height h. log(n+2)-.328 Prove using recurrence relation for minimum number of nodes S(h) in an AVL tree of height h: S(h) = S(h-) + S(h-2) + Use Fibonacci numbers to get bound on S(h) bound on height h Height = See textbook for details O(log N) 5 8-3 The really good news about AVL Trees Can prove: Height of an AVL tree of N nodes is always O(log N) All operations (e.g. Find, Remove using lazy deletion, etc.) on an AVL tree are O(log N) except Insert Insert 3 Why is Insert different? 5 -

The bad news about AVL Trees 5 - Insert 3 2 5-3 No longer an AVL tree (i.e. not balanced anymore) 5 Restoring Balance in (the life of) an AVL Tree Problem: Insert may cause balance factor to become 2 or 2 for some node on the path from insertion point to root node Idea: After Inserting the new node,. Back up to root updating heights along the access path 2. If Balance Factor = 2 or 2, adjust tree by rotation around deepest such node. 2 5-3

Rotating to restore Balance: A Simple Example Insert 3 5-2 5-3 Rotate - 3 5 AVL Not AVL AVL Next Class: Rotating and Splaying for Fun and Profit To Do: Finish Reading Chapter 8