Lecture 4: Balanced Binary Search Trees



Similar documents
Analysis of Algorithms I: Binary Search Trees

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

Algorithms Chapter 12 Binary Search Trees

CS711008Z Algorithm Design and Analysis

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

An Evaluation of Self-adjusting Binary Search Tree Techniques

Binary Heaps. CSE 373 Data Structures

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

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

TREE BASIC TERMINOLOGIES

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

Cpt S 223. School of EECS, WSU

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

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

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

Data Structures and Algorithms

Converting a Number from Decimal to Binary

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

Binary Heap Algorithms

6 March Array Implementation of Binary Trees

A Comparison of Dictionary Implementations

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

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

From Last Time: Remove (Delete) Operation

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

Chapter 14 The Binary Search Tree

Binary Search Trees CMPSC 122

Rotation Operation for Binary Search Trees Idea:

A Randomized Self-Adjusting Binary Search Tree

Algorithms and Data Structures

Ordered Lists and Binary Trees

Lecture 2 February 12, 2003

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Search Trees 3/20/14

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

Symbol Tables. Introduction

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

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Full and Complete Binary Trees

Binary Search Trees (BST)

Binary Trees and Huffman Encoding Binary Search Trees

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,

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

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

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

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

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Binary Search Trees. Ric Glassey

Exam study sheet for CS2711. List of topics

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

Data Structure [Question Bank]

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

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)

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

Introduction to Data Structures and Algorithms

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

Union-Find Problem. Using Arrays And Chains

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

Binary Search Trees. Adnan Aziz. Heaps can perform extract-max, insert efficiently O(log n) worst case

Persistent Data Structures and Planar Point Location

Core Maths C2. Revision Notes

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:

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

MATHEMATICAL ENGINEERING TECHNICAL REPORTS. The Best-fit Heuristic for the Rectangular Strip Packing Problem: An Efficient Implementation

Fundamental Algorithms

11 Finger Search Trees

Classification/Decision Trees (II)

OPTIMAL BINARY SEARCH TREES

Algorithms and Data Structures

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.

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


Laboratory Module 6 Red-Black Trees

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

PES Institute of Technology-BSC QUESTION BANK

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

Physical Data Organization

The Goldberg Rao Algorithm for the Maximum Flow Problem

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

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

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

Lecture Notes on Binary Search Trees

International Journal of Software and Web Sciences (IJSWS)

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

Big Data and Scripting. Part 4: Memory Hierarchies

Load Balancing. Load Balancing 1 / 24

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

Why Use Binary Trees?

Introduction to Data Structures and Algorithms

Strategic Deployment in Graphs. 1 Introduction. v 1 = v s. v 2. v 4. e 1. e e 3. e 2. e 4

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

A Non-Linear Schema Theorem for Genetic Algorithms

Data Mining on Streams

- 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

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

Data Structure with C

The ADT Binary Search Tree

Transcription:

Lecture 4 alanced inar Search Trees 6.006 Fall 009 Lecture 4: alanced inar Search Trees Lecture Overview The importance of being balanced VL trees Definition alance Insert Other balanced trees Data structures in general Readings LRS hapter. and. (but different approach: red-blac trees) Recall: inar Search Trees (STs) rooted binar tree each node has e left pointer right pointer parent pointer See Fig. 4 0 9 50 6 Figure : Heights of nodes in a ST

Lecture 4 alanced inar Search Trees 6.006 Fall 009 Figure : ST propert ST propert (see Fig. ). height of node = length ( edges) of longest downward path to a leaf (see LRS.5 for details). The Importance of eing alanced: STs support insert, min, delete, ran, etc. in O(h) time, where h = height of tree (= height of root). h is between lg(n) and n: (see Fig. ). vs. Perfectl alanced Path Figure : alancing STs balanced ST maintains h = O(lg n) all operations run in O(lg n) time.

Lecture 4 alanced inar Search Trees 6.006 Fall 009 VL Trees: Definition VL trees are self-balancing binar search trees. These trees are named after their two inventors G.M. del son-vel sii and E.M. Landis. n VL tree is one that requires heights of left and right children of ever node to differ b at most ±. This is illustrated in Fig. 4) - Figure 4: VL Tree oncept In order to implement an VL tree, follow two critical steps: Treat nil tree as height. Each node stores its height. This is inherentl a DT STRUTURE UGMENTTION procedure, similar to augmenting subtree size. lternativel, one can just store difference in heights. good animation applet for VL trees is available at this lin. To compare inar Search Trees and VL balancing of trees use code provided here. Original Russian article: delson-velsii, G.; E. M. Landis (96). n algorithm for the organization of information. Proceedings of the USSR cadem of Sciences 46: 666. (English translation b Mron J. Ricci in Soviet Math. Dolad, :596, 96.)

Lecture 4 alanced inar Search Trees 6.006 Fall 009 alance: The balance is the worst when ever node differs b. Let N h = min ( nodes). lternativel: N h = N h + N h + > N h N h > h/ = h < lg N h N h > F n (n th Fibonacci number) In fact, N h = F n+ (simple induction) F h = h 5 (rounded to nearest integer) where, = + 5.68 = ma h log (n).440 lg(n) (golden ratio) VL Insert:. insert as in simple ST.. wor our wa up tree, restoring VL propert (and updating heights as ou go). Each Step: suppose is lowest node violating VL assume is right-heav (left case smmetric) if s right child is right-heav or balanced: follow steps in Fig. 5 else follow steps in Fig. 6 then continue up to s grandparent, greatgrandparent... 4

Lecture 4 alanced inar Search Trees 6.006 Fall 009 - + Left-Rotate() - - - - z + Left-Rotate() + - Figure 5: VL Insert alancing (FIX: Node z should be ) + Right-Rotate(z) - z + Left-Rotate() z D - - - or - D - - or - Figure 6: VL Insert alancing 5

Lecture 4 alanced inar Search Trees 6.006 Fall 009 Eample: n eample implementation of the VL Insert process is illustrated in Fig. 7. Insert() 4 = 9: left-left case 4 0 0 9 50 50 9 6 Done 4 6 Insert(55) 4 0 0 50 6 50 6 9 9 =: left-right case 4 Done 4 0 0 55 6 9 50 55 50 6 9 Figure 7: Illustration of VL Tree Insert Process. Note that node is left-heav. omment. In general, process ma need several rotations before an Insert is completed. omment. Delete(-min) harder but possible. 6

Lecture 4 alanced inar Search Trees 6.006 Fall 009 alanced Search Trees: There are man balanced search trees. VL Trees del son-velsii and Landis 96 -Trees/--4 Trees aer and Mcreight 97 (see LRS 8) [α] Trees Nievergelt and Reingold 97 Red-blac Trees LRS hapter Spla-Trees Sleator and Tarjan 985 Sip Lists Pugh 989 Scapegoat Trees Galperin and Rivest 99 Treaps Seidel and ragon 996 Note. Sip Lists and Treaps use random numbers to mae decisions fast with high probabilit. Note. Spla Trees and Scapegoat Trees are amortized : adding up costs for several operations = fast on average. 7

Lecture 4 alanced inar Search Trees 6.006 Fall 009 Spla Trees Upon access (search or insert), move node to root b sequence of rotations and/or doublerotations (just lie VL trees). Height can be linear but still O(lg n) per operation on average (amortized) Note: We will see more on amortization in a couple of lectures. Optimalit For STs, cannot do better than O(lg n) per search in worst case. In some cases, can do better e.g. in-order traversal taes Θ(n) time for n elements. put more frequent items near root onjecture: Spla trees are O(best ST) for ever access pattern. With fancier trics, can achieve O(lg lg u) performance for integers u [Van Ernde oas; see 6.854 or 6.85 (dvanced Data Structures)] ig Picture: bstract Data Tpe(DT): interface spec. e.g. Priorit Queue: Q = new-empt-queue() Q.insert() = Q.deletemin() vs. Data Structure (DS): algorithm for each op. There are man possible DSs for one DT. One eample that we will discuss much later in the course is the heap priorit queue. 8