Rotation Operation for Binary Search Trees Idea:



Similar documents
Binary Heaps. CSE 373 Data Structures

Analysis of Algorithms I: Binary Search Trees

6 March Array Implementation of Binary Trees

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

Algorithms Chapter 12 Binary Search Trees

From Last Time: Remove (Delete) Operation

Cpt S 223. School of EECS, WSU

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 Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

TREE BASIC TERMINOLOGIES

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

Chapter 14 The Binary Search Tree

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

An Evaluation of Self-adjusting Binary Search Tree Techniques

Binary Search Trees CMPSC 122

Symbol Tables. Introduction

Ordered Lists and Binary Trees

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

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

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

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

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

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

Algorithms and Data Structures

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

Analysis of Algorithms I: Optimal Binary Search Trees

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

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

Binary Search Trees 3/20/14

Binary Heap Algorithms

Binary Trees and Huffman Encoding Binary Search Trees

Data Structures and Algorithms

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

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

Why Use Binary Trees?

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

Classification/Decision Trees (II)

Persistent Data Structures

A Comparison of Dictionary Implementations

Chapter 7: Termination Detection

Data Structure [Question Bank]

Data Structures Fibonacci Heaps, Amortized Analysis

Big Data and Scripting. Part 4: Memory Hierarchies

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

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

- 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

Lecture 4: Balanced Binary Search Trees

Introduction to Data Structures and Algorithms

Binary Search Trees. Ric Glassey

Introduction to Data Structures and Algorithms

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:

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

Persistent Data Structures and Planar Point Location

Converting a Number from Decimal to Binary

Data Structures and Data Manipulation

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

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

International Journal of Advanced Research in Computer Science and Software Engineering

Full and Complete Binary Trees

DATA STRUCTURES USING C

Data storage Tree indexes

Physical Data Organization

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

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

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

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

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Section IV.1: Recursive Algorithms and Recursion Trees

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


Persistent Binary Search Trees

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

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

PES Institute of Technology-BSC QUESTION BANK

DATABASE DESIGN - 1DL400

Data Structure with C

OPTIMAL BINARY SEARCH TREES

Hash-based Digital Signature Schemes

GRAPH THEORY LECTURE 4: TREES

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)

CS711008Z Algorithm Design and Analysis

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

Lecture 1: Data Storage & Index

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

6 Creating the Animation

Binary Search Trees (BST)

Lecture 2 February 12, 2003

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

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

Zabin Visram Room CS115 CS126 Searching. Binary Search

Data Structures. Level 6 C Module Descriptor

Social Media Mining. Graph Essentials

Chapter 13. Disk Storage, Basic File Structures, and Hashing

Network (Tree) Topology Inference Based on Prüfer Sequence

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

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,

Exam study sheet for CS2711. List of topics

Data Mining on Streams

Transcription:

Rotation Operation for Binary Search Trees Idea: Change a few pointers at a particular place in the tree so that one subtree becomes less deep in exchange for another one becoming deeper. A sequence of rotations along a root to leaf path can help to make a binary search tree more "balanced". RR = "rotate right" and RL = "rotate left". - 1 -

(rotation operation continued) Notation: q.left, q.right, and q.parent denote the pointer fields associated with a vertex q. RR(v) is just a few assignment statements: u := v.parent w := v.left x := w.right v.left := x x.parent := v w.right := v v.parent := w if u nil then begin if u.left = v then u.left := w else u.right := w end w.parent := u - 2 -

RL(w) is symmetric to RR(v); exchange "v" with "w" and "left" with "right": u := w.parent v := w.right x := v.left w.right := x x.parent := w v.left := w w.parent := v if u nil then begin if u.right := w then u.right := v else u.left := v end v.parent := u - 3 -

Example: A Sequence of Rotations - 4 -

Self-Adjusting Binary Search Trees Idea: Each time an operation such as INSERT walks down to a vertex x, do a sequence of rotations to "push" x up to the root. SPLAY-STEP(x) uses two rotations to reduce the depth of x by two (except it uses 1 rotation when x has depth 1). If x has depth d, SPLAY(x) performs a sequence of d/2 SPLAY-STEP's to make x become the root: SPLAY(x): while x is not the root do SPLAY-STEP(x) - 5 -

(SABT's continued) The SPLAY-STEP procedure: Apply one of steps 1L, 2L, or 3L (or three symmetric cases 1R, 2R, and 3R where the roles of right and left are reversed): Case 1L: x is a left child PARENT(x) is the root: A x B y C RR(y) A x B y C - 6 -

(SABT's continued) Case 2L: x is left child and PARENT(x) is a left child. z x y y D A x z C B RR(z); RR(y) A B C D Case 3L: x is a left child and PARENT(x) is a right child. z A x y RR(y); RL(z) z x D y B C A B C D - 7 -

(SABT's continued) Advantages of self-adjusting BST's: Splaying can be applied to any binary search tree because SPLAY-STEP needs no additional information over the normal PARENT, LCHILD, and RCHILD fields. When a SPLAY is performed after every basic binary search tree operation, the tree tends to stay balanced. It can be proved that although an individual operation can be time consuming, a sequence of O(n) operations is always O(nlog(n)). (Self-adjusting BST's have good amortized performance.) - 8 -

(SABST's continued) Example, SPLAY(4): - 9 -

Example: Tree Sort Tree sort algorithm: Given a sequence of n input items to be sorted: Initialize an empty binary search tree T. for each input item x do INSERT(x,T) while T is not empty do output DELETEMIN(T) Standard binary search tree: Expected time O(nlog(n)): If input items are randomly ordered, it can be proved that the expected time O(nlog(n)). Worst case time Ω(n 2 ): If input arrives in order of largest to smallest, the for loop constructs a chain of n 1 left children where the largest is the root and the smallest is the only leaf (essentially a linked list), and both the for and while loops are O(n 2 ). Self-adjusting binary search tree is worst case O(nlog(n)) time: Starting with an empty self-adjusting binary search tree, a sequence of O(n) INSERT and DELETEMIN operations is always O(nlog(n)). - 10 -

Example: Using a self-adjusting binary search tree, consider again the case when the input arrives in reverse sorted order. The first item inserted becomes the root. After that each successive item is made the left child of the root and then a SPLAY makes it the root; so in only O(n) time the for loop constructs a root with a chain of n 1 right children (again, essentially a linked list). This is now a very easy O(n) time case for the while loop because each delete simply removes the root and the SPLAY does nothing Even if the problem was to sort from largest to smallest, a self-adjusting tree does well. It starts by using O(n) time to delete a leaf of depth n 1, but as time goes on, it gets more and more balanced and DELETEMIN's get less and less expensive, where the total time sums to only O(nlog(n)). For example, the first three deletions for n=16 go as follows: - 11 -

Joining and Splitting Self-Adjusting Binary Search Trees JOIN(T 1,d,T 2 ): A single tree with root d is formed from T 1, d, and T 2. ** Always assumes that all items in T 1 are less than d and all items in T 2 are greater than d. SPLIT(d,T): Split T into items <d and items >d by searching for d (which SPLAY's d to make it the root) and then detach the left and right subtrees of d (discard d). ** For ease of presentation, we assume that d is in T. (*) Complexity: It can be proved that a sequence of INSERT, DELETE, MEMBER, JOIN, and SPLIT operations is always O(nlog(n)) time. - 12 -