Balanced Search Trees CS 3110 Fall 2010. Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.

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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
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.
CS 171: Introduction to Computer Science II
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Department of Computer Science University of Maryland, College Park
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
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.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
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:
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
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.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
AVL Tree Definition: Theorem (Adel'son-Vel'skii and Landis 1962):
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
Binary Search Trees (BST)
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
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   
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each 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.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
3.1 Height-Balanced Trees 3.2 Weight-Balanced Trees
AA Trees.
UNIT III TREES.
Binary Search Tree (BST)
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
Binary Search Trees.
(edited by Nadia Al-Ghreimil)
Balanced Binary Search Trees
Binary Search Trees.
(edited by Nadia Al-Ghreimil)
AVL-Trees (Part 1).
1 Lecture 13 CS2013.
Data Structures Using C++ 2E
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Balanced Search Trees CS 3110 Fall 2010

Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need to know size in advance Insertion, deletion O(n) – need to shift elements Lists –Advantages No need to know size in advance Insertion, deletion O(1) (not counting search time) –Disadvantages Search is O(n), even if list is sorted

Balanced Search Trees Best of both! –Search, insert, delete in O(log n) time –No need to know size in advance Several flavors –AVL trees, 2-3 trees, red-black trees, skip lists, random treaps,...

Review – Binary Search Trees Every node has a left child, a right child, both, or neither Data elements are drawn from a totally ordered set Every node contains one data element Data elements are ordered in inorder

A Binary Search Tree

Binary Search Trees In any subtree: all elements smaller than the element at the root are in the left subtree all elements larger than the element at the root are in the right subtree

Search To search for an element x: if tree is empty, return false if x = object at root, return true If x < object at root, search left subtree If x > object at root, search right subtree

Search Example: search for 13

25 Search ?

6 Search ?

20 Search ?

13 Search ?

13 Search

Search type 'a tree = Node of 'a * 'a tree * 'a tree | Leaf let rec contains (t :'a tree) (x :'a) : bool = match t with Leaf -> false | Node (y, l, r) -> if x = y then true else if x < y then contains l x else contains r x

Insertion To insert an element x: search for x – if there, just return when arrive at a leaf y, make x a child of y –left child if x < y –right child if x > y

Insertion Example: insert 15

25 Insertion ?

6 Insertion ?

20 Insertion ?

13 Insertion ?

15 13 Insertion

Insertion let rec insert (x : 'a) (t : 'a tree) : 'a tree = match t with Leaf -> Node (x, Leaf, Leaf) (* if at a leaf, put new node there *) | Node (y, l, r) as t -> (* recursively search for insert point *) if x = y then t else if x > y then Node (y, l, insert x r) else (* x < y *) Node (y, insert x l, r)

Deletion To delete an element x: remove x from its node – this creates a hole if the node was a leaf, just delete it find greatest y less than x in the left subtree (or least y greater than x in the right subtree), move it to x's node this creates a hole where y was – repeat

Deletion To find least y greater than x: follow left children as far as possible in right subtree

Deletion To find greatest y less than x: follow right children as far as possible in left subtree

Deletion Example: delete 25

25 Deletion

Deletion

Deletion

Deletion

Deletion

Deletion

Deletion

Deletion

Deletion Example: delete 47

Deletion

Deletion

Deletion

Deletion

Deletion

Deletion Example: delete 29

Deletion

Deletion

Deletion

Deletion

Deletion

Deletion

Deletion

These operations take time proportional to the height of the tree (length of the longest path) O(n) if tree is not sufficiently balanced Bad case for search, insertion, and deletion – essentially like searching a list Observation

54 Solution Try to keep the tree balanced (all paths roughly the same length)

Balanced Trees Size is exponential in height Height = log 2 (size) Search, insert, delete will be O(log n)

Creating a Balanced Tree Creating one from a sorted array: Find the median, place that at the root Recursively form the left subtree from the left half of the array and the right subtree from the right half of the array

Keeping the Tree Balanced Insertions and deletions can put tree out of balance – we may have to rebalance it Can we do this efficiently?

AVL Trees Adelson-Velsky and Landis, 1962 AVL Invariant: The difference in height between the left and right subtrees of any node is never more than one

27 29 An AVL Tree Nonexistent children are considered to have height –1 Note that paths can differ in length by more than 1 (e.g., paths to 2, 48)

AVL Trees are Balanced The AVL invariant implies that: Size is at least exponential in height n   d, where  = (1 +  5)/2 ~ 1.618, the golden ratio! Height is at most logarithmic in size d  log n / log  ~ 1.44 log n

AVL Trees are Balanced To see that n   d, look at the smallest possible AVL trees of each height AVL Invariant: The difference in height between the left and right subtrees of any node is never more than one A0A0 A1A1 A2A2 A3A3

AVL Trees are Balanced AVL Invariant: The difference in height between the left and right subtrees of any node is never more than one A0A0 A1A1 A2A2 A3A3 A2A2 A1A1 To see that n   d, look at the smallest possible AVL trees of each height

AVL Trees are Balanced AVL Invariant: The difference in height between the left and right subtrees of any node is never more than one A0A0 A1A1 A2A2 A3A3 A2A2 A1A1... AdAd A d–1 A d–2 To see that n   d, look at the smallest possible AVL trees of each height

AVL Trees are Balanced A0A0 A1A1 A2A2 A3A3 A2A2 A1A1... AdAd A d–1 A d–2 A 0 = 1 A 1 = 2 A d = A d–1 + A d–2 + 1, d  2

AVL Trees are Balanced A 0 = 1 A 1 = 2 A d = A d–1 + A d–2 + 1, d 

AVL Trees are Balanced A 0 = 1 A 1 = 2 A d = A d–1 + A d–2 + 1, d  The Fibonacci sequence

AVL Trees are Balanced A 0 = 1 A 1 = 2 A d = A d–1 + A d–2 + 1, d  A d = F d+2 – 1 = O(  d )

Rebalancing Insertion and deletion can invalidate the AVL invariant May have to rebalance

Rebalancing Rotation A local rebalancing operation Preserves inorder ordering of the elements The AVL invariant can be reestablished with at most O(log n) rotations up and down the tree v uw x yx yw v u rotate  AB C BC A

Rebalancing Example: delete 27

Rebalancing

Rebalancing

Rebalancing

Rebalancing

Rebalancing

2-3 Trees Another balanced tree scheme Data stored only at the leaves Ordered left-to-right All paths of the same length Every non-leaf has either 2 or 3 children Each internal node has smallest, largest element in its subtree (for searching)

2-3 Trees smallest 2-3 tree of height d = 3 2 d = 8 data elements largest 2-3 tree of height d = 3 3 d = 27 data elements number of elements satisfies 2 d  n  3 d height satisfies d  log n

Insertion in 2-3 Trees

want to insert new element here

Insertion in 2-3 Trees

want to insert new element here

Insertion in 2-3 Trees

Deletion in 2-3 Trees want to delete this element

Deletion in 2-3 Trees

want to delete this element

Deletion in 2-3 Trees If neighbor has 3 children, borrow one

Deletion in 2-3 Trees If neighbor has 3 children, borrow one

Deletion in 2-3 Trees If neighbor has 2 children, coalesce with neighbor

Deletion in 2-3 Trees If neighbor has 2 children, coalesce with neighbor

Deletion in 2-3 Trees This may cascade up the tree!

Deletion in 2-3 Trees This may cascade up the tree!

Deletion in 2-3 Trees This may cascade up the tree!

Deletion in 2-3 Trees This may cascade up the tree!

Conclusion Balanced search trees are good Search, insert, delete in O(log n) time No need to know size in advance Several different versions –AVL trees, 2-3 trees, red-black trees, skip lists, random treaps, Huffman trees,... –find out more about them in CS4820