Balanced Trees Abs(depth(leftChild) – depth(rightChild)) <= 1 Depth of a tree is it’s longest path length Red-black trees – Restructure the tree when rules.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Chapter 4: Trees Part II - AVL Tree
Trees Types and Operations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
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 Binary Search Trees
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
A balanced life is a prefect life.
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):
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.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
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.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
Binary Search Trees CSE 331 Section 2 James Daly.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
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)
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
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
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
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.
AVL Tree Definition: Theorem (Adel'son-Vel'skii and Landis 1962):
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
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.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
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
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
AA Trees.
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Balanced Trees (AVL and RedBlack)
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
AVL Search Tree put(9)
short illustrative repetition
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Presentation transcript:

Balanced Trees Abs(depth(leftChild) – depth(rightChild)) <= 1 Depth of a tree is it’s longest path length Red-black trees – Restructure the tree when rules among nodes of the tree are violated as we follow the path from root to the insertion point. AVL Trees – Maintain a three way flag at each node (- 1,0,1) determining whether the left sub-tree is longer, shorter or the same length. Restructure the tree when the flag would go to –2 or +2. Splay Trees – Don’t require complete balance. However, N inserts and deletes can be done in NlgN time. Rotates are done to move accessed nodes to the top of the tree.

Rotates Analyze possible tree depths after rotates LL, RR Rotates –Child node is raised one level RL, LR Rotates –Child node is raised two levels in two steps Splay Tree Rotates –Outer nodes of grandparent nodes are raised two levels.

Outer R and L rotates LR X A Y Y X CB A B C X A Y Y X CB A B C Note: The squares are subtrees, the circles are nodes

Inner LR and RL rotates X A Y Z X C B A B D RL ZD Y C Note: LR is the mirror image

Outer Splay Rotates X A Y Z Y C B A B D Z D X C Note: There is also a rotate for the mirror image

AVL and Splay Trees Adelson-Velskii and Landis AVL Insertion Algorithm Insert using the same algorithm as the binary search Traverse back up the tree and for each node on the path to the root –Update the balance weight –IF weight becomes zero, break out of traversal –IF weight becomes +2 or -2, perform an appropriate rotation; break AVL Removal Algorithm Add the following logic to a standard binary search tree deletion After the deleting node having less than two children, traverse up the tree WHILE traversing IF the path to the left was reduced in length, add 1 to the weight IF the path to the right was reduced in length, subtract 1 from the weight IF the tree did not shrink (new weight = ±1) THEN BREAK IF the weight becomes +2 or -2 Find a path in the sibling direction that we can rotate Perform an appropriate rotation and adjust weights

Splay Tree Algorithm Splaying a node (bringing it up to the top) Find the node and double rotate it to the top. A single rotation might be needed at the last step Splay Tree Insertion Algorithm Perform a normal binary search tree insertion and then splay Deletion Algorithm Find and Splay node, n, to delete Remove n leaving two trees (one with nodes with smaller keys) Splay the smallest node in the tree with larger nodes Link the root of the tree with smaller nodes to the root of the tree with the larger nodes

Red Black Trees Red-black trees are tress that conform to the following rules: –Every node is colored (either red or black) –The root is always black –If a node is red, its children must be black –Every path from the root to leaf, or to a null child, must contain the same number of black nodes. –During insertions and deletions, these rules must be maintained

Red-black Insertion Algorithm current node = root node parent = grandParent = null While current <> null If current is black, and current’s children are red Change current to red (If current<>root) and current’s children to black Call rotateTree() grandParent = parent parent = current current is set to the child node in the binary search sequence If parent is null root = node to insert; color it black Else Connect the node to insert to the leaf node; color it red Call rotateTree()

Rotate Tree() Algorithm If current <> root and Parent is red If current is an outer child of the grandParent node Set color of grandParent node to red Set color of parent node to black Raise current by rotating parent with grandParent If current is an inner child of the grandParent node Set color of grandParent node to red Set color of current to black Raise current by rotating current with parent Raise current by rotating current with grandParent node

Red Black Example Before adding 99 After adding 99 Note: Color change at 92 led to an outer rotation involving 52, 67, 92

Red Black Deletion A standard Binary Search Tree removal reduces to removing a node with less than two children If a node with a single child is black, change the child’s color to black. Then simply connect the child to the parent Leafs can be red or black. If red, simply remove it. If black, removal causes an imbalance of black nodes, which must be restored. Weapons at our disposal –Color flips –Traversal upward –Rotations

Red Black Deletion (cont.) Explanation 1.X, Y, Z represent sub-trees 2.Path from P to the left has K black nodes; the path to the right has K+1 black nodes 3.P is the parent node, S is the sibling node (A sibling must exist, because of step 2) 4.To restore balance Case A: IF Head of X is red; turn it black Case B: IF S black with red children; rotate Case C: IF S black with no red children i.IF P red, set P black and S red ii.ELSE color S red restoring balance. Traversal up because both paths now have only K black nodes Case D: IF S red, perform one rotate, two if one or three of S's grandchildren are red. The general case S P X Y Z K black nodes

Example Case B (Balance Restored) P S G X ZQ Y P S G X ZQ Y Note: P=parent, S=sibling, G = grandchild, Green node can be either black or red

Case D Examples Case D (An S grandchild is red) Note: These examples require another rotation because a double red occurs X P S G B Q A G YZ S G G X B Q P Y Z A X P S G ZQY G AB G S G X Z Q Y P AB

Another Case D Example Case D (All of S's Grandchildren are Red) Balance has been restored A B CD G S G X P G EF H X P S G G G EF H C AB D

Which algorithm is best? Advantages –AVL: relatively easy to program. Insert requires only one rotation. –Splay: No extra storage, high frequency nodes near the top –RedBlack: Fastest in practice, no traversal back up the tree on insert Disadvantages –AVL: Repeated rotations are needed on deletion, must traverse back up the tree. –SPLAY: Can occasionally have O(N) finds, multiple rotates on every search –RedBlack: Multiple rotates on insertion, delete algorithm difficult to understand and program