Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,

Slides:



Advertisements
Similar presentations
1 COP 3540 Data Structures with OOP Chapter 9 – Red Black Trees.
Advertisements

Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
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.
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
Trees Types and Operations
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
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.
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
Tirgul 5 AVL trees.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Tirgul 5 Comparators AVL trees. Comparators You already know interface Comparable which is used to compare objects. By implementing the interface, one.
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.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
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:
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
1 CS 310 – Data Structures All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley. All rights reserved. photo ©Oregon Scenics used.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
CS 367 Introduction to Data Structures Lecture 9.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
Binary Search Trees (BST)
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.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
Red-Black Trees an 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.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
AA Trees.
File Organization and Processing Week 3
Red-Black Tree Neil Tang 02/07/2008
Red-Black Tree Neil Tang 02/04/2010
Binary Search Tree (BST)
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Advanced Associative Structures
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.
CS202 - Fundamental Structures of Computer Science II
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.
CSE 332: Data Abstractions AVL Trees
Richard Anderson Spring 2016
CS202 - Fundamental Structures of Computer Science II
Data Structures Using C++ 2E
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Keeping Binary Trees Sorted

Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node, Object goal) { // try this node if (node == null) return null; // not found yet if (goal.equals(node.value)) return node; // found // look in the left subtree BinaryTree result = findNode(node.leftChild, goal); if (result != null) return result; // look in the right subtree else return findNode(node.rightChild, goal); }

Balance If a search tree is balanced, search time is O(log n) –Using the number of nodes in the tree as n If a search tree is unbalanced, search time could be as much as O(n) If a search tree is constructed once and searched many times, then constructing the balanced tree need not be particularly fast If insertions and deletions to the tree are frequent, then we need a fast way of rebalancing the tree Rebalancing can be done via a series of rotations

Rotations Notice that rotations keep the binary tree sorted! Rotate right about this node Rotate right about this node Rotate left about this node

Complications When we did a rotate right, the left child ( 50 ) of the topmost node ( 60 ) did not have a left child to worry about Thus the previous topmost node ( 60 ) could become the right child of the previous left child ( 50 ) If 50 had had a right child, we would have had to do something about it

Rotate right with crossover When a node with an “inside” (towards the center) child moves up, the inside child must cross over Notice that this is always possible, because the previous topmost node ( 60 ) “loses” its left child

Rotate left with crossover The same thing happens if the rotation is to the left rather than to the right Notice how, in each case, the “inside” child of the node moves to be a child of the previous top node ( 50 ) and changes sides (in this case, from left child to right child)

Red-black and AVL trees Rotations can be used to balance any binary tree The hard part is figuring out which rotations to perform, in what order Algorithms for doing this are complex –The two most common algorithms use binary trees with extra information: AVL trees and red-black trees With either kind of tree, the idea is to keep the tree in balance at all times –Rebalance after each insertion –Rebalance after each deletion We will not go into complete detail on either of these algorithms

AVL trees AVL trees are the simpler of the two kinds Each node contains (in addition to its usual fields) a field containing the difference between the depths of its two subtrees –This difference cannot be greater than 1 Nodes are added in the usual way –When a node is added, the depth may change, and a rotation (or two rotations) may be needed –When a rotation is performed, the next higher node must also be checked –This process continues, potentially all the way up to the root of the binary tree

Red-black trees A second way of keeping a binary tree balanced is to keep a “color” indicator in each node The “color” of a node is just a bookkeeping device There are rules that put restrictions on the color that each node can be When a tree is red-black correct (follows the rules), then it is also balanced

The four rules If these four conditions are met, the binary tree is balanced: 1.Every node is either “red” or “black” 2.The root is always black 3.The children of a red node must be black 4.Every path from the root to a leaf, or to a “null child,” must contain the same number of black nodes. OKNot OKOK

Additional operations In addition to rotations, there are additional operations that just operate on the colors –These are part of the “bookkeeping” for red-black trees The first operation is trivial: Change the color of a node The second operation is a flip: Change the color of a node and its two children There are two cases: or and Special case: If the root is the topmost node, it stays black

About red-black trees The notion of “red” and “black” nodes is just a bookkeeping device to tell us what rotations to perform We start with a balanced red-black tree that follows all the rules –One (root) node, black, satisfies the rules As we insert each node, we do rotations and color changes to preserve the red-black rules –In the process, the tree stays balanced –The operations are fairly complex –You do not need to memorize all the transformations, just try to get the general idea

Finding the insertion point We insert a value into a (sorted) red-black tree in the usual way: –If the value we are inserting is smaller than the node, go left –If the value is larger than or equal to the node, go right Along the way, if we find a black node with two red children, we flip (change the color of all three nodes) It is OK for a black node to have red children –We started with a red-black balanced tree This change does not affect the number of black nodes along any path to a leaf The change may give us two red nodes in a row (not OK) OKMaybe OK

Inside and outside grandchildren A grandchild (child of a child) is an inside grandchild if it is: –The left child of a right child, or –The right child of a left child A grandchild is an outside grandchild if it is: –The left child of a left child, or –The right child of a right child We use this distinction in the red-black algorithm inside grandchildren outside grandchildren

Cases Starting with a red-black correct tree, inserting a node, and possibly doing B-R-R  R-B-B flips along the way, there are only two kinds of problems that might arise: –Grandparent and parent are both red, and the child is an outside child –Grandparent and parent are both red, and the child is an inside child In addition, these problems may occur on the way down the tree, or after the insertion Every case requires a different sequence of operations We will not go into these details

In real life... Tree balancing algorithms are complex, and I do not expect you to learn them You will probably never need to implement them anyway But if you do... –Get one of the big fat algorithms books (Sedgewick is the classic) –Look up the algorithm you need –Implement it very, very carefully and test it thoroughly

The End