83 Pages

05-BalancedTrees

Course: TCSS 342, Fall 2009
School: Washington
Rating:
 
 
 
 
 

Word Count: 1947

Document Preview

342, TCSS Winter 2006 Lecture Notes Balanced Binary Search Trees 1 TCSS 342B v1.0 Learn about balanced binary search trees AVL trees red-black trees Objectives Talk about why they work Talk about their run-time performance. 2 TCSS 342B v1.0 AVL Trees In 1960 two Russian mathematicians Georgii Maksimovich Adel'son-Vel'skii Evgenii Mikhailovich Landis developed a technique for keeping a binary search...

Register Now

Unformatted Document Excerpt

Coursehero >> Washington >> Washington >> TCSS 342

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
342, TCSS Winter 2006 Lecture Notes Balanced Binary Search Trees 1 TCSS 342B v1.0 Learn about balanced binary search trees AVL trees red-black trees Objectives Talk about why they work Talk about their run-time performance. 2 TCSS 342B v1.0 AVL Trees In 1960 two Russian mathematicians Georgii Maksimovich Adel'son-Vel'skii Evgenii Mikhailovich Landis developed a technique for keeping a binary search tree balanced as items are inserted into it. called AVL trees. 3 TCSS 342B v1.0 Balanced AVL Tree 4 TCSS 342B v1.0 extremely unbalanced tree 5 TCSS 342B v1.0 AVL Trees The balance factor of node x = height of x's right subtree minus height of x's left subtree define height of empty tree to be -1. binary search tree is an AVL tree if balance factor of each node is 0, 1 or -1 Are AVL trees always completely balanced? 6 TCSS 342B v1.0 Which are AVL Trees? 7 TCSS 342B v1.0 Examples of AVL Trees 1 0 -1 0 0 -1 0 8 TCSS 342B v1.0 0 -1 1 0 -1 0 0 0 Not AVL Trees -1 -2 -1 0 0 9 TCSS 342B v1.0 -2 2 -1 0 -1 0 2 1 0 AVL Tree Data Structure extension of binary search tree where trees are always AVL trees. Maintain AVL property using rotations Rotations occur when tree becomes unbalanced from insertion or deletion At each node, keep track of height of subtree rooted at node (for balance factor computations) 10 TCSS 342B v1.0 balancing a tree with a right rotation 11 TCSS 342B v1.0 A right rotation in an AVL tree 12 TCSS 342B v1.0 FIGURE 13.11 Unbalanced tree and balanced tree after a left rotation 13 TCSS 342B v1.0 figure 10.12 A right-left rotation 14 TCSS 342B v1.0 figure 10.13 A left-right rotation 15 TCSS 342B v1.0 AVL insertion After normal BST insert, update heights from new leaf up towards root. If balance factor changes to +2 or -2, then use rotation(s) to rebalance. Let A be the first unbalanced node found. (This is the current lowest depth node that is a non-AVL subtree). Four cases: 1. A has balance factor -2 and A's left child has balance factor 1. Then perform right rotation around A. (right rotation) 2. A has and balance factor -2 and A's left child has balance factor 1. Then perform left rotation around A's left child, followed by right rotation around A. (left-right rotation) 16 TCSS 342B v1.0 AVL insertion (2) Let A be the unbalanced node. Remaining 2 cases are mirror image of the two before. 3. A has balance factor 2 and A's right child has balance factor 1. Then perform right rotation around A's right child, followed by left rotation around A. (right-left rotation) 4. A has balance factor 2 and A's right child has balance factor 1. Then perform left rotation around A. (left rotation) After rebalancing, continue up the tree updating heights. (and checking for imbalances in balance factor) Are all cases handled? What if new item added was A's left child or right child? What if A's child has balance factor 0? 17 TCSS 342B v1.0 Right rotation to fix Case 1 right rotation (clockwise): left child becomes parent; original parent demoted to right 18 TCSS 342B v1.0 Left rotation to fix Case 4 left rotation (counterclockwise): right child becomes parent; original parent demoted to left 19 TCSS 342B v1.0 a single right rotation does not fix Case 2! a single left rotation also does not fix Case 3 Problem: Cases 2, 3 20 TCSS 342B v1.0 Left-right rotation to fix Case 2 leftright double rotation: a left rotation of the left child, followed by a right rotation at the parent 21 TCSS 342B v1.0 Right-left rotation to fix Case 3 rightleft double rotation: a right rotation of the right child, followed by a left rotation at the parent 22 TCSS 342B v1.0 AVL rotation notes right-left and left-right rotation sometimes called double rotation If subtree rooted at A was an AVL tree before the insertion, then subtree rooted at A is always an AVL tree after applying the rotation(s) specified for each case. [Correctness] no further rotations will be made for ancestors of A. If A has balance factor 2, then A's left child cannot have balance factor 0. A's left child has balance factor is 1 if and only if new node was added to subtree rooted at A's leftmost grandchild. A's left child has balance factor is 1 if and only if new node was added to subtree rooted at A's left child's right child. new node must be added at grandchild level of A or deeper. 23 TCSS 342B v1.0 a right-left rotation after removal 24 TCSS 342B v1.0 AVL deletion Perform normal BST remove (with replacement of node to be removed with its successor). Then update heights from replacement node location upwards towards root. If balance factor changes to +2 or -2, then use rotation(s) to rebalance. Let A be the first unbalanced node found. (This is the current lowest depth node that is a non-AVL subtree). At least four cases: 1. A has balance factor -2 and A's left child has balance factor 1. Then perform right rotation around A. (right rotation) 2. A has and balance factor -2 and A's left child has balance factor 1. Then perform left rotation around A's left child, followed by right rotation around A. (left-right rotation) 25 TCSS 342B v1.0 AVL deletion (2) Let A be the unbalanced node. Additional 2 mirror image cases: 3. A has balance factor 2 and A's right child has balance factor 1. Then perform right rotation around A's right child, followed by left rotation around A. (right-left rotation) 4. A has balance factor 2 and A's right child has balance factor 1. Then perform left rotation around A. (left rotation) 5. Additional cases? After rebalancing, continue up the tree updating heights. Must continue checking for imbalances in balance factor, and rebalancing if necessary. Are all cases handled? What if item removed was A's left child or right child? What if A's child has balance factor 0? 26 TCSS 342B v1.0 Red-Black Trees Root property: Root is BLACK. External Property: Every external node is BLACK (external nodes: null nodes) external nodes not drawn in pictures.. Internal property: Children of a RED node are BLACK. Depth property: All external nodes have the same BLACK depth. (BLACK depth = depth counting just black nodes) 27 TCSS 342B v1.0 A tree. RedBlack Black depth 3. 30 15 10 5 40 TCSS 342B v1.0 70 20 50 55 28 60 65 80 85 90 figure 10.16 Valid red/black trees 29 TCSS 342B v1.0 red/black tree after insertion 30 TCSS 342B v1.0 red/black tree after removal 31 TCSS 342B v1.0 RedBlack Insertion 32 TCSS 342B v1.0 Red Black Trees, Insertion 1. Find proper external node. 2. Insert and color node red. 3. No black depth violation but may violate the red-black parent-child relationship. 4. Fix red-red violation on current level, then move upwards and repeat Let: z be the current red node with a red parent v. (z may have been just inserted). Let u be its grandparent. u must be black. Two cases, discussed next 1. Color root black. 33 TCSS 342B v1.0 Red child, red parent. Parent has a black sibling a b v z Vl TCSS 342B v1.0 Fixing Tree, Case one u w Zl Zr 34 Case I: Rotate and recolor Z-middle key. Black height does not change! No more red-red. a b v Zr z u w 35 TCSS 342B v1.0 Vl Zl Still Case One a b v Vr z Zr 36 u w Zl TCSS 342B v1.0 Case I: Rotate and recolor v-middle key b z a v u w Zl Zr Vr 37 TCSS 342B v1.0 Case II Red child, red parent. Parent has a red sibling. a b v z Vl TCSS 342B v1.0 u w Zr 38 Case II: Recolor Red-red may move up... a b v z Vl TCSS 342B v1.0 u w Zl Zr 39 Red Black Trees, Insertion 4. Fix red-red violation on current level, then move upwards (by two levels) and repeat Let: z, v, u be current red node, red parent, and black grandparent, Case one: v has black sibling. Then rotate middle key of (z,v,u) up to the grandparent position. Color middle key (in former grandparent position) black, and its children red. Case two: v has red sibling. Then recolor grandparent red and its children black. 40 TCSS 342B v1.0 Red Black Tree Insert 10 root 10 41 TCSS 342B v1.0 Red Black Tree Insert 10 root (external nodes not shown) 10 42 TCSS 342B v1.0 Red Black Tree Insert 85 10 85 43 TCSS 342B v1.0 Red Black Tree Insert 15 10 85 15 44 TCSS 342B v1.0 Red Black Tree Rotate Change colors 15 10 85 45 TCSS 342B v1.0 Red Black Tree Insert 70 15 10 70 85 46 TCSS 342B v1.0 Red Black Tree Change Color 15 10 70 85 47 TCSS 342B v1.0 Red Black Tree Insert 20 (sibling of parent is black) 15 10 70 20 85 48 TCSS 342B v1.0 Red Black Tree Rotate 15 10 20 70 85 49 TCSS 342B v1.0 Red Black Tree Insert 60 (sibling of parent is red) 15 10 20 60 50 TCSS 342B v1.0 70 85 Red Black Tree Change Color 15 10 20 60 51 TCSS 342B v1.0 70 85 Red Black Tree Insert 30 (sibling of parent is black) 15 10 20 60 30 TCSS 342B v1.0 52 70 85 Red Black Tree Rotate 15 10 30 20 60 53 TCSS 342B v1.0 70 85 Red Black Tree Insert 50 (sibling ?) 15 10 30 20 50 TCSS 342B v1.0 70 85 60 54 Red Black Tree Insert 50 (sibling of 70 is black!) 15 10 Child 30 20 Oops, red-red. ROTATE! TCSS 342B v1.0 gramps 15 70 30 60 50 55 Parent 70 85 Red Black Tree Double Rotate Adjust colors 30 15 10 Child-Parent-Gramps Middle goes to "top" Previous top becomes child. Its right TCSS 342B v1.0 56 70 20 50 60 85 Red Black Tree Insert 65 30 15 10 20 50 60 65 57 TCSS 342B v1.0 70 85 Red Black Tree Insert 80 30 15 10 20 50 60 65 80 58 TCSS 342B v1.0 70 85 Red Black Tree Insert 90 30 15 10 20 50 60 65 80 70 85 90 59 TCSS 342B v1.0 Red Black Tree Insert 40 30 15 10 20 50 40 TCSS 342B v1.0 70 60 65 80 85 90 60 Red Black Tree Adjust color 30 15 10 20 50 40 TCSS 342B v1.0 70 60 65 80 85 90 61 Red Black Tree Adjust color 30 15 10 20 50 40 TCSS 342B v1.0 70 60 65 80 85 90 62 Red Black Tree Insert 5 30 15 10 5 40 TCSS 342B v1.0 70 20 50 60 65 80 85 90 63 Red Black Tree Insert 55 30 15 10 5 40 TCSS 342B v1.0 70 20 50 55 60 65 80 85 90 64 Delete W...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Washington - TCSS - 342
In class drill The following 20 numbers are inserted into an binary search tree. 297 789 597 408 723 344 695 91 289 981 27 360 65 436 700 920 99 925 6151. Build this BST.2. Insert into the BST the following numbers:256006056103. Delete
Washington - TCSS - 342
In class drill: 1. Show how mergesort would sort the following numbers: 297 597 723 695 289 27 65 700 99 6152. Do the same for quicksort.3. Also try insertion sort and selection sort on the same list of numbers.
Washington - TCSS - 342
TCSS 342, Winter 2006 Lecture NotesJava's Set ADT Java's Map ADT Sample Word Finding/Counting applicationversion 1.0 1ObjectivesLearn more about Java's Collection API Learn how Set and Map are used Discuss the basics of how they are im
Washington - TCSS - 342
Hashing practice sheet. Using the standard java hash function, the hash code for a String object is computed ass[0]*31^(n-1) + s[1]*31^(n-2) + . + s[n-1]using int arithmetic, where s[i] is the ith character of the string, n is the length of the st
Washington - TCSS - 342
In class self study.Heaps: Insert the following integers into a priority queue implemented as a heap, using top down insertion. Count how many comparisons and swaps you needed to execute. Draw both the complete binary tree and the corresponding arr
Washington - TCSS - 342
A Red-Black Tree15 3 2 5 10 11 20 25 2630 50 40 55 59 60 65 6670 85 80 81 90 91 115 7102-3-4 Trees and Red-Black Trees. Converting a Red-Black tree to a 2-3-4 tree and vice-versa.Unchanged5 22 4555 7 47646949The 2-3-4 tr
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Mathematical Principles of Computing I Winter 2006 Instructor: Ed Hong edhong@u.washington.edu Pinkerton 310 (253) 692-4659 Office hours: Tuesday 11AM-12Noon, Wednesday 2PM-4PM, or by appointment (use e-mail
Washington - TCSS - 342
import java.util.*;import java.io.*;class UseScanner { / This method reads a text file and prints its contents to the screen / it illustrates using the scanner class. / the name of the text file is taken from the input arguments
Washington - TCSS - 342
Reading file Hw1testinput1.txtE 13.32%N 12.1%T 9.49%I 9.19%S 7.96%A 7.35%L 5.82%R 5.67%O 4.13%M 3.52%F 3.37%C 3.22%U 2.6%G 2.14%P 1.99%H 1.84%D 1.68%W 1.07%Y 1.07%X 0.92%V 0.61%B 0.46%J 0.31%K 0.15%Q 0.0%Z 0.0%
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Winter 2006, Section B (Hong) Assignment #7 version 1.1 Due: Thursday, March 9, 2006, 4:15 PM Please show your work and explain your reasoning. Write legibly (or type) and organize your answers. Turn in your
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Winter 2006, Section B Written Assignment 1, Answers 1. Using the definition of logarithm in class, show that logA B = logC B / logC A . Let X = logA B Let Y = logC B Let Z = logC A. To show this, we will sh
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Winter 2006, Section B (Hong) Assignment #7 solution version 1.0 Due: Thursday, March 9, 2006, 4:15 PM Please show your work and explain your reasoning. Write legibly (or type) and organize your answers. Tur
Washington - TCSS - 342
Name:_ University of Washington, Tacoma TCSS 342B (Ed Hong), Mathematical Principles of Computing I MIDTERM. 2/7/06. solutions. Version 1.0 1. True, False, True, False. 2. front temp2 10 3 5353. O(n log n). Outer loop executes n times, inner l
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Mathematical Principles of Computing I September 29, 2004. If you need to review mathematics, you may read the following book: Discrete Mathematics and its Applications, by Kenneth H. Rosen. Chapter 1 includ
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Autumn 2004, Section A Handout 02: Practice Math and Java Problems September 29, 2004 If you need to review mathematics, you may read the following book: Discrete Mathematics and its Applications, by Kenneth
Washington - TCSS - 342
TCSS 342 In-class exercise (10/04/04) (revised 10/5) Exercise 1: What is the running time (expressed in Big-Oh notation) of the following code fragment? sum = 0; for (i = 0; i < n; i+) { for (j = 0; j < n * n; j+) { sum+; } }Exercise 2: What is the
Washington - TCSS - 342
Estimating Algorithm Performance How to determine how much time an algorithm A uses to solve problem X? Depends on input; use input length as parameter. Determine function f(n) representing cost. Code it and use timer running on many inputs Anal
Washington - TCSS - 342
TCSS 342 In-class exercise (10/04/04) (revised 10/5) SOLUTIONS. Exercise 1: What is the running time (expressed in Big-Oh notation) of the following code fragment? sum = 0; for (i = 0; i < n; i+) { for (j = 0; j < n * n; j+) { sum+; } }Clearly the l
Washington - TCSS - 342
BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there.void addElement(Object el, BinaryTreeNode t) { if (t = null) t = new BinaryTreeNode(el, null, null); else if (el < t.element) addE
Washington - TCSS - 342
In class drill The following 20 numbers are inserted into an binary search tree. 297 789 597 408 723 344 695 91 289 981 27 360 65 436 700 920 99 925 6151. Build this BST.2. Insert into the BST the following numbers:256006056103. Delete
Washington - TCSS - 342
Binary heaps binary tree that satisfy two properties structural property (is a complete tree) heap-ordering property (minimum item on top) Can have maximizing heaps too.1Defining complete trees Perfect binary tree all leaves are at the sam
Washington - TCSS - 342
Graphs G = (V, E) V are the vertices; E are the edges. Edges are of the form (v, w), where v, w V. ordered pair: directed graph or digraph unordered pair: undirected graph weighted graph: A weight or cost can be associated with each edge.1Gra
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Mathematical Principles of Computing I Homework #1 Due: Wednesday, October 6, 2004, 10:30 AM Fall 2004 Ed Hong September 29, 2004Written homework is due at the beginning of class on the day specified. Any
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Autumn 2004, Section B Written Assignment 2 Assigned: Wednesday, October 6, 2004 Due: Wednesday, October 13, 2004, 10:30 AM (start of class) Please show your work and explain your reasoning. Write legibly (o
Washington - TCSS - 342
TCSS 342, Autumn 2004 Homework #4: Sieve of EratosthenesAssigned: Due: Wed 2004/10/20 Mon 2004/10/25, 10:30amYour assignment is to implement the Sieve of Eratosthenes algorithm for finding prime numbers, as describe below, and then analyze it to d
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Autumn 2004, Section B Homework #5 (Written Assignment #3) Assigned: Monday, Nov. 1, 2004 Due: Monday, Nov. 8, 2004, 10:30 AM (start of class) Please show your work and explain your reasoning. Write legibly
Washington - TCSS - 342
1TCSS 342B Fall 2004Word Counting Project, version 1.1 writeup.Project #3, (Homework #6)Out: Wednesday, November 10, 2004. Due: Submission in three stages: Part one due November 17, 10:30 AM; use e-submit to turn-in code. Part two due November 2
Washington - TCSS - 342
1TCSS 342B Fall 2004Word Counting Project, Additional notes version 1.0Project #3, (Homework #6)This document contains additional hints on the project, including dealing with stack overflow and HashTable implementation hints. StackOverflow notes
Washington - TCSS - 342
1TCSS 342B Fall 2004Word Counting Project, Additional notes version 1.0Project #3, (Homework #6)This document contains additional hints on the project, including dealing with stack overflow and HashTable implementation hints. StackOverflow notes
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342B, Mathematical Principles of Computing I Due: Wednesday, Dec. 9, 2004, 10:30 AM If you requested it, then you will be assigned a partner. Ideally, you will be able to work together closely and debug and impro
Washington - TCSS - 342
xqzxxqdxyyyyxyyyqqqxqdxxqzxxxqdxqzxqdxqdxxqdxqdxqdxxqzxqzxqzxqzxxqdxqdxqdxqdxqdxxyqdxxyyqdxxyyyqdxxyyyyqdxxqdxyxqdxyyxqdxyyyxyqdxyyxyyyqdxyyyxqdxqzxqdxqdxqd xxqxdyxqyxdxxyyxqdxyxyxyyxqqddxzzyxyyqxzxyyyqzxxqdxyyyyyy