SlideShare a Scribd company logo
1 of 25
Download to read offline
CSCI 136
Data Structures &
Advanced Programming
Lecture 25
Fall 2017
Instructor: B2
2
Last Time
• Binary search trees (Ch 14)
• The locate method
• Further Implementation
3
Today’s Outline
• Binary search trees (Ch 14)
• Tree balancing to maintain small height
• AVL Trees
• Partial taxonomy of balanced tree species
• Red-Black Trees
• Splay Trees
4
But What About Height?
• Can we design a binary search tree that is
always “shallow”?
• Yes! In many ways. Here’s one
• AVL trees
• Named after its two inventors, G.M. Adelson-
Velsky and E.M. Landis, who published a paper
about AVL trees in 1962 called "An algorithm for
the organization of information"
5
AVL Trees
6
• Balance Factor of a binary tree node:
• height of right subtree minus height of left subtree.
• A node with balance factor 1, 0, or -1 is considered
balanced.
• A node with any other balance factor is considered
unbalanced and requires rebalancing the tree.
• Definition:An AVLTree is a binary tree in which every
node is balanced.
AVL Trees
7
AVL Trees have O(log n) Height
Theorem: An AVL tree on n nodes has height O(log n)
Proof idea
• Show that an AVL tree of height h has at least fib(h)
nodes (easy induction proof---try it!)
• Recall (HW): 𝑓𝑖𝑏 ℎ ≥ ((
)
⁄ ),
if h ≥ 10
• So 𝑛 ≥ ((
)
⁄ ),
and thus log1
2
⁄ 𝑛 ≥ ℎ
• Recall that for any 𝑎, 𝑏 > 0, log7 𝑛 =
9:;< =
9:;< 7
• So log7 𝑛 and log> 𝑛 are Big-O of one another
• So h is O(log n)
8
A
B
C
+2
+1
0
A
B
C
0
0
0
Single Rotation
Unbalanced trees can be rotated to achieve balance.
9
Single Right Rotation
10
B
E
F
-2
0
1
A D
-1
0
C
0
D
E
F
-2
0
-2
B
0
A
0
C
0
B
D
E
0
+1
0
A
0
F
0
C
0
Double Rotation
11
AVL Tree Facts
• A tree that is AVL except at root, where root
balance factor equals ±2 can be rebalanced
with at most 2 rotations
• add(v) requires at most O(log n) balance
factor changes and one (single or double)
rotation to restore AVL structure
• remove(v) requires at most O(log n) balance
factor changes and (single or double) rotations
to restore AVL structure
• An AVL tree on n nodes has height O(log n)
12
AVL Trees: One of Many
There are many strategies for tree balancing to
preserve O(log n) height, including
• AVL Trees: guaranteed O(log n) height
• Red-black trees: guaranteed O(log n) height
• B-trees (not binary): guaranteed O(log n) height
• 2-3 trees, 2-3-4 trees, red-black 2-3-4 trees, ...
• Splay trees: Amortized O(log n) time operations
• Randomized trees: O(log n) expected height
13
A Red-Black Tree
(from Wikipedia.org)
14
Red-Black Trees
Red-Black trees, like AVL, guarantee shallowness
• Each node is colored red or black
• Coloring satisfies these rules
• All empty trees are black
• We consider them to be the leaves of the tree
• Children of red nodes are black
• All paths from a given node to it’s descendent leaves
have the same number of black nodes
• This is called the black height of the node
15
A Red-Black Tree
(from Wikipedia.org)
16
Red-Black Trees
The coloring rules lead to the following result
Proposition: No leaf has depth more than twice
that of any other leaf.
This in turn can be used to show
Theorem: A Red-Black tree with n internal nodes
has height satisfying ℎ ≤ 2 log(𝑛 + 1)
• Note: The tree will have exactly n+1 (empty) leaves
• since each internal node has two children
17
Red-Black Trees
Theorem: A Red-Black tree with n internal nodes has
height satisfying ℎ ≤ 2 log(𝑛 + 1)
Proof sketch: Note: we count empty tree nodes!
• If root is red, recolor it black.
• Now merge red children into (black) parents
• Now n’ ≤ n nodes and height h’ ≥ h/2
• New tree has all children with degree 2, 3, or 4
• All leaves have depth exactly h’ and there are n+1 leaves
• So 𝑛 + 1 ≥ 2,J
, so log) 𝑛 + 1 ≥ ℎK ≥
,
)
• Thus 2 log) 𝑛 + 1 ≥ ℎ
Corollary: R-B trees with n nodes have height O(log n)
18
Red-Black Tree Insertion
19
Red-Black Tree Insertion
20
Red-Black Tree Insertion
21
Red-Black Tree Insertion
22
Red-Black Tree Insertion
23
Splay Trees
Splay trees are self-adjusting binary trees
• Each time a node is accessed, it is moved to
root position via rotations
• No guarantee of balance (or shallow height)
• But good amortized performance
Theorem: Any set of m operations (add, remove,
contains, get) on an n-node splay tree take at
most O(m log n) time.
24
Splay Tree Rotations
Right Zig Rotation (left version too)
Right Zig-Zig Rotation (left version too)
Right Zig-Zag Rotation (left version too)
25
Splay Tree Iterator
• Even contains method changes splay tree shape
• This breaks the standard in-order iterator!
• Because the stack is based on the shape of the tree
• Solution: Remove the stack from the iterator
• Observation: Given location of current node (node
whose value is next to be returned), we can compute
it’s (in-order)successor in next()
• It’s either left-most leaf of right child of current, or
• It’s closest ”left-ancestor” of current
• Ancestor whose left child is also an ancestor of current
• Also, reset must “re-find” root
• Idea: Hold a single “reference” node, use it to find root

More Related Content

Similar to bst trees AVL trees-red black trees spay trees

Avl tree
Avl treeAvl tree
Avl tree
Van Pham
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
Umang Gupta
 

Similar to bst trees AVL trees-red black trees spay trees (20)

Red black trees
Red black treesRed black trees
Red black trees
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Avl tree
Avl treeAvl tree
Avl tree
 
Red black trees
Red black treesRed black trees
Red black trees
 
Lecture3
Lecture3Lecture3
Lecture3
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
 
Lec27
Lec27Lec27
Lec27
 
Red black tree
Red black treeRed black tree
Red black tree
 
Problems in parallel computations of tree functions
Problems in parallel computations of tree functionsProblems in parallel computations of tree functions
Problems in parallel computations of tree functions
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 
Algorithm presentation
Algorithm presentationAlgorithm presentation
Algorithm presentation
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptxBST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdf
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Red black tree
Red black treeRed black tree
Red black tree
 

Recently uploaded

Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
drjose256
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 

Recently uploaded (20)

Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor bank
 
Introduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsIntroduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and Applications
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission line
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Vip ℂall Girls Karkardooma Phone No 9999965857 High Profile ℂall Girl Delhi N...
Vip ℂall Girls Karkardooma Phone No 9999965857 High Profile ℂall Girl Delhi N...Vip ℂall Girls Karkardooma Phone No 9999965857 High Profile ℂall Girl Delhi N...
Vip ℂall Girls Karkardooma Phone No 9999965857 High Profile ℂall Girl Delhi N...
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Introduction to Arduino Programming: Features of Arduino
Introduction to Arduino Programming: Features of ArduinoIntroduction to Arduino Programming: Features of Arduino
Introduction to Arduino Programming: Features of Arduino
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)
 
AI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfAI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdf
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
Quiz application system project report..pdf
Quiz application system project report..pdfQuiz application system project report..pdf
Quiz application system project report..pdf
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 

bst trees AVL trees-red black trees spay trees

  • 1. CSCI 136 Data Structures & Advanced Programming Lecture 25 Fall 2017 Instructor: B2
  • 2. 2 Last Time • Binary search trees (Ch 14) • The locate method • Further Implementation
  • 3. 3 Today’s Outline • Binary search trees (Ch 14) • Tree balancing to maintain small height • AVL Trees • Partial taxonomy of balanced tree species • Red-Black Trees • Splay Trees
  • 4. 4 But What About Height? • Can we design a binary search tree that is always “shallow”? • Yes! In many ways. Here’s one • AVL trees • Named after its two inventors, G.M. Adelson- Velsky and E.M. Landis, who published a paper about AVL trees in 1962 called "An algorithm for the organization of information"
  • 6. 6 • Balance Factor of a binary tree node: • height of right subtree minus height of left subtree. • A node with balance factor 1, 0, or -1 is considered balanced. • A node with any other balance factor is considered unbalanced and requires rebalancing the tree. • Definition:An AVLTree is a binary tree in which every node is balanced. AVL Trees
  • 7. 7 AVL Trees have O(log n) Height Theorem: An AVL tree on n nodes has height O(log n) Proof idea • Show that an AVL tree of height h has at least fib(h) nodes (easy induction proof---try it!) • Recall (HW): 𝑓𝑖𝑏 ℎ ≥ (( ) ⁄ ), if h ≥ 10 • So 𝑛 ≥ (( ) ⁄ ), and thus log1 2 ⁄ 𝑛 ≥ ℎ • Recall that for any 𝑎, 𝑏 > 0, log7 𝑛 = 9:;< = 9:;< 7 • So log7 𝑛 and log> 𝑛 are Big-O of one another • So h is O(log n)
  • 11. 11 AVL Tree Facts • A tree that is AVL except at root, where root balance factor equals ±2 can be rebalanced with at most 2 rotations • add(v) requires at most O(log n) balance factor changes and one (single or double) rotation to restore AVL structure • remove(v) requires at most O(log n) balance factor changes and (single or double) rotations to restore AVL structure • An AVL tree on n nodes has height O(log n)
  • 12. 12 AVL Trees: One of Many There are many strategies for tree balancing to preserve O(log n) height, including • AVL Trees: guaranteed O(log n) height • Red-black trees: guaranteed O(log n) height • B-trees (not binary): guaranteed O(log n) height • 2-3 trees, 2-3-4 trees, red-black 2-3-4 trees, ... • Splay trees: Amortized O(log n) time operations • Randomized trees: O(log n) expected height
  • 13. 13 A Red-Black Tree (from Wikipedia.org)
  • 14. 14 Red-Black Trees Red-Black trees, like AVL, guarantee shallowness • Each node is colored red or black • Coloring satisfies these rules • All empty trees are black • We consider them to be the leaves of the tree • Children of red nodes are black • All paths from a given node to it’s descendent leaves have the same number of black nodes • This is called the black height of the node
  • 15. 15 A Red-Black Tree (from Wikipedia.org)
  • 16. 16 Red-Black Trees The coloring rules lead to the following result Proposition: No leaf has depth more than twice that of any other leaf. This in turn can be used to show Theorem: A Red-Black tree with n internal nodes has height satisfying ℎ ≤ 2 log(𝑛 + 1) • Note: The tree will have exactly n+1 (empty) leaves • since each internal node has two children
  • 17. 17 Red-Black Trees Theorem: A Red-Black tree with n internal nodes has height satisfying ℎ ≤ 2 log(𝑛 + 1) Proof sketch: Note: we count empty tree nodes! • If root is red, recolor it black. • Now merge red children into (black) parents • Now n’ ≤ n nodes and height h’ ≥ h/2 • New tree has all children with degree 2, 3, or 4 • All leaves have depth exactly h’ and there are n+1 leaves • So 𝑛 + 1 ≥ 2,J , so log) 𝑛 + 1 ≥ ℎK ≥ , ) • Thus 2 log) 𝑛 + 1 ≥ ℎ Corollary: R-B trees with n nodes have height O(log n)
  • 23. 23 Splay Trees Splay trees are self-adjusting binary trees • Each time a node is accessed, it is moved to root position via rotations • No guarantee of balance (or shallow height) • But good amortized performance Theorem: Any set of m operations (add, remove, contains, get) on an n-node splay tree take at most O(m log n) time.
  • 24. 24 Splay Tree Rotations Right Zig Rotation (left version too) Right Zig-Zig Rotation (left version too) Right Zig-Zag Rotation (left version too)
  • 25. 25 Splay Tree Iterator • Even contains method changes splay tree shape • This breaks the standard in-order iterator! • Because the stack is based on the shape of the tree • Solution: Remove the stack from the iterator • Observation: Given location of current node (node whose value is next to be returned), we can compute it’s (in-order)successor in next() • It’s either left-most leaf of right child of current, or • It’s closest ”left-ancestor” of current • Ancestor whose left child is also an ancestor of current • Also, reset must “re-find” root • Idea: Hold a single “reference” node, use it to find root