Binary Search Trees. Implementing Balancing Operations. Hash Tables Reading: , Appendix E. AVL Trees Red/Black Trees

Similar documents
A Comparison of Dictionary Implementations

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

Symbol Tables. Introduction

Persistent Binary Search Trees

Fundamental Algorithms

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)

TREE BASIC TERMINOLOGIES

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

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

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

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

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

From Last Time: Remove (Delete) Operation

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

- 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

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

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

Full and Complete Binary Trees

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

Binary Heap Algorithms

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

Data Structures in the Java API

Chapter 8: Bags and Sets

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Java Software 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.

Big Data and Scripting. Part 4: Memory Hierarchies

Analysis of Algorithms I: Binary Search Trees

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

Binary Heaps. CSE 373 Data Structures

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Binary Search Trees 3/20/14

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:

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

DATA STRUCTURES USING C

An Immediate Approach to Balancing Nodes of Binary Search Trees

Exam study sheet for CS2711. List of topics

Lecture Notes on Binary Search Trees

Binary Search Trees CMPSC 122

Chapter 14 The Binary Search Tree

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

An Evaluation of Self-adjusting Binary Search Tree Techniques

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

PES Institute of Technology-BSC QUESTION BANK

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Why Use Binary Trees?

6 March Array Implementation of Binary Trees

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

Structural Design Patterns Used in Data Structures Implementation

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

Binary Search Trees. Ric Glassey

Simple Balanced Binary Search Trees

Efficient representation of integer sets

Converting a Number from Decimal to Binary

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

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

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

International Journal of Software and Web Sciences (IJSWS)

Ordered Lists and Binary Trees

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

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Algorithms and Data Structures

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

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

Useful Java Collections

Introduction to Programming System Design. CSCI 455x (4 Units)

Review of Hashing: Integer Keys

Physical Data Organization

Algorithms and Data Structures

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

Sample Questions Csci 1112 A. Bellaachia

Structure for String Keys

Binary Search Trees (BST)

CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions. Linda Shapiro Spring 2016

Enhancing the Search in MOLAP Sparse Data

Data Structures and Algorithms

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

Fast Sequential Summation Algorithms Using Augmented Data Structures

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

1 Introduction to Internet Content Distribution

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

M-way Trees and B-Trees

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

Randomized Binary Search Trees

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

Introduction to Data Structures

Data Structures and Data Manipulation

Data Structures in Java. Session 15 Instructor: Bert Huang

Record Storage and Primary File Organization

Lecture 1: Data Storage & Index

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 storage Tree indexes

Class 32: The Java Collections Framework

Lecture 2: Data Structures Steven Skiena. skiena

Efficient Data Structures for Decision Diagrams

Data Structures. Level 6 C Module Descriptor

A Study on Skip Lists, Jump Lists and Random Binary. Search Trees. (Kuan-Chien Wang) (Prof. Wei-Mei Chen)

Transcription:

Binary Search Trees Implementing Balancing Operations AVL Trees Red/Black Trees Hash Tables Reading: 11.5-11.6, Appendix E 1

Implementing Balancing Operations Knowing rotations, we must know how to detect that the tree needs to be rebalanced and which way There are 2 ways for tree to become unbalanced By insertion of a node By deletion of a node There are two mechanisms for detecting if a rotation is needed and which rotation to perform: AVL Trees Red/Black Trees It is best for both to have a parent reference in each child node to backtrack up the tree easily 2

Implementing Balancing Operations AVL trees (after Adel son-vel ski and Landis) keep a balance factor attribute in each node that equals the height of the right sub-tree minus the height of the left sub-tree Each time an insertion or deletion occurs: The balance factors must be updated The balance of the tree must be checked from the point of change up to and including the root If a node s balance factor is > +1 or < -1, the subtree with that node as root must be rebalanced 3

Implementing Balancing Operations If the balance factor of a node is -2 If the balance factor of its left child is -1 or zero* Perform a right rotation If the balance factor of its left child is +1 Perform a leftright rotation If the balance factor of a node is +2 If the balance factor of its right child is +1 or zero* Perform a left rotation If the balance factor of its right child is -1 Perform a rightleft rotation Note: Error in L&C on page 315 zero is not mentioned, 4 but zero can happen in some cases of remove operations

AVL Tree Right Rotation Initial Tree After Add 1 7 (-1) Node is -2 7 (-2) Left Child is -1 5 (0) 9 (0) 5 (-1) 9 (0) 3 (0) 6 (0) 3 (-1) 6 (0) 1 (0) 5

AVL Tree Right Rotation After Right Rotation 5 (0) 3 (-1) 7 (0) 1 (0) 6 (0) 9 (0) 6

AVL Tree Rightleft Rotation Initial Tree After Remove 3 10 (1) 10 (2) Node is +2 Right Child is -1 Remove 5 (-1) 15 (-1) 5 (0) 15 (-1) 3 (0) 13 (-1) 17 (0) 13 (-1) 17 (0) 11 (0) 11 (0) 7

AVL Tree Rightleft Rotation After Right Rotation After Left Rotation 10 (2) 13 (0) 5 (0) 13 (1) 10 (0) 15 (1) 11 (0) 15 (1) 5 (0) 11 (0) 17 (0) 17 (0) 8

AVL Tree Right Rotation Initial Tree After Remove 9 7 (-1) Node is -2 Left Child is zero! 7 (-2) 5 (0) 9 (0) 5 (0) 3 (0) 6 (0) 3 (0) 6 (0) Note: This is the case that the L&C text misses in its discussion and examples 9

AVL Tree Right Rotation After Right Rotation 5 (+1) 3 (0) 7 (-1) 6 (0) Note: This is the case that the L&C text misses in its discussion and examples 10

Red/Black Trees Red/Black Trees (developed by Bayer and extended by Guibas and Sedgewick) keep a color red or black for each node in the tree This approach is used in the Java class library binary search tree classes The maximum height of a Red/Black tree is roughly 2*log n (not as well controlled as an AVL tree), but the height is still O(log n) The rules for insert, delete, and rebalance are more complicated than our textbook section covers, so we won t study this technique 11

Hash Tables Binary search trees can be used for both sorting in O(N log N) time and searching in O(log N) time However, if you have an application that requires only searching but not sorting, a hash table is a data structure that provides O(1) performance for searching A hash table optimizes time performance at the expense of using more memory 12

Hash Tables Some programming languages include direct language support for hash tables - such as PERL (hashes) and Python (dictionaries) Java does not - hash tables are implemented in library classes such as HashMap<K,V> or you can write a class yourself if necessary You should understand how to implement a hash table in any language such as C where there is no language or library support 13

Hash Tables The base of a hash table can be an array A hash function operates on the search key to produce an integer index into the array All data elements whose hash of their keys result in the same index are stored starting at that position in the array Essentially, a hash table is structured like a comb with a long spine and short teeth Only a few elements start at the same index 14

Hash Tables In Java, one reasonable way to implement a hash table is with a parameterized class that encapsulates an array of ArrayList<K> objects ArrayList<K> hashtable = new ArrayList<K>(); Hash Value hashtable ArrayList(s) for key K1 as an index to one ArrayList... K1... 15

Hash Tables To add an element, the add method: Hashes the key to get an index into the table Instantiates an ArrayList (if needed) Adds the key object to that ArrayList To find an element, the find method: Hashes the key to get an index into the table If there is no ArrayList there, it returns not found It searches the ArrayList to find the Key If the Key is not there, it returns not found 16

Hash Tables The hash table lookup is faster because a search quickly finds the correct ArrayList which is short for searching relative to N If as you add elements to the Hash Table the ArrayLists get too large, you need to expand the capacity of the array as we did previously for stacks and queues and then rehash every key into the new larger array with new ArrayLists 17

Hash Functions The hashing function is critical for random distribution of the key elements For example, if your keys are telephone numbers in Massachusetts, using the first 3 digits (the area codes) would be a poor choice because there are only a few area codes in use in the state ( clustering ) The last 4 digits would be a better choice, but is still not ideal because the array size must be 10,000 (not a prime number) 18

Hash Functions The mathematics don t work well unless the array size is a prime number (P) A better choice would be to select a prime number for the size of the array and use the whole phone number modulo the array size to get an array index from 0 to P-1 If you need to expand the array capacity, you can t just double the size of the array because that would not be a prime number 19

Hash Functions Implementing a good hash function is not easy There is a hashcode() method in the Object class that is the parent class of all classes Although it is not considered to generate the best hash codes, you should probably use it if you don t know how to generate a good one yourself 20