Binary Search Trees (Continued)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
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.
AVL Trees Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
CS202 - Fundamental Structures of Computer Science II
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
1 Binary Search Trees (Continued) Study Project 3 Solution Balanced Binary Search Trees Balancing Operations Reading: L&C 11.1 – 11.4.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
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)
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved Balancing Binary Trees There are many approaches to balancing binary trees One method.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Binary Search Trees Implementing Balancing Operations Hash Tables
AVL Trees CSE, POSTECH.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
G64ADS Advanced Data Structures
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.
Week 7 - Friday CS221.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Problems with Linked List (as we’ve seen so far…)
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Balanced Trees (AVL and RedBlack)
COMP 103 Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Monday, April 16, 2018 Announcements… For Today…
TCSS 342, Winter 2006 Lecture Notes
Chapter 6 Transform and Conquer.
Trees Chapter 15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Data Structures and Algorithms
AVL Trees: AVL Trees: Balanced binary search tree
Tree Rotations and AVL Trees
2-3-4 Trees Red-Black Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
Lecture 9: Intro to Trees
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Binary Search Trees (Continued) Study Project 3 Solution Balanced Binary Search Trees Balancing Operations Implementing Balancing Operations AVL Trees Red/Black Trees Reading: L&C 10.1 – 10.9

Study Project 3 Solution Project 3 was due before class today Discuss solution

Balanced Binary Search Trees The balance of a binary search tree is important for obtaining its efficiency If we add 3, 5, 9, 12, 18, and 20 to a binary search tree, we get a degenerate tree This is less efficient than a singly linked list because our code needs to check the null left pointer at each level while traversing it Operations are O(n) instead of O(log n) We want our binary search trees balanced

Balanced Binary Search Trees Degenerate tree for a binary search tree 3 5 9 12 18 20

Balancing Operations Brute force balancing methods work but are unnecessarily time consuming We could use an in-order traversal of the tree and move everything out to an array Then, we could use a recursive method to insert the middle element of the array as the root and subdivide the array into two halves Eventually, we will rebuild a balanced tree

Balancing Operations We prefer to use balancing operations after each add or remove element operation Semantics of balancing operations Right rotation Left rotation Rightleft rotation Leftright rotation

Balancing Operations Semantics of Right Rotation A. Make the left child of the root the new root B. Make former root the right child of the new root C. Make right child of the former left child of the former root the new left child of the former root 13 7 7 7 7 15 5 13 5 13 5 13 5 10 3 15 3 15 3 10 15 10 10 Initial Tree Step C 3 Step A Step B

Balancing Operations Semantics of Left Rotation A. Make the right child of the root the new root B. Make former root the left child of the new root C. Make left child of the former right child of the former root the new right child of the former root 5 10 10 10 3 10 5 13 5 13 5 13 7 13 3 15 3 15 3 7 15 7 7 Initial Tree Step A Step B Step C 15

Balancing Operations Semantics of Rightleft Rotation A. Right rotation around right child of root B. Left rotation around root 5 5 10 3 13 3 10 5 13 10 15 7 13 3 7 15 7 15 Initial Tree After Right Rotation After Left Rotation

Balancing Operations Semantics of Leftright Rotation A. Left rotation around left child of root B. Right rotation around root 13 13 7 5 15 7 15 5 13 3 7 5 10 3 10 15 10 3 Initial Tree After Left Rotation After Right Rotation

Implementing Balancing Operations Now that we know how to rebalance the tree when needed, we must know how to detect that the tree needs to be rebalanced 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 If a node’s balance factor is > +1 or < -1, the sub-tree with that node as root needs to be rebalanced

Implementing Balancing Operations There are 2 ways for tree to become unbalanced By insertion of a node By deletion of a node Each time one of those 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 It is best for AVL trees to have a parent reference in each child node to backtrack up the tree easily We can now detect the need for making any one of the balancing rotations we have already studied

Implementing Balancing Operations If the balance factor of a node is -2 If the balance factor of its left child is -1 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 Perform a left rotation If the balance factor of its right child is -1 Perform a rightleft rotation

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)

Implementing Balancing Operations There is another way to detect the need to rebalance a binary search tree Red/Black Trees (developed by Bayer and extended by Guibas and Sedgewick) keep a “color” red or black for each node in the tree The root is black All children of a red node are black Every path from the root to a leaf contains the same number of black nodes

Implementing Balancing Operations As with AVL trees: The only time we need to rebalance and recolor is after the insertion or deletion of a node It is best for Red/Black trees to have a parent reference in each child node to backtrack up the tree easily The maximum height of a Red/Black tree is roughly 2*log n (not as well controlled as an AVL tree), but traversal of the longest path is still O(log n) The rest of this topic is left to the student to study

Introduction to Project 4 Study use of a stack data structure in the solution of the maze in the text In project 4, you will write different code to solve a maze via “wall-following” algorithm Your code will use a stack data structure to backtrack from blind alleys or dead ends The previous location visited is pushed onto the stack and is the first one to be retrieved if the code needs to backtrack

Introduction to Project 4 Maze with Wall-following (Left) Solution Alternatively, we could follow the right wall Enter Exit

UML Diagram for Project 4 Main Maze - path : Stack<Site> + main(String [ ] args) : void - iterate(start : Site, end : Site) : void - recurse(current:Site, end:Site) : bool - start : Site - end : Site + Maze( ) + getStart( ) : Site + getEnd ( ) : Site java.util.Stack Site - name : String - walls : Site [4] - direction : int + Site(name : String, left : Site, ahead : Site, right : Site) + toString( ) : String + next (direction : int) : Site + getDirection( ) : int

Introduction to Project 4 Because there is no reference back from a Site to the previous Site, your code must save the reference to the previous Site on an internal stack before moving forward You will write two different methods that can each solve the maze In “iterate”, your code will explicitly keep track of the path backwards on a stack In “recurse”, your code will implicitly keep track of the path back on the system stack