Balanced binary search trees AVL trees, 2 3 trees, B trees Professor Clark F. Olson (with edits by Carol Zander)

Similar documents
From Last Time: Remove (Delete) Operation

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

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

TREE BASIC TERMINOLOGIES

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

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

Big Data and Scripting. Part 4: Memory Hierarchies

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

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

Analysis of Algorithms I: Binary Search Trees

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

Ordered Lists and Binary Trees

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

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

Binary Heap Algorithms

6 March Array Implementation of Binary Trees

Full and Complete Binary Trees

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

Converting a Number from Decimal to Binary

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:

Data Structures and Algorithms

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

A Comparison of Dictionary Implementations

Fundamental Algorithms

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

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

Rotation Operation for Binary Search Trees Idea:

Binary Search Trees (BST)

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

Binary Search Trees CMPSC 122

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

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

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

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

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

Symbol Tables. Introduction

Binary Trees and Huffman Encoding Binary Search Trees

Binary Heaps. CSE 373 Data Structures

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

Laboratory Module 6 Red-Black Trees

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

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

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

Review of Hashing: Integer Keys

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

Algorithms Chapter 12 Binary Search Trees

DATABASE DESIGN - 1DL400

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees

Data Structures Fibonacci Heaps, Amortized Analysis

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

Why Use Binary Trees?

Binary Search Trees 3/20/14

Physical Data Organization

Data Structures and Data Manipulation

The ADT Binary Search Tree

- 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

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

External Memory Geometric Data Structures

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

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

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

APP INVENTOR. Test Review

recursion, O(n), linked lists 6/14

Cpt S 223. School of EECS, WSU

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.

Lecture 1: Data Storage & Index

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

Data Structure with C

CIS 631 Database Management Systems Sample Final Exam

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

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

Structure for String Keys

Optimal Binary Search Trees Meet Object Oriented Programming

10CS35: Data Structures Using C

International Journal of Software and Web Sciences (IJSWS)

Chapter 14 The Binary Search Tree

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

The Taxman Game. Robert K. Moniot September 5, 2003

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

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

An Evaluation of Self-adjusting Binary Search Tree Techniques

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

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

An Immediate Approach to Balancing Nodes of Binary Search Trees

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

Simple Balanced Binary Search Trees

Dynamic 3-sided Planar Range Queries with Expected Doubly Logarithmic Time

Enhancing the Search in MOLAP Sparse Data

Lecture 4: Balanced Binary Search Trees

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

Algorithms and Data Structures

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

File Management. Chapter 12

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

PES Institute of Technology-BSC QUESTION BANK

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)

Unit Storage Structures 1. Storage Structures. Unit 4.3

Transcription:

alanced binary search trees VL trees, 3 trees, trees Professor lark F. Olson (with edits by arol Zander) VL trees One potential problem with an ordinary binary search tree is that it can have a height that is O(n), where n is the number of items stored in the tree. This occurs when the items are inserted in (nearly) sorted order. We can fix this problem if we can enforce that the tree remains balanced while still inserting and deleting items in O(log n) time. The first (and simplest) data structure to be discovered for which this could be achieved is the VL tree, which is names after the two Russians who discovered them, Georgy delson Velskii and Yevgeniy Landis. It takes longer (on average) to insert and delete in an VL tree, since the tree must remain balanced, but it is faster (on average) to retrieve. n VL tree must have the following properties: It is a binary search tree. For each node in the tree, the height of the left subtree and the height of the right subtree differ by at most one (the balance property). The height of each node is stored in the node to facilitate determining whether this is the case. The height of an VL tree is logarithmic in the number of nodes. This allows insert/delete/retrieve to all be performed in O(log n) time. Here is an example of an VL tree: 3 37 5 40 3 4 6 0 5 Inserting 0 or 5 or 6 or 43 would result in an unbalanced tree. The key to an VL tree is keeping it balanced when an insert or delete operation is performed. We will examine insertion in detail. If an insertion causes the balance property to be violated, then we must perform a tree rotation in order to fix the balance. If we start with an VL tree, then what is needed is either a single rotation or a double rotation (which is two single rotations) on the unbalanced node and that will always restore the balance property in O() time. Note that the rotations are always applied at the lowest (deepest) unbalanced node and no nodes above it need to have rotations applied. Page of 0

When a node becomes unbalanced, four cases need to be considered: Left left: The insertion was in the left subtree of the left child of the unbalanced node. Right right: The insertion was in the right subtree of the right child of the unbalanced node. Left right: The insertion was in the right subtree of the left child of the unbalanced node. Right left: The insertion was in the left subtree of the right child of the unbalanced node. Single rotations The first two require single rotations and the last two require double rotations to rebalance the tree. In general, a left left looks like this: K is the unbalanced node. Nothing above it matters. The left left subtree always has height one greater than the right subtree, which unbalances the tree. The solution is to make K the root of this (sub)tree with as its right child and as the right left subtree: K Page of 0

right right is symmetrical. K K Here is a right right example with actual data: 3 37 5 40 3 4 6 0 5 45 Nodes 37 and 40 are unbalanced. The lower node needs to be fixed. This is a right right, since 4 is the right child of 40 and 45 is in the right subtree of 4. In this case, two of the subtrees, and, are empty. The subtree has the single node 45. Rotating (with right child) yields: Page 3 of 0

3 37 5 4 3 40 45 6 0 5 Here is a left left, where the unbalanced node is not as deep: 3 37 5 40 3 3 6 0 5 0 Node 37 is unbalanced. The inserted node is in the left subtree of the left child of 37. Therefore, we rotate 37 with its left child: Page 4 of 0

3 5 3 37 3 0 40 6 0 5 Double rotations When we have the left right and right left cases, a single rotation is not sufficient to rebalance the tree. In this case, we need to perform a double rotation, which consists of two single rotations. Here is the generic situation for a left right: K3 K D To fix this, we need to rotate all the way to the top. First, we do a right right rotation of K with and then a left left rotation of K3 with. The first rotation (K/) yields: Page 5 of 0

K3 K D The second rotation (/K3) gives us: K K3 D onceptually, the splits the K and the K3 and rises to the top, with the K and the K3 ending up being subchildren and,,, and D being grandchild trees. Here is an example of a right left with real data: Page 6 of 0

3 5 3 37 3 0 40 6 0 5 5 First rotation: 3 5 3 37 6 0 40 5 0 3 5 Page 7 of 0

Second rotation: 5 3 3 37 6 0 3 0 40 5 5 It is worth noting that VL trees are not commonly used in practice, since better balanced binary trees exist. However, these are more complex. The rotations in VL trees demonstrate important concepts that are used in other balanced trees. trees trees are balanced (non binary) trees that are commonly used in practice. They allow much greater branching than in a binary tree (up to M children for a tree of order M) and have the following properties: Different implementations store data items at all nodes (our use) or at the leaves only. The root has between and M children. ll non leaf nodes (except the root) have between M/ and M children. ll leaves are at the same depth. If a non leaf node has children, then it stores keys. The i th key is the smallest key in the i + subtree. These keys guide the search process in the tree. 3 tree is a special case of a tree with M = 3. In a 3 tree, we might store data as follows: struct node3 { node3* left; Object* smallitem; node3* middle; Object* largeitem; node3* right; }; Page of 0

Here is an example of a 3 tree (only values are shown in leaf nodes, assume all pointers are NULL): 7 4 30 54 7 0 3 5 0 6 3 35 4 4 55 59 When we insert a value in a tree, we try to find a spot in the appropriate leaf. If none exists, then the leaf is split into two leaves and we propagate the process upwards. trees actually grow up rather than down, because if you insert enough time you have to split a leaf, which causes, the parent of the leaf to split, and, so on, all the way up to the root. For example, let s say we tried to insert 4. This would cause one of our leaves to have to be split since it cannot contain three values: 7 4 30 54 7 0 3 5 0 3 35 4 4 55 59 4 6 The middle value, the 4, moves up to its parent. However, there isn t room for another value in the parent node, which means we need to split it, too. The next diagrams only show values. 7 4 4 30 54 7 0 3 5 0 3 35 4 4 55 59 6 Page 9 of 0

gain, the middle value, the 4, moves up to its parent, the tree root. Now there isn t enough space at the root and a new root must be created. 7 4 4 30 54 7 0 3 5 0 6 3 35 4 4 55 59 So we split the root and create a new root that has the old (split) root as children. Notice that whenever a node has four children in the interim process of inserting, a resulting parent is created with two children and those children will each point to half of the original four children. 4 7 4 30 54 7 0 3 5 0 6 3 35 4 4 55 59 Like VL trees, 3 trees guarantee logarithmic time insert, delete, and retrieve. trees do also (since a 3 tree is a tree with M = 3). trees are commonly used in practice for applications such as databases. The reason for this is that the most expensive operation is accessing a disk. This is much more expensive than typically operations in memory. Therefore, we want to minimize the number of disk accesses and this is done by make the tree as short as possible. The more branches each node has, the shorter the tree will be. So, how large should we make M? The ideal case is to make a node in the tree large enough to fill an entire disk block (but no more), since an entire disk block is usually read at a time. Since the tree height is minimal, this minimizes the total number of disk accesses (swapping a node out) that are performed for operations on the tree, such as retrieving an item. One optimization that can be used if an item doesn t fit in the current leaf, a leaf can put an item up for adoption. In this case, we check to see if a sibling leaf can take that item without performing a split (note that parent values may have to be adjusted). Page 0 of 0