Binary Search Trees Implementing Balancing Operations Hash Tables

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
Chapter 4: Trees Part II - AVL Tree
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
CS202 - Fundamental Structures of Computer Science II
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
SELF-BALANCING SEARCH TREES Chapter 9. Self-Balancing Search Trees  The performance of a binary search tree is proportional to the height of the tree.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
1 Binary Search Trees (Continued) Study Project 3 Solution Balanced Binary Search Trees Balancing Operations Reading: L&C 11.1 – 11.4.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
1 CSC TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
Sections 10.5 – 10.6 Hashing.
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CO4301 – Advanced Games Development Week 11 AVL Trees
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Data Structures Michael J. Watts
Week 7 - Friday CS221.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Binary Search Trees (Continued)
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Hashing Exercises.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
structures and their relationships." - Linus Torvalds
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 6 Transform and Conquer.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Hash Tables and Associative Containers
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CSE 373: Data Structures and Algorithms
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Tree Rotations and AVL Trees
CSE 332: Data Abstractions AVL Trees
Lecture No.20 Data Structures Dr. Sohail Aslam
Advanced Implementation of Tables
Advanced Implementation of Tables
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Lecture 9: Self Balancing Trees
slides created by Marty Stepp
Lecture 10: BST and AVL Trees
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Lecture 9: Intro to Trees
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
structures and their relationships." - Linus Torvalds
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

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

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

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 sub-tree with that node as root must be rebalanced

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, but zero can happen in some cases of remove operations

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

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

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

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)

AVL Tree Right Rotation Initial Tree After Remove 9 Node is -2 Left Child is zero! 7 (-1) 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

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

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

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

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

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: 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

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

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)

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

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