Self-Balancing Search Trees

Slides:



Advertisements
Similar presentations
AVL Trees When bad trees happen to good programmers.
Advertisements

1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.
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.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
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.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
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.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
SELF-BALANCING SEARCH TREES Chapter 9. Self-Balancing Search Trees  The performance of a binary search tree is proportional to the height of the tree.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
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.
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.
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.
Data Structures AVL Trees.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Structures: A Pseudocode Approach with C, Second Edition1 Objectives Upon completion you will be able to: Explain the differences between a BST and.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
1 CSC TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Binary Search Trees Chapter 7 Objectives
Lecture 23 Red Black Tree Chapter 10 of textbook
AVL Trees CSE, POSTECH.
AA Trees.
Data Structures – LECTURE Balanced trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 8 – Binary Search Tree
Monday, April 16, 2018 Announcements… For Today…
AVL Tree A Balanced Binary Search Tree
TCSS 342, Winter 2006 Lecture Notes
Self-Balancing Search Trees
Wednesday, April 18, 2018 Announcements… For Today…
Friday, April 13, 2018 Announcements… For Today…
Data Structures and Algorithms
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.
AVL Trees: AVL Trees: Balanced binary search tree
AVL Trees Lab 11: AVL Trees.
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.
Tree Rotations and AVL Trees
AVL Trees CSE 373 Data Structures.
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
short illustrative repetition
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
CSC 143 Binary Search Trees.
AVL Tree Chapter 6 (cont’).
Lecture 9: Intro to Trees
8.2 Tree Traversals Chapter 8 - Trees.
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Self-Balancing Search Trees Chapter 11 Self-Balancing Search Trees Chapter 11

Tip #38: Very Late? Self-Balancing Search Trees Is someone’s junior year in college very late to learn programming? Yes, give up… Yes, give up… Or, You can take the advice of this wise Chinese proverb: “The best time to plant a tree was 20 years ago. The second best time is now.” You are still young. You have most of your life ahead of you. Regardless of what field or trade you pick up, you WILL have to learn something new, whether you like it or not. So, why not learn something that interests you?

Self-Balancing Search Trees Chapter 11 Self-Balancing Search Trees 11.1, pgs. 624-628

Tree Terminology Summary Self-Balancing Search Trees Branch (Edge) Node (Vertex) Depth Height: 4 Depth: 0 Level: 1 Height Height: 2 Depth: 1 Level: 2 Height: 3 Depth: 1 Level: 2 Height: 1 Depth: 2 Level: 3 Height: 1 Depth: 2 Level: 3 Height: 2 Depth: 2 Level: 3 Height: 1 Depth: 3 Level: 4 Root Node Internal Node Leaf Node Root node is depth 0 Leaf nodes are height 1

Self-Balancing Search Trees The performance of a binary search tree is proportional to the height of the tree or the maximum number of nodes along a path from the root to a leaf. A full binary tree of height k can hold 2k - 1 items. If a binary search tree is full and contains n items, the expected performance is O(log n). However, if a binary tree is not full, the actual performance is worse than expected. To solve this problem, we introduce self-balancing trees to achieve a balance so that the heights of the right and left subtrees are equal (or nearly equal.) Self-balancing trees include the AVL binary search tree, and non-binary search trees such as the B-tree and its specializations, the 2-3 and 2-3-4 trees, and the B+ tree.

11.1, pgs. 624-628 11.1 Tree Balance and Rotation Why Balance Is Important Rotation Algorithm for Rotation Implementing Rotation 11.1, pgs. 624-628

Why Balance is Important Self-Balancing Search Trees Searches into this unbalanced search tree are O(n), not O(log n). A realistic example of an unbalanced tree.

Rotation Self-Balancing Search Trees A binary tree with n nodes (root, leaf, and internal nodes) and of height h is balanced if: 2(h − 1) ≤ n < 2h Otherwise, it is unbalanced. For example, a binary tree with height 4 can have between 8 and 15 nodes (between 1 and 8 leaf nodes) to be balanced. We need an operation on a binary tree that changes the relative heights of left and right subtrees, but preserves the binary search tree property.

Algorithm for Right Rotation Self-Balancing Search Trees 20 10 40 5 15 7 root root = left right = data = 20 BTNode = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Right rotate around 20 = left right = data = 7 NULL BTNode

Algorithm for Right Rotation Self-Balancing Search Trees 20 10 40 5 15 7 root root = left right = data = 20 BTNode temp temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) = left right = data = 7 NULL BTNode

Algorithm for Right Rotation Self-Balancing Search Trees 20 10 40 5 15 7 root temp root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right = left right = data = 7 NULL BTNode

Algorithm for Right Rotation Self-Balancing Search Trees 20 10 40 5 15 7 root temp root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root = left right = data = 7 NULL BTNode

Algorithm for Right Rotation Self-Balancing Search Trees 20 10 40 5 15 7 root temp root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root Set root to temp = left right = data = 7 NULL BTNode

Algorithm for Right Rotation Self-Balancing Search Trees = left right = data = 20 BTNode data = 10 data = 40 NULL data = 5 data = 15 data = 7 root Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root Set root to temp 20 10 40 5 15 7 root 10 5 20 15 7 40 root

11.2 AVL Trees 11.2, pgs. 628-643 Balancing a Left-Left Tree Balancing a Left-Right Tree Four Kinds of Critically Unbalanced Trees Implementing an AVL Tree Inserting into an AVL Tree Removal from an AVL Tree Performance of the AVL 11.2, pgs. 628-643

AVL Trees Self-Balancing Search Trees In 1962 G.M. Adel'son-Vel'skiî and E.M. Landis developed a self-balancing tree. The tree is known by their initials: AVL. The AVL tree algorithm keeps track of the difference in height of each subtree. As items are added to or removed from a tree, the balance of each subtree from the insertion or removal point up to the root is updated. BalanceFactor = height(right-subtree) – height(left-subtree) The absolute difference between the left sub tree and right sub tree is never greater than 1. If the balance gets out of the range -1 to +1, the tree is rotated to bring it back into range.

Balanced: 2(h − 1) ≤ n < 2h AVL: |Rh – Lh| ≤ 1 Q38.1: Which of these trees are 1) binary, 2) balanced, and 3) AVL trees? Why or why not? 20 10 40 5 15 7 Balanced: 2(h − 1) ≤ n < 2h AVL: |Rh – Lh| ≤ 1

Not BST, not AVL, Balanced Q38.1: Which of these trees are 1) binary, 2) balanced, and 3) AVL trees? Why or why not? Not BST, not AVL, Balanced (3,1) (2,1) (0,1) BST, not AVL, not Balanced 20 10 40 5 15 7 Balanced: 2(h − 1) ≤ n < 2h AVL: |Rh – Lh| ≤ 1 (3,2) (0,1) (1,2) (1,1) BST, AVL, Balanced (3,3) (0,2) (1,2) (1,1) (0,1) BST, not AVL, Balanced

Balancing a Left-Left Tree Self-Balancing Search Trees The dark purple trapezoid represents an insertion into this tree, making its height k + 1 50 25 c a Each light purple triangle represents a tree of height k b

Balancing a Left-Left Tree Self-Balancing Search Trees The heights of the left and right subtrees are unimportant; only the relative difference matters when balancing 50 k - (k + 2) -2 25 c -1 k - (k + 1) a b The formula hR – hL is used to calculate the balance of each node

Balancing a Left-Left Tree Self-Balancing Search Trees When the root and left subtree are both left-heavy, the tree is called a Left-Left tree 50 -2 25 c -1 a b A Left-Left tree can be balanced by a rotation right

Balancing a Left-Left Tree Self-Balancing Search Trees 25 a 50 b c Even after insertion, the overall height has not increased

Balancing a Left-Right Tree Self-Balancing Search Trees k - (k + 2) 50 -2 25 c +1 (k + 1) - k a b A Left-Right tree cannot be balanced by a simple rotation right

Balancing a Left-Right Tree Self-Balancing Search Trees 50 -2 25 c +1 a b Subtree b needs to be expanded into its subtrees bL and bR

Balancing a Left-Right Tree Self-Balancing Search Trees 50 -2 25 c +1 40 a -1 bL bR 40 is left-heavy. The left subtree now can be rotated left

Balancing a Left-Right Tree Self-Balancing Search Trees 50 -2 40 c -2 25 bR The overall tree is now Left-Left and a rotation right will balance it. a bL

Balancing a Left-Right Tree Self-Balancing Search Trees 40 50 25 +1 a bL bR c

4 Critically Unbalanced Trees Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child

AVL Tree Example Build an AVL tree from the words: Self-Balancing Search Trees Build an AVL tree from the words: "The quick brown fox jumps over the lazy dog"

AVL Tree Example The Insert The Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The Insert The

AVL Tree Example The +1 quick Insert quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +1 Insert quick quick

AVL Tree Example The +2 -1 quick brown Insert brown Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +2 -1 Insert brown quick brown The overall tree is right-heavy (Right-Left) 1. Rotate right around the child (quick)

AVL Tree Example The brown +2 +1 quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The brown +2 +1 quick Rotate right around the child (quick) Rotate left around the parent (The)

AVL Tree Example brown quick The Insert fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The Insert fox

AVL Tree Example +1 brown The quick fox Insert fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 brown Insert fox The quick fox

AVL Tree Example brown quick The +1 fox Insert jumps Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +1 Insert jumps fox

The tree is now left-heavy about quick (Left-Right case) AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 -2 +1 brown Insert jumps The quick fox The tree is now left-heavy about quick (Left-Right case) jumps

AVL Tree Example brown quick The +2 -2 fox +1 jumps Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 fox +1 Rotate left around the child (fox) jumps

AVL Tree Example brown quick The +2 -2 jumps -1 fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 jumps -1 Rotate left around the child fox Rotate right around the parent

AVL Tree Example brown jumps The +1 fox quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +1 fox quick

We now have a Right-Right imbalance AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 +1 -1 brown Insert over The jumps fox quick We now have a Right-Right imbalance over

1. Rotate left around the parent AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +2 +1 fox quick -1 1. Rotate left around the parent over

AVL Tree Example jumps quick brown -1 The fox over Insert the Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown -1 The fox over Insert the

AVL Tree Example jumps brown quick The fox over the Insert the Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps Insert the brown quick The fox over the

AVL Tree Example jumps quick brown The fox over the Insert lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown The fox over the Insert lazy

AVL Tree Example +1 -1 jumps brown quick The fox over the lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 -1 jumps Insert lazy brown quick The fox over the lazy

AVL Tree Example jumps quick brown +1 -1 The fox over the lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown +1 -1 The fox over the lazy Insert dog

AVL Tree Example -1 +1 jumps brown quick The fox over the dog lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child -1 +1 jumps Insert dog brown quick The fox over the dog lazy