CS 61B Data Structures and Programming Methodology Aug 11, 2008 David Sun.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Rizwan Rehman Centre for Computer Studies Dibrugarh University
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
Chapter 4: Trees Part II - AVL Tree
Balanced Binary Search Trees
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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):
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
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.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
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.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Binary search trees Definition Binary search trees and dynamic set operations Balanced binary search trees –Tree rotations –Red-black trees Move to front.
Splay Trees Splay trees are binary search trees (BSTs) that:
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Splay Trees and B-Trees
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Balanced Binary Search Trees
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSC 213 – Large Scale Programming. Implementing Map with a Tree  Accessing root much faster than going to leaves  In real-world, should place important.
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)
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
CSIT 402 Data Structures II
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
Search Trees Last Update: Nov 5, 2014 EECS2011: Search Trees1 “Grey Tree”, Piet Mondrian, 1912.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
CS 367 Introduction to Data Structures Lecture 9.
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.
Jim Anderson Comp 750, Fall 2009 Splay Trees - 1 Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
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 β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 23 Red Black Tree Chapter 10 of textbook
File Organization and Processing Week 3
Red Black Trees
Balanced Trees (AVL and RedBlack)
Red-Black Trees.
AVL Tree.
Lecture 25 Splay Tree Chapter 10 of textbook
Red-Black Trees Motivations
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
Data Structures and Algorithms
Red-Black Trees.
2-3-4 Trees Red-Black Trees
AVL-Trees.
Splay Trees Binary search trees.
Presentation transcript:

CS 61B Data Structures and Programming Methodology Aug 11, 2008 David Sun

Announcements Final is to be held this Thursday from 11:00 – 2:00 pm in 306 and 310 Soda. – Open book format. – If you have conflicts and have not contacted me please send me an asap. Project 3 due Aug 12 at 11a.m! – Solve at least 15 hard puzzles under the time limit to earn full points for the programming part. Regrades for Midterm 1 and 2 are should be visible through glookup. Midterm 2 question 1 (d) is nontrivial. – The question is treated as a bonus question, so the midterm is now out of 49. – If you answered along the lines of O(log 3 n) you will have received some points. If you were not awarded any points for a similar answer come and see me.

Splay Tree A splay tree is a type of balanced binary search tree. Structurally, it is identical to an ordinary binary search tree; the only difference is in the algorithms for finding, inserting, and deleting entries. All splay tree operations run in O(log n) time on average, where n is the number of entries in the tree. Any single operation can take Theta(n) time in the worst case. But any sequence of k splay tree operations, with the tree initially empty and never exceeding n items, takes O(k log n) worst-case time.

Rotation Rotation is a procedure that keeps splay tree (and other balanced search trees) balanced. There are two types -- a left rotation and a right rotation -- and each is the other’s reverse. The rotation procedure preserves the binary search tree invariant, i.e. after the rotation, the left subtree is less than or equal to the root and the right subtree is more than or equal to the root.

find 1.Walk down the tree until we find the entry with key k, or reach a dead end (just like binary search tree). 2.In both cases, splay the last visited node X (where the search ended) up the tree to become the new root via a sequence of rotations. 1.Recently accessed entries are near the root of the tree, so if you access the same few entries repeatedly, accesses will be very fast. 2.If X lies deeply down an unbalanced branch of the tree, the splay operation will improve the balance along that branch.

find Case 1 X is the right child of a left child – let P be the parent of X, and let G be the grandparent of X, first rotate X and P left and then rotate X and G right. – called the zig-zag. Case 1.1 X is a left child and P is a right child: – rotate X and P right, then X and G left. Case 2 X is the left child of a left child – start with the grandparent, and rotate G and P right. Then, rotate P and X right. – called the zig-zig. Case 2.1 X is the right child of a right child – rotate G and P left. rotate P and X left.

find Repeatedly apply zig-zag and zig-zig rotations to X. – each pair of rotations raises X two levels higher in the tree. – eventually, either X will reach the root (and we're done), or X will become the child of the root Case 3 X's parent P is the root: – rotate X and P so that X becomes the root. This is called "zig" case.

Observations First, in none of these three cases does the depth of a subtree decrease by more than two. Second, every time X takes two steps toward the root (zig-zig or zig-zag) every node in the subtree rooted at X moves at least one step closer to the root. It can be shown that a node that initially lies at depth d on the access path from the root to X moves to a final depth no greater than 3 + d/2. In other words, all the nodes deep down the search path have depths roughly halved.

Other Operations min/max: – Find the entry with the minimum or maximum key (using binary search). The node containing the minimum or maximum key is splayed to the root. Insert – Insert the new entry (k, v), just like in an ordinary binary search tree. Then, it splays the new node to the root. Remove – If the node containing k is removed if it has zero or one children. – If it has two children, the node with the next higher key is removed instead. In either case, let X be the node removed from the tree. After X is removed, splay X's parent to the root. – If the key k is not in the tree, splay the node where the search ended to the root, just like in a find() operation.

In 2000, Danny Sleator and Robert Tarjan won the ACM Kanellakis Theory and Practice Award for their papers on splay trees and amortized analysis. Splay trees are used in Windows NT (in the virtual memory, networking, and file system code), the gcc compiler and GNU C++ library, the sed string editor, Fore Systems network routers, the most popular implementation of Unix malloc, Linux loadable kernel modules, and in many other software.

Red-Black Trees Red-black tree is a binary search tree with the following additional properties: 1.Each node is either red or black 2.The root is black. 3.Every leaf (NIL) is black. 4.If a node is red, then both its children are black. 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes. Used for Java’s TreeSet and TreeMap types.

Red-black Tree Every red-black tree corresponds to a tree, and the operations on one correspond to those on the other. Each node of tree corresponds to a cluster of 1–3 red-black nodes in which the top node is black and any others are red.

find Exactly the same as a normal binary search tree.

insert Insertions begins by adding the node n to the tree as if it were an ordinary binary search tree. The newly inserted node is then colored red. Two properties of red-black trees can be violated: – Property 2: root needs to be black, which occurs if n is the root. – Property 4: a red node cannot have a red child, which occurs when n’s parent is red. First case is easy to deal with, just color the root black.

Preserving Property 4 Case 1: n’s parent and uncle are both red. – n’s grandparent must be black (since the tree was valid before the insertion) – Color n’s parent and uncle red, and the grandparent black. – Preserve property 1 and 4 for the grandparent. Case 2: n is a right child, n’s parent is red but n’s uncle is black. – Do a left rotation through the parent so the parent is the new child and child is the new parent. – Apply the solution for case 3. Case 3: n is a left child, n’s parent is red but n’s uncle is black. – Do a right rotation through the grandparent, so the new node and the former grandparent become the child of the parent. – Exchange the colors of the parent and the former grandparent.