CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
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.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
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):
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
© 2004 Goodrich, Tamassia (2,4) Trees
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
CSCE 3110 Data Structures & Algorithm Analysis
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
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.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
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 18: Zen & the Art of O (log n ) Search.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
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.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Part-D1 Binary Search Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Balancing Binary Search Trees
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Chapter 2: Basic Data Structures
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Balanced Binary Search Trees
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees

2 Binary Search Trees - Summary Operations on binary search trees: ◦ SEARCH O(h) ◦ PREDECESSOR O(h) ◦ SUCCESSOR O(h) ◦ MINIMUM O(h) ◦ MAXIMUM O(h) ◦ INSERT O(h) ◦ DELETE O(h) These operations are fast if the height of the tree is small Theorem 12.4 The expected height of a randomly built binary search tree on n distinct keys is O(lgn) Can we make sure that h = O(lgn) ?

Balance To achieve logarithmic performance for dictionary operations (Search, Predecessor, Successor, Insert/Delete, Min/Max), we need to guarantee that we have a balanced Binary Search Tree (BST). In a balanced BST, we can afford to increase the number of nodes exponentially (e.g. 2 k, because each op takes O(log(2 k )) = O(k)) In general, we refer to BSTs that achieve this performance through some kinds of balancing rules or actions as balanced search trees.

AVL Trees Height-balance Property: For every internal node v of T, the heights of the children of v can differ by at most 1. An AVL Tree Example:

AVL Trees A non-AVL Tree Example:

Theorem: The height of an AVL tree, T, storing n items is O(log n). Proof: Let’s call n h to be the minimum number of internal nodes of an AVL tree with height h. Base cases: n 1 = 1, an AVL tree of height 1 must have at least one internal node n 2 = 2, an AVL tree of height 2 must have at least two internal nodes. General case for height, h ≥ 3: n h = 1 + n h−1 + n h−2 Note that n h values are strictly increasing as h increases (similar to the Fibonacci sequence). In other words, n h−1 > n h−2, for h ≥ 3, which allows us to simplify the above formula as: n h > 2n h−2 i.e. n h at least doubles each time h increases by 2. Which implies that n h > 2 k *n h-2k (choose h-2k = 2) n h > 2 h/2 By taking logarithms of both sides, we obtain log n h > h/2  h < 2 log n h < 2 log n Therefore, an AVL tree with n keys has height h < 2 log n  h = O(log n)

Trinode restructuring involves a node, x, which has a parent, y, and a grandparent, z. A trinode restructure temporarily renames the nodes x, y, and z as a, b, and c, so that a precedes b and b precedes c in an inorder traversal of T.

Algorithm restructure(x): Input: A node x of a BST T that has both a parent y and a grandparent z Output: Tree T after a trinode restructuring (which corresponds to a single or double rotation) involving nodes x, y, and z 1: Let (a, b, c) be a left-to-right (inorder) listing of the nodes x, y, and z, and let (T0, T1, T2, T3) be a left-to-right (inorder) listing of the four subtrees of x, y, and z not rooted at x, y, or z. 2: Replace the subtree rooted at z with a new subtree rooted at b. 3: Let a be the left child of b and let T0 and T1 be the left and right subtrees of a, respectively. 4: Let c be the right child of b and let T2 and T3 be the left and right subtrees of c, respectively.

Rotations The trinode restructure method modifies parent-child relationships of O(1) nodes in T, while preserving the inorder traversal ordering of all the nodes in T. In addition to its order-preserving property, a trinode restructuring operation changes the heights of nodes in T, so as to restore balance. restructure(x) is typically executed when z, the grandparent of x, is unbalanced. Moreover, this unbalance is due to one of the children of x now having too large a height relative to the height of z’s other child. Move up the “tall” child of x while pushing down the “short” child of z. Thus, after performing restructure(x), all the nodes in the subtree now rooted at the node b are more balanced.

Trinode restructuring example for an AVL Tree involves a node, x, which has a parent, y, and a grandparent, z.